async-job-adapter-active_job 0.2.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/async/job/adapter/active_job/builder.rb +72 -0
- data/lib/async/job/adapter/active_job/dispatcher.rb +63 -0
- data/lib/async/job/adapter/active_job/queue_adapter.rb +0 -11
- data/lib/async/job/adapter/active_job/queue_handler.rb +28 -0
- data/lib/async/job/adapter/active_job/railtie.rb +53 -15
- data/lib/async/job/adapter/active_job/service.rb +4 -2
- data/lib/async/job/adapter/active_job/version.rb +1 -1
- data/readme.md +16 -0
- data.tar.gz.sig +0 -0
- metadata +5 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e85a0785c19d351e69e11d2a90c9721efaba1911d8a8d27f3f1d49c44171a71e
|
4
|
+
data.tar.gz: b32e507ccee493d1c00b2369fa8e754330d312eb3ab0bf060fc1c0dc7ef1ae8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a0460d622a76a432ea6bf92c1f7c3c7bcf6124aa2708a71d1a7ed027fc88dc2eab6a80fd3b58292ef9d5e8d371489adad0ed1354caab4aaccf6aad6648331bf
|
7
|
+
data.tar.gz: 2665fe4ee80f3c638157d28297c4a3fe7cb27974ea7ff4ca11d78a8a367fc2ea7611e4dabf89f853dadcffeb7b1ca42f5fdb851775a629acb27117a35a8e497f
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2024, by Samuel Williams.
|
5
|
+
|
6
|
+
require_relative 'queue_adapter'
|
7
|
+
require_relative 'queue_handler'
|
8
|
+
|
9
|
+
module Async
|
10
|
+
module Job
|
11
|
+
module Adapter
|
12
|
+
module ActiveJob
|
13
|
+
class Builder
|
14
|
+
Pipeline = Struct.new(:adapter, :queue)
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@enqueue = []
|
18
|
+
@dequeue = []
|
19
|
+
@handler = QueueHandler::DEFAULT
|
20
|
+
|
21
|
+
@queue = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def enqueue(middleware)
|
25
|
+
@enqueue << middleware
|
26
|
+
end
|
27
|
+
|
28
|
+
def queue(queue, *arguments, **options)
|
29
|
+
# The handler is the output side of the queue, e.g. a QueueHandler or similar wrapper.
|
30
|
+
# The queue itself is instantiated with the handler.
|
31
|
+
@queue = ->(handler){queue.new(handler, *arguments, **options)}
|
32
|
+
end
|
33
|
+
|
34
|
+
def dequeue(middleware)
|
35
|
+
@dequeue << middleware
|
36
|
+
end
|
37
|
+
|
38
|
+
def handler(handler)
|
39
|
+
@handler = handler
|
40
|
+
end
|
41
|
+
|
42
|
+
def build
|
43
|
+
# To construct the queue, we need the handler.
|
44
|
+
handler = @handler
|
45
|
+
|
46
|
+
# We then wrap the handler with the middleware in reverse order:
|
47
|
+
@dequeue.reverse_each do |middleware|
|
48
|
+
handler = middleware.new(handler)
|
49
|
+
end
|
50
|
+
|
51
|
+
# We can now construct the queue with the handler:
|
52
|
+
queue = @queue.call(handler)
|
53
|
+
|
54
|
+
adapter = queue
|
55
|
+
|
56
|
+
# We now construct the queue adapter:
|
57
|
+
@enqueue.reverse_each do |middleware|
|
58
|
+
adapter = middleware.new(queue)
|
59
|
+
end
|
60
|
+
|
61
|
+
unless adapter.is_a?(QueueAdapter)
|
62
|
+
adapter = QueueAdapter.new(adapter)
|
63
|
+
end
|
64
|
+
|
65
|
+
# The adapter is the part we provide to ActiveJob, and the handler is the part that actually executes the job:
|
66
|
+
return Pipeline.new(adapter, queue)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2024, by Samuel Williams.
|
5
|
+
|
6
|
+
require 'async/job'
|
7
|
+
require 'thread/local'
|
8
|
+
|
9
|
+
require_relative 'builder'
|
10
|
+
|
11
|
+
module Async
|
12
|
+
module Job
|
13
|
+
module Adapter
|
14
|
+
module ActiveJob
|
15
|
+
class Dispatcher
|
16
|
+
def initialize(backends, aliases = {})
|
17
|
+
@backends = backends
|
18
|
+
@aliases = aliases
|
19
|
+
|
20
|
+
@pipelines = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
attr :backends
|
24
|
+
|
25
|
+
attr :aliases
|
26
|
+
|
27
|
+
def [](name)
|
28
|
+
name = name.to_sym
|
29
|
+
|
30
|
+
@pipelines.fetch(name) do
|
31
|
+
backend = @backends.fetch(name)
|
32
|
+
@pipelines[name] = build(backend)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def enqueue(job)
|
37
|
+
name = @aliases.fetch(job.queue_name, job.queue_name)
|
38
|
+
|
39
|
+
self[name].adapter.enqueue(job)
|
40
|
+
end
|
41
|
+
|
42
|
+
def enqueue_at(job, timestamp)
|
43
|
+
name = @aliases.fetch(job.queue_name, job.queue_name)
|
44
|
+
|
45
|
+
self[name].adapter.enqueue_at(job, timestamp)
|
46
|
+
end
|
47
|
+
|
48
|
+
def start(name)
|
49
|
+
self[name].queue.start
|
50
|
+
end
|
51
|
+
|
52
|
+
private def build(backend)
|
53
|
+
builder = Builder.new
|
54
|
+
|
55
|
+
builder.instance_eval(&backend)
|
56
|
+
|
57
|
+
builder.build
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -22,17 +22,6 @@ module Async
|
|
22
22
|
Console.info(self, "Enqueueing job at...", id: job.job_id, timestamp: timestamp)
|
23
23
|
@server.schedule(@coder.dump(job.serialize), timestamp)
|
24
24
|
end
|
25
|
-
|
26
|
-
def start
|
27
|
-
Async do
|
28
|
-
@server.start
|
29
|
-
|
30
|
-
@server.each do |id, data|
|
31
|
-
job = @coder.parse(data)
|
32
|
-
::ActiveJob::Base.execute(job)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
25
|
end
|
37
26
|
end
|
38
27
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2024, by Samuel Williams.
|
5
|
+
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
module Async
|
9
|
+
module Job
|
10
|
+
module Adapter
|
11
|
+
module ActiveJob
|
12
|
+
class QueueHandler
|
13
|
+
def initialize(coder = JSON)
|
14
|
+
@coder = coder
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(job)
|
18
|
+
job = @coder.load(job)
|
19
|
+
Console.info(self, "Calling job...", id: job[:job_id])
|
20
|
+
::ActiveJob::Base.execute(job)
|
21
|
+
end
|
22
|
+
|
23
|
+
DEFAULT = self.new.freeze
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -4,38 +4,76 @@
|
|
4
4
|
# Copyright, 2024, by Samuel Williams.
|
5
5
|
|
6
6
|
require 'async/job'
|
7
|
-
|
8
7
|
require 'thread/local'
|
9
8
|
|
9
|
+
require_relative 'dispatcher'
|
10
|
+
|
11
|
+
class Thread
|
12
|
+
attr_accessor :async_job_adapter_active_job_dispatcher
|
13
|
+
end
|
10
14
|
|
11
15
|
module Async
|
12
16
|
module Job
|
13
17
|
module Adapter
|
14
18
|
module ActiveJob
|
15
19
|
class Railtie < ::Rails::Railtie
|
16
|
-
|
20
|
+
DEFAULT_REDIS = proc do
|
21
|
+
queue Async::Job::Backend::Redis
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
@backends = {default: DEFAULT_REDIS}
|
26
|
+
@aliases = {}
|
27
|
+
end
|
28
|
+
|
29
|
+
attr :backends
|
30
|
+
|
31
|
+
attr :aliases
|
32
|
+
|
33
|
+
def backend_for(name, *aliases, &block)
|
34
|
+
@backends[name] ||= block
|
35
|
+
|
36
|
+
if aliases.any?
|
37
|
+
alias_for(name, *aliases)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def alias_for(name, *aliases)
|
42
|
+
aliases.each do |alias_name|
|
43
|
+
@aliases[alias_name] = name
|
44
|
+
end
|
45
|
+
end
|
17
46
|
|
18
|
-
|
19
|
-
|
47
|
+
# Used for dispatching jobs to a thread-local backend to avoid thread-safety issues.
|
48
|
+
class ThreadLocalDispatcher
|
49
|
+
def initialize(backends, aliases)
|
50
|
+
@backends = backends
|
51
|
+
@aliases = aliases
|
52
|
+
end
|
53
|
+
|
54
|
+
def dispatcher
|
55
|
+
Thread.current.async_job_adapter_active_job_dispatcher ||= Dispatcher.new(@backends)
|
56
|
+
end
|
20
57
|
|
21
|
-
def
|
22
|
-
|
23
|
-
**Railtie.config.async_job
|
24
|
-
)
|
25
|
-
|
26
|
-
return QueueAdapter.new(server)
|
58
|
+
def enqueue_at(job, timestamp)
|
59
|
+
dispatcher.enqueue_at(job, timestamp)
|
27
60
|
end
|
28
61
|
|
29
|
-
def
|
30
|
-
|
62
|
+
def enqueue(job)
|
63
|
+
dispatcher.enqueue(job)
|
31
64
|
end
|
32
65
|
|
33
|
-
def
|
34
|
-
|
66
|
+
def start(name)
|
67
|
+
dispatcher.start(name)
|
35
68
|
end
|
36
69
|
end
|
37
70
|
|
38
|
-
|
71
|
+
def start(name = :default)
|
72
|
+
config.active_job.queue_adapter.start(name)
|
73
|
+
end
|
74
|
+
|
75
|
+
config.async_job = self
|
76
|
+
config.active_job.queue_adapter = ThreadLocalDispatcher.new(self.backends, self.aliases)
|
39
77
|
end
|
40
78
|
end
|
41
79
|
end
|
@@ -12,16 +12,18 @@ module Async
|
|
12
12
|
class Service < Async::Service::Generic
|
13
13
|
def self.each
|
14
14
|
yield :service_class, self
|
15
|
+
yield :queue_name, :default
|
15
16
|
end
|
16
17
|
|
17
18
|
def setup(container)
|
18
19
|
container.run(name: self.name, restart: true) do |instance|
|
19
|
-
|
20
|
+
evaluator = @environment.evaluator
|
21
|
+
require File.expand_path('config/environment', evaluator.root)
|
20
22
|
|
21
23
|
instance.ready!
|
22
24
|
|
23
25
|
Sync do
|
24
|
-
Railtie
|
26
|
+
Railtie.start(evaluator.queue_name)
|
25
27
|
end
|
26
28
|
end
|
27
29
|
end
|
data/readme.md
CHANGED
@@ -30,6 +30,22 @@ Then run it:
|
|
30
30
|
$ bundle exec ./job-server.rb
|
31
31
|
```
|
32
32
|
|
33
|
+
### Configuration
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
Rails.application.configure do
|
37
|
+
config.async_job.backend_for :default, :critical do
|
38
|
+
queue Async::Job::Backend::Redis, endpoint: Async::IO::Endpoint.tcp('redis.local')
|
39
|
+
end
|
40
|
+
|
41
|
+
config.async_job.aliases_for :default, :email
|
42
|
+
|
43
|
+
config.async_job.backend_for :local do
|
44
|
+
queue Async::Job::Backend::Inline
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
33
49
|
## Contributing
|
34
50
|
|
35
51
|
We welcome contributions to this project.
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async-job-adapter-active_job
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
@@ -37,7 +37,7 @@ cert_chain:
|
|
37
37
|
Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
|
38
38
|
voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
|
39
39
|
-----END CERTIFICATE-----
|
40
|
-
date: 2024-02-
|
40
|
+
date: 2024-02-25 00:00:00.000000000 Z
|
41
41
|
dependencies:
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: async-job
|
@@ -74,7 +74,10 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- lib/async/job/adapter/active_job.rb
|
77
|
+
- lib/async/job/adapter/active_job/builder.rb
|
78
|
+
- lib/async/job/adapter/active_job/dispatcher.rb
|
77
79
|
- lib/async/job/adapter/active_job/queue_adapter.rb
|
80
|
+
- lib/async/job/adapter/active_job/queue_handler.rb
|
78
81
|
- lib/async/job/adapter/active_job/railtie.rb
|
79
82
|
- lib/async/job/adapter/active_job/service.rb
|
80
83
|
- lib/async/job/adapter/active_job/version.rb
|
metadata.gz.sig
CHANGED
Binary file
|