async-job-adapter-active_job 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36873f3cd52e06462bdd73217a4176fdaf7cd458f2da0316bfa69519d1bdca99
4
- data.tar.gz: '0694a274303bad0cc146ed531cbe10358416f0e46d74a5126a40563e9813a1cc'
3
+ metadata.gz: 3d4be983547a0c60486b8b1aae93ab56cbaee13f02eaf92f453449dfb9826dd1
4
+ data.tar.gz: b7d2dc7848fd46112f89a9f071f5f541bbbcecce7de2f45d477eaeb3c2dd4adb
5
5
  SHA512:
6
- metadata.gz: 244e4b18674fd7cdeb6680352bf154a298c0aa7efaa24a84aa850119f54ce231878e7f91d77c98c78f6222b60610583cd7cff775f66c367f982c376712b240c3
7
- data.tar.gz: 259a097e1931650b0d77014db832887876cc837babf99a193776b024149d6372323931be057f432f6ca141c3f16fd5c8e2ce54b72626dcafe8cda7ab0b304b55
6
+ metadata.gz: ab85aae290c2f4673fb7d0ef65b96c94c50e3d511fb8148686e51d5bf31564f1ac4449fb3d3df655cc4115d45c20f0fd0c4f3cc5c07cca2925d3e93b06e6bbba
7
+ data.tar.gz: d84e38626ff2774069444432a29704206195403592e15b1b57a57c52cfa5e485ddcafe042357b6a0df447b7babb2a0774348460ebd979afeea3f436fab8e1131
checksums.yaml.gz.sig CHANGED
@@ -1,3 +1,2 @@
1
- ���gh�` Ġ����orN�?_*Z�Qe���
2
- ���s�8]9Q���%�Ψ�L,�&�.Rs$�ŊY4ޅS���� ��-��_JaG _�؟�
3
- ��# �fs���k�Z�9ؿ���l��Yb���`9R��U�#ߖ�Viݟy�o/�v��Fv���m����W��}<��J��#���d(� �F�|������f[��T�� .���0twK(�6@�wVI]n�=�� xU��Xz��8��0�nÕ[�F����P�2#�I��p������U��3��n{�v����������%4<�4<q|��+/P����Ԓ��@��Y�d��È� �u�o����D��V��Ry�91w|�{�}�$
1
+ �^'|�q����~�O2{23�&�鏯?���Ad�H��P
2
+ �Z�L�ER9|FGOQ̘.�E��5a�.��5��i�`]fs@3�4N���̎��?#���� � E���u[����H�?8���Iqqo C�ܹ[]̈{`�Vߓ�޲��F����s�[�����e���w`��Й?XD�S�yA^�6�V�i�k�ޅA/z*K)BRD�
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2024, by Samuel Williams.
5
+
6
+ require 'active_job/queue_adapters/abstract_adapter'
7
+
8
+ module ActiveJob
9
+ module QueueAdapters
10
+ class AsyncJobAdapter < AbstractAdapter
11
+ def initialize(dispatcher = ::Async::Job::Adapter::ActiveJob::Railtie.dispatcher)
12
+ @dispatcher = dispatcher
13
+ end
14
+
15
+ # Enqueue a job for processing.
16
+ def enqueue(job)
17
+ @dispatcher.call(job)
18
+ end
19
+
20
+ # Enqueue a job for processing at a specific time.
21
+ def enqueue_at(job, timestamp)
22
+ @dispatcher.call(job)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -4,10 +4,8 @@
4
4
  # Copyright, 2024, by Samuel Williams.
5
5
 
6
6
  require_relative 'executor'
7
- require_relative 'interface'
8
7
 
9
8
  require 'async/job'
10
- require 'thread/local'
11
9
  require 'async/job/builder'
12
10
 
13
11
  module Async
@@ -41,18 +39,10 @@ module Async
41
39
  end
42
40
  end
43
41
 
44
- # Enqueue a job for processing.
45
- def enqueue(job)
42
+ def call(job)
46
43
  name = @aliases.fetch(job.queue_name, job.queue_name)
47
44
 
48
- self[name].client.enqueue(job)
49
- end
50
-
51
- # Enqueue a job for processing at a specific time.
52
- def enqueue_at(job, timestamp)
53
- name = @aliases.fetch(job.queue_name, job.queue_name)
54
-
55
- self[name].client.enqueue_at(job, timestamp)
45
+ self[name].client.call(job.serialize)
56
46
  end
57
47
 
58
48
  # Start processing jobs in the given queue.
@@ -65,12 +55,7 @@ module Async
65
55
 
66
56
  builder.instance_eval(&definition)
67
57
 
68
- builder.build do |client|
69
- # Ensure that the client is an interface:
70
- unless client.is_a?(Interface)
71
- Interface.new(client)
72
- end
73
- end
58
+ return builder.build
74
59
  end
75
60
  end
76
61
  end
@@ -5,6 +5,12 @@
5
5
 
6
6
  require 'json'
7
7
 
8
+ require 'console'
9
+ require 'console/event/failure'
10
+
11
+ require 'active_job'
12
+ require 'active_job/base'
13
+
8
14
  module Async
9
15
  module Job
10
16
  module Adapter
@@ -15,15 +21,26 @@ module Async
15
21
  @delegate = delegate
16
22
  end
17
23
 
24
+ def execute(job_data)
25
+ ::ActiveJob::Callbacks.run_callbacks(:execute) do
26
+ job = ::ActiveJob::Base.deserialize(job_data)
27
+
28
+ begin
29
+ job.perform_now
30
+ rescue => error
31
+ # Ignore, as ActiveJob has already logged the error.
32
+ end
33
+ end
34
+ end
35
+
18
36
  # Execute the given job.
19
37
  def call(job)
20
- Console.debug(self, "Executing job...", job: job)
21
38
  begin
22
- ::ActiveJob::Base.execute(job)
39
+ execute(job)
23
40
  rescue => error
24
41
  # Error handling is done by the job itself.
25
42
  # Ignore the error here, as ActiveJob has already logged unhandled errors.
26
- # Console::Event::Failure.for(error).emit(self, "Failed to execute job!", job: job)
43
+ Console::Event::Failure.for(error).emit(self, "Failed to execute job!", job: job)
27
44
  end
28
45
 
29
46
  @delegate&.call(job)
@@ -6,11 +6,7 @@
6
6
  require 'async/job'
7
7
  require 'async/job/processor/inline'
8
8
 
9
- require 'thread/local'
10
-
11
- require_relative 'dispatcher'
12
-
13
- Thread.attr_accessor :async_job_adapter_active_job_dispatcher
9
+ require_relative 'thread_local_dispatcher'
14
10
 
15
11
  module Async
16
12
  module Job
@@ -26,6 +22,8 @@ module Async
26
22
  def initialize
27
23
  @definitions = {"default" => DEFAULT_QUEUE_DEFINITION}
28
24
  @aliases = {}
25
+
26
+ @dispatcher = ThreadLocalDispatcher.new(@definitions, @aliases)
29
27
  end
30
28
 
31
29
  # The queues that are available for processing jobs.
@@ -34,11 +32,16 @@ module Async
34
32
  # The aliases for the definitions, if any.
35
33
  attr :aliases
36
34
 
35
+ # Thed default dispatcher for processing jobs.
36
+ attr :dispatcher
37
+
37
38
  # Define a new backend for processing jobs.
38
39
  # @parameter name [String] The name of the backend.
39
40
  # @parameter aliases [Array(String)] The aliases for the backend.
40
41
  # @parameter block [Proc] The block that defines the backend.
41
42
  def define_queue(name, *aliases, &block)
43
+ name = name.to_s
44
+
42
45
  @definitions[name] = block
43
46
 
44
47
  if aliases.any?
@@ -49,49 +52,18 @@ module Async
49
52
  # Define an alias for a queue.
50
53
  def alias_queue(name, *aliases)
51
54
  aliases.each do |alias_name|
55
+ alias_name = alias_name.to_s
56
+
52
57
  @aliases[alias_name] = name
53
58
  end
54
59
  end
55
60
 
56
- # Used for dispatching jobs to a thread-local queue to avoid thread-safety issues.
57
- class ThreadLocalDispatcher
58
- def initialize(definitions, aliases)
59
- @definitions = definitions
60
- @aliases = aliases
61
- end
62
-
63
- # The dispatcher for the current thread.
64
- def dispatcher
65
- Thread.current.async_job_adapter_active_job_dispatcher ||= Dispatcher.new(@definitions, @aliases)
66
- end
67
-
68
- # Enqueue a job to be processed at a specific time.
69
- def enqueue_at(job, timestamp)
70
- dispatcher.enqueue_at(job, timestamp)
71
- end
72
-
73
- # Enqueue a job to be processed as soon as possible.
74
- def enqueue(job)
75
- dispatcher.enqueue(job)
76
- end
77
-
78
- # Start processing jobs in the queue with the given name.
79
- # @parameter name [String] The name of the queue.
80
- def start(name)
81
- dispatcher.start(name)
82
- end
83
- end
84
-
85
61
  # Start the job server with the given name.
86
62
  def start(name = "default")
87
- config.active_job.queue_adapter.start(name)
63
+ @dispatcher.start(name)
88
64
  end
89
65
 
90
- DEFAULT_DISPATCHER = ThreadLocalDispatcher.new(self.definitions, self.aliases)
91
-
92
66
  config.async_job = self
93
-
94
- config.active_job.queue_adapter = DEFAULT_DISPATCHER
95
67
  end
96
68
  end
97
69
  end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2024, by Samuel Williams.
5
+
6
+ require_relative 'dispatcher'
7
+
8
+ Thread.attr_accessor :async_job_adapter_active_job_dispatcher
9
+
10
+ module Async
11
+ module Job
12
+ module Adapter
13
+ module ActiveJob
14
+ # Used for dispatching jobs to a thread-local queue to avoid thread-safety issues.
15
+ class ThreadLocalDispatcher
16
+ def initialize(definitions, aliases)
17
+ @definitions = definitions
18
+ @aliases = aliases
19
+ end
20
+
21
+ # @attribute [Hash(String, Proc)] The definitions to use for creating queues.
22
+ attr :definitions
23
+
24
+ # @attribute [Hash(String, String)] The aliases for the job queues.
25
+ attr :aliases
26
+
27
+ # The dispatcher for the current thread.
28
+ #
29
+ # As a dispatcher contains state, it is important to ensure that each thread has its own dispatcher.
30
+ def dispatcher
31
+ Thread.current.async_job_adapter_active_job_dispatcher ||= Dispatcher.new(@definitions, @aliases)
32
+ end
33
+
34
+ def [](name)
35
+ dispatcher[name]
36
+ end
37
+
38
+ def call(job)
39
+ dispatcher.call(job)
40
+ end
41
+
42
+ # Start processing jobs in the queue with the given name.
43
+ # @parameter name [String] The name of the queue.
44
+ def start(name)
45
+ dispatcher.start(name)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -7,7 +7,7 @@ module Async
7
7
  module Job
8
8
  module Adapter
9
9
  module ActiveJob
10
- VERSION = "0.12.0"
10
+ VERSION = "0.13.0"
11
11
  end
12
12
  end
13
13
  end
@@ -3,7 +3,10 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2024, by Samuel Williams.
5
5
 
6
- require_relative 'active_job/version'
7
- require_relative 'active_job/interface'
8
- require_relative 'active_job/executor'
9
- require_relative "active_job/railtie" if defined?(Rails::Railtie)
6
+ require_relative "active_job/version"
7
+ require_relative "active_job/executor"
8
+
9
+ if defined?(Rails::Railtie)
10
+ require_relative "active_job/railtie"
11
+ require "active_job/queue_adapters/async_job_adapter"
12
+ end
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.12.0
4
+ version: 0.13.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-08-09 00:00:00.000000000 Z
40
+ date: 2024-08-11 00:00:00.000000000 Z
41
41
  dependencies:
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: async-job
@@ -67,20 +67,6 @@ dependencies:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0.12'
70
- - !ruby/object:Gem::Dependency
71
- name: thread-local
72
- requirement: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: '0'
77
- type: :runtime
78
- prerelease: false
79
- version_requirements: !ruby/object:Gem::Requirement
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- version: '0'
84
70
  description:
85
71
  email:
86
72
  executables:
@@ -89,13 +75,14 @@ extensions: []
89
75
  extra_rdoc_files: []
90
76
  files:
91
77
  - bin/async-job-adapter-active_job-server
78
+ - lib/active_job/queue_adapters/async_job_adapter.rb
92
79
  - lib/async/job/adapter/active_job.rb
93
80
  - lib/async/job/adapter/active_job/dispatcher.rb
94
81
  - lib/async/job/adapter/active_job/environment.rb
95
82
  - lib/async/job/adapter/active_job/executor.rb
96
- - lib/async/job/adapter/active_job/interface.rb
97
83
  - lib/async/job/adapter/active_job/railtie.rb
98
84
  - lib/async/job/adapter/active_job/service.rb
85
+ - lib/async/job/adapter/active_job/thread_local_dispatcher.rb
99
86
  - lib/async/job/adapter/active_job/version.rb
100
87
  - license.md
101
88
  - readme.md
metadata.gz.sig CHANGED
Binary file
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Released under the MIT License.
4
- # Copyright, 2024, by Samuel Williams.
5
-
6
- module Async
7
- module Job
8
- module Adapter
9
- module ActiveJob
10
- # An interface for `ActiveJob` that allows you to use `Async::Job` as the queue.
11
- class Interface
12
- def initialize(delegate)
13
- @delegate = delegate
14
- end
15
-
16
- # Enqueue a job for processing.
17
- def enqueue(job)
18
- Console.debug(self, "Enqueueing job...", id: job.job_id)
19
- @delegate.call(serialize(job))
20
- end
21
-
22
- # Enqueue a job for processing at a specific time.
23
- def enqueue_at(job, timestamp)
24
- # We assume the given timestamp is the same as `job.scheduled_at` which is true in every case we've seen so far.
25
- Console.debug(self, "Scheduling job...", id: job.job_id, scheduled_at: job.scheduled_at)
26
- @delegate.call(serialize(job))
27
- end
28
-
29
- protected
30
-
31
- def serialize(job)
32
- job.serialize
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end