activejob 8.0.2.1 → 8.1.0.beta1

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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +60 -27
  3. data/README.md +8 -6
  4. data/lib/active_job/arguments.rb +46 -47
  5. data/lib/active_job/base.rb +4 -6
  6. data/lib/active_job/configured_job.rb +5 -4
  7. data/lib/active_job/continuable.rb +102 -0
  8. data/lib/active_job/continuation/step.rb +83 -0
  9. data/lib/active_job/continuation/test_helper.rb +89 -0
  10. data/lib/active_job/continuation/validation.rb +50 -0
  11. data/lib/active_job/continuation.rb +332 -0
  12. data/lib/active_job/core.rb +12 -2
  13. data/lib/active_job/enqueue_after_transaction_commit.rb +1 -26
  14. data/lib/active_job/enqueuing.rb +8 -4
  15. data/lib/active_job/exceptions.rb +16 -6
  16. data/lib/active_job/execution_state.rb +11 -0
  17. data/lib/active_job/gem_version.rb +3 -3
  18. data/lib/active_job/instrumentation.rb +12 -12
  19. data/lib/active_job/log_subscriber.rb +61 -6
  20. data/lib/active_job/queue_adapters/abstract_adapter.rb +6 -0
  21. data/lib/active_job/queue_adapters/async_adapter.rb +5 -1
  22. data/lib/active_job/queue_adapters/sidekiq_adapter.rb +19 -0
  23. data/lib/active_job/queue_adapters/test_adapter.rb +5 -1
  24. data/lib/active_job/railtie.rb +9 -19
  25. data/lib/active_job/serializers/action_controller_parameters_serializer.rb +25 -0
  26. data/lib/active_job/serializers/big_decimal_serializer.rb +3 -4
  27. data/lib/active_job/serializers/date_serializer.rb +3 -4
  28. data/lib/active_job/serializers/date_time_serializer.rb +3 -4
  29. data/lib/active_job/serializers/duration_serializer.rb +5 -6
  30. data/lib/active_job/serializers/module_serializer.rb +3 -4
  31. data/lib/active_job/serializers/object_serializer.rb +11 -13
  32. data/lib/active_job/serializers/range_serializer.rb +9 -9
  33. data/lib/active_job/serializers/symbol_serializer.rb +4 -5
  34. data/lib/active_job/serializers/time_serializer.rb +3 -4
  35. data/lib/active_job/serializers/time_with_zone_serializer.rb +3 -4
  36. data/lib/active_job/serializers.rb +46 -15
  37. data/lib/active_job.rb +2 -0
  38. metadata +13 -9
  39. data/lib/active_job/queue_adapters/sucker_punch_adapter.rb +0 -56
  40. data/lib/active_job/timezones.rb +0 -13
  41. data/lib/active_job/translation.rb +0 -13
@@ -18,6 +18,25 @@ module ActiveJob
18
18
  #
19
19
  # Rails.application.config.active_job.queue_adapter = :sidekiq
20
20
  class SidekiqAdapter < AbstractAdapter
21
+ def initialize(*) # :nodoc:
22
+ @stopping = false
23
+
24
+ Sidekiq.configure_server do |config|
25
+ config.on(:quiet) { @stopping = true }
26
+ end
27
+
28
+ Sidekiq.configure_client do |config|
29
+ config.on(:quiet) { @stopping = true }
30
+ end
31
+ end
32
+
33
+ def check_adapter
34
+ ActiveJob.deprecator.warn <<~MSG.squish
35
+ The built-in `sidekiq` adapter is deprecated and will be removed in Rails 8.2.
36
+ Please upgrade `sidekiq` gem to version 7.3.3 or later to use the `sidekiq` gem's adapter.
37
+ MSG
38
+ end
39
+
21
40
  def enqueue(job) # :nodoc:
22
41
  job.provider_job_id = JobWrapper.set(
23
42
  wrapped: job.class,
@@ -12,7 +12,7 @@ module ActiveJob
12
12
  #
13
13
  # Rails.application.config.active_job.queue_adapter = :test
14
14
  class TestAdapter < AbstractAdapter
15
- attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter, :reject, :queue, :at)
15
+ attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter, :reject, :queue, :at, :stopping)
16
16
  attr_writer(:enqueued_jobs, :performed_jobs)
17
17
 
18
18
  # Provides a store of all the enqueued jobs with the TestAdapter so you can check them.
@@ -35,6 +35,10 @@ module ActiveJob
35
35
  perform_or_enqueue(perform_enqueued_at_jobs && !filtered?(job), job, job_data)
36
36
  end
37
37
 
38
+ def stopping?
39
+ @stopping.is_a?(Proc) ? @stopping.call : @stopping
40
+ end
41
+
38
42
  private
39
43
  def job_to_hash(job, extras = {})
40
44
  job.serialize.tap do |job_data|
@@ -19,7 +19,7 @@ module ActiveJob
19
19
  end
20
20
 
21
21
  initializer "active_job.custom_serializers" do |app|
22
- config.after_initialize do
22
+ ActiveSupport.on_load(:active_job) do
23
23
  custom_serializers = app.config.active_job.custom_serializers
24
24
  ActiveJob::Serializers.add_serializers custom_serializers
25
25
  end
@@ -29,25 +29,14 @@ module ActiveJob
29
29
  ActiveSupport.on_load(:active_job) do
30
30
  ActiveSupport.on_load(:active_record) do
31
31
  ActiveJob::Base.include EnqueueAfterTransactionCommit
32
+ end
33
+ end
34
+ end
32
35
 
33
- if app.config.active_job.key?(:enqueue_after_transaction_commit)
34
- ActiveJob.deprecator.warn(<<~MSG.squish)
35
- `config.active_job.enqueue_after_transaction_commit` is deprecated and will be removed in Rails 8.1.
36
- This configuration can still be set on individual jobs using `self.enqueue_after_transaction_commit=`,
37
- but due the nature of this behavior, it is not recommended to be set globally.
38
- MSG
39
-
40
- value = case app.config.active_job.enqueue_after_transaction_commit
41
- when :always
42
- true
43
- when :never
44
- false
45
- else
46
- false
47
- end
48
-
49
- ActiveJob::Base.enqueue_after_transaction_commit = value
50
- end
36
+ initializer "active_job.action_controller_parameters" do |app|
37
+ ActiveSupport.on_load(:active_job) do
38
+ ActiveSupport.on_load(:action_controller) do
39
+ ActiveJob::Serializers.add_serializers ActiveJob::Serializers::ActionControllerParametersSerializer
51
40
  end
52
41
  end
53
42
  end
@@ -70,6 +59,7 @@ module ActiveJob
70
59
  options = options.except(
71
60
  :log_query_tags_around_perform,
72
61
  :custom_serializers,
62
+ # This config can't be applied globally, so we need to remove otherwise it will be applied to `ActiveJob::Base`.
73
63
  :enqueue_after_transaction_commit
74
64
  )
75
65
 
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveJob
4
+ module Serializers
5
+ class ActionControllerParametersSerializer < ObjectSerializer
6
+ def serialize(argument)
7
+ Arguments.serialize(argument.to_h.with_indifferent_access)
8
+ end
9
+
10
+ def deserialize(hash)
11
+ raise NotImplementedError # Serialized as a HashWithIndifferentAccess
12
+ end
13
+
14
+ def serialize?(argument)
15
+ argument.respond_to?(:permitted?) && argument.respond_to?(:to_h)
16
+ end
17
+
18
+ def klass
19
+ if defined?(ActionController::Parameters)
20
+ ActionController::Parameters
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -13,10 +13,9 @@ module ActiveJob
13
13
  BigDecimal(hash["value"])
14
14
  end
15
15
 
16
- private
17
- def klass
18
- BigDecimal
19
- end
16
+ def klass
17
+ BigDecimal
18
+ end
20
19
  end
21
20
  end
22
21
  end
@@ -11,10 +11,9 @@ module ActiveJob
11
11
  Date.iso8601(hash["value"])
12
12
  end
13
13
 
14
- private
15
- def klass
16
- Date
17
- end
14
+ def klass
15
+ Date
16
+ end
18
17
  end
19
18
  end
20
19
  end
@@ -7,10 +7,9 @@ module ActiveJob
7
7
  DateTime.iso8601(hash["value"])
8
8
  end
9
9
 
10
- private
11
- def klass
12
- DateTime
13
- end
10
+ def klass
11
+ DateTime
12
+ end
14
13
  end
15
14
  end
16
15
  end
@@ -6,20 +6,19 @@ module ActiveJob
6
6
  def serialize(duration)
7
7
  # Ideally duration.parts would be wrapped in an array before passing to Arguments.serialize,
8
8
  # but we continue passing the bare hash for backwards compatibility:
9
- super("value" => duration.value, "parts" => Arguments.serialize(duration.parts))
9
+ super("value" => duration.value, "parts" => Arguments.serialize(duration.parts.to_a))
10
10
  end
11
11
 
12
12
  def deserialize(hash)
13
13
  value = hash["value"]
14
- parts = Arguments.deserialize(hash["parts"])
14
+ parts = Arguments.deserialize(hash["parts"].to_h)
15
15
  # `parts` is originally a hash, but will have been flattened to an array by Arguments.serialize
16
16
  klass.new(value, parts.to_h)
17
17
  end
18
18
 
19
- private
20
- def klass
21
- ActiveSupport::Duration
22
- end
19
+ def klass
20
+ ActiveSupport::Duration
21
+ end
23
22
  end
24
23
  end
25
24
  end
@@ -12,10 +12,9 @@ module ActiveJob
12
12
  hash["value"].constantize
13
13
  end
14
14
 
15
- private
16
- def klass
17
- Module
18
- end
15
+ def klass
16
+ Module
17
+ end
19
18
  end
20
19
  end
21
20
  end
@@ -17,11 +17,9 @@ module ActiveJob
17
17
  # Money.new(hash["amount"], hash["currency"])
18
18
  # end
19
19
  #
20
- # private
21
- #
22
- # def klass
23
- # Money
24
- # end
20
+ # def klass
21
+ # Money
22
+ # end
25
23
  # end
26
24
  class ObjectSerializer
27
25
  include Singleton
@@ -37,19 +35,19 @@ module ActiveJob
37
35
 
38
36
  # Serializes an argument to a JSON primitive type.
39
37
  def serialize(hash)
40
- { Arguments::OBJECT_SERIALIZER_KEY => self.class.name }.merge!(hash)
38
+ hash[Arguments::OBJECT_SERIALIZER_KEY] = self.class.name
39
+ hash
41
40
  end
42
41
 
43
42
  # Deserializes an argument from a JSON primitive type.
44
- def deserialize(json)
45
- raise NotImplementedError
43
+ def deserialize(hash)
44
+ raise NotImplementedError, "#{self.class.name} should implement a public #deserialize(hash) method"
46
45
  end
47
46
 
48
- private
49
- # The class of the object that will be serialized.
50
- def klass # :doc:
51
- raise NotImplementedError
52
- end
47
+ # The class of the object that will be serialized.
48
+ def klass
49
+ raise NotImplementedError, "#{self.class.name} should implement a public #klass method"
50
+ end
53
51
  end
54
52
  end
55
53
  end
@@ -3,21 +3,21 @@
3
3
  module ActiveJob
4
4
  module Serializers
5
5
  class RangeSerializer < ObjectSerializer
6
- KEYS = %w[begin end exclude_end].freeze
7
-
8
6
  def serialize(range)
9
- args = Arguments.serialize([range.begin, range.end, range.exclude_end?])
10
- super(KEYS.zip(args).to_h)
7
+ super(
8
+ "begin" => Arguments.serialize(range.begin),
9
+ "end" => Arguments.serialize(range.end),
10
+ "exclude_end" => range.exclude_end?, # Always boolean, no need to serialize
11
+ )
11
12
  end
12
13
 
13
14
  def deserialize(hash)
14
- klass.new(*Arguments.deserialize(hash.values_at(*KEYS)))
15
+ Range.new(*Arguments.deserialize([hash["begin"], hash["end"]]), hash["exclude_end"])
15
16
  end
16
17
 
17
- private
18
- def klass
19
- ::Range
20
- end
18
+ def klass
19
+ ::Range
20
+ end
21
21
  end
22
22
  end
23
23
  end
@@ -4,17 +4,16 @@ module ActiveJob
4
4
  module Serializers
5
5
  class SymbolSerializer < ObjectSerializer # :nodoc:
6
6
  def serialize(argument)
7
- super("value" => argument.to_s)
7
+ super("value" => argument.name)
8
8
  end
9
9
 
10
10
  def deserialize(argument)
11
11
  argument["value"].to_sym
12
12
  end
13
13
 
14
- private
15
- def klass
16
- Symbol
17
- end
14
+ def klass
15
+ Symbol
16
+ end
18
17
  end
19
18
  end
20
19
  end
@@ -7,10 +7,9 @@ module ActiveJob
7
7
  Time.iso8601(hash["value"])
8
8
  end
9
9
 
10
- private
11
- def klass
12
- Time
13
- end
10
+ def klass
11
+ Time
12
+ end
14
13
  end
15
14
  end
16
15
  end
@@ -16,10 +16,9 @@ module ActiveJob
16
16
  Time.iso8601(hash["value"]).in_time_zone(hash["time_zone"] || Time.zone)
17
17
  end
18
18
 
19
- private
20
- def klass
21
- ActiveSupport::TimeWithZone
22
- end
19
+ def klass
20
+ ActiveSupport::TimeWithZone
21
+ end
23
22
  end
24
23
  end
25
24
  end
@@ -19,16 +19,17 @@ module ActiveJob
19
19
  autoload :ModuleSerializer
20
20
  autoload :RangeSerializer
21
21
  autoload :BigDecimalSerializer
22
+ autoload :ActionControllerParametersSerializer
22
23
 
23
- mattr_accessor :_additional_serializers
24
- self._additional_serializers = Set.new
24
+ @serializers = Set.new
25
+ @serializers_index = {}
25
26
 
26
27
  class << self
27
28
  # Returns serialized representative of the passed object.
28
29
  # Will look up through all known serializers.
29
30
  # Raises ActiveJob::SerializationError if it can't find a proper serializer.
30
31
  def serialize(argument)
31
- serializer = serializers.detect { |s| s.serialize?(argument) }
32
+ serializer = @serializers_index[argument.class] || serializers.find { |s| s.serialize?(argument) }
32
33
  raise SerializationError.new("Unsupported argument type: #{argument.class.name}") unless serializer
33
34
  serializer.serialize(argument)
34
35
  end
@@ -47,24 +48,54 @@ module ActiveJob
47
48
  end
48
49
 
49
50
  # Returns list of known serializers.
50
- def serializers
51
- self._additional_serializers
51
+ attr_reader :serializers
52
+
53
+ def serializers=(serializers)
54
+ @serializers = serializers
55
+ index_serializers
52
56
  end
53
57
 
54
58
  # Adds new serializers to a list of known serializers.
55
59
  def add_serializers(*new_serializers)
56
- self._additional_serializers += new_serializers.flatten
60
+ new_serializers = new_serializers.flatten
61
+ new_serializers.map! do |s|
62
+ if s.is_a?(Class) && s < ObjectSerializer
63
+ s.instance
64
+ else
65
+ s
66
+ end
67
+ end
68
+
69
+ @serializers += new_serializers
70
+ index_serializers
71
+ @serializers
57
72
  end
73
+
74
+ private
75
+ def index_serializers
76
+ @serializers_index.clear
77
+ serializers.each do |s|
78
+ if s.respond_to?(:klass)
79
+ @serializers_index[s.klass] = s
80
+ elsif s.respond_to?(:klass, true)
81
+ klass = s.send(:klass)
82
+ ActiveJob.deprecator.warn(<<~MSG.squish)
83
+ #{s.class.name}#klass method should be public.
84
+ MSG
85
+ @serializers_index[klass] = s
86
+ end
87
+ end
88
+ end
58
89
  end
59
90
 
60
- add_serializers SymbolSerializer,
61
- DurationSerializer,
62
- DateTimeSerializer,
63
- DateSerializer,
64
- TimeWithZoneSerializer,
65
- TimeSerializer,
66
- ModuleSerializer,
67
- RangeSerializer,
68
- BigDecimalSerializer
91
+ add_serializers SymbolSerializer.instance,
92
+ DurationSerializer.instance,
93
+ DateTimeSerializer.instance,
94
+ DateSerializer.instance,
95
+ TimeWithZoneSerializer.instance,
96
+ TimeSerializer.instance,
97
+ ModuleSerializer.instance,
98
+ RangeSerializer.instance,
99
+ BigDecimalSerializer.instance
69
100
  end
70
101
  end
data/lib/active_job.rb CHANGED
@@ -39,9 +39,11 @@ module ActiveJob
39
39
  autoload :Arguments
40
40
  autoload :DeserializationError, "active_job/arguments"
41
41
  autoload :SerializationError, "active_job/arguments"
42
+ autoload :UnknownJobClassError, "active_job/core"
42
43
  autoload :EnqueueAfterTransactionCommit
43
44
 
44
45
  eager_autoload do
46
+ autoload :Continuation
45
47
  autoload :Serializers
46
48
  autoload :ConfiguredJob
47
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activejob
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.0.2.1
4
+ version: 8.1.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 8.0.2.1
18
+ version: 8.1.0.beta1
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 8.0.2.1
25
+ version: 8.1.0.beta1
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: globalid
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -51,12 +51,18 @@ files:
51
51
  - lib/active_job/base.rb
52
52
  - lib/active_job/callbacks.rb
53
53
  - lib/active_job/configured_job.rb
54
+ - lib/active_job/continuable.rb
55
+ - lib/active_job/continuation.rb
56
+ - lib/active_job/continuation/step.rb
57
+ - lib/active_job/continuation/test_helper.rb
58
+ - lib/active_job/continuation/validation.rb
54
59
  - lib/active_job/core.rb
55
60
  - lib/active_job/deprecator.rb
56
61
  - lib/active_job/enqueue_after_transaction_commit.rb
57
62
  - lib/active_job/enqueuing.rb
58
63
  - lib/active_job/exceptions.rb
59
64
  - lib/active_job/execution.rb
65
+ - lib/active_job/execution_state.rb
60
66
  - lib/active_job/gem_version.rb
61
67
  - lib/active_job/instrumentation.rb
62
68
  - lib/active_job/log_subscriber.rb
@@ -72,12 +78,12 @@ files:
72
78
  - lib/active_job/queue_adapters/resque_adapter.rb
73
79
  - lib/active_job/queue_adapters/sidekiq_adapter.rb
74
80
  - lib/active_job/queue_adapters/sneakers_adapter.rb
75
- - lib/active_job/queue_adapters/sucker_punch_adapter.rb
76
81
  - lib/active_job/queue_adapters/test_adapter.rb
77
82
  - lib/active_job/queue_name.rb
78
83
  - lib/active_job/queue_priority.rb
79
84
  - lib/active_job/railtie.rb
80
85
  - lib/active_job/serializers.rb
86
+ - lib/active_job/serializers/action_controller_parameters_serializer.rb
81
87
  - lib/active_job/serializers/big_decimal_serializer.rb
82
88
  - lib/active_job/serializers/date_serializer.rb
83
89
  - lib/active_job/serializers/date_time_serializer.rb
@@ -91,8 +97,6 @@ files:
91
97
  - lib/active_job/serializers/time_with_zone_serializer.rb
92
98
  - lib/active_job/test_case.rb
93
99
  - lib/active_job/test_helper.rb
94
- - lib/active_job/timezones.rb
95
- - lib/active_job/translation.rb
96
100
  - lib/active_job/version.rb
97
101
  - lib/rails/generators/job/USAGE
98
102
  - lib/rails/generators/job/job_generator.rb
@@ -103,10 +107,10 @@ licenses:
103
107
  - MIT
104
108
  metadata:
105
109
  bug_tracker_uri: https://github.com/rails/rails/issues
106
- changelog_uri: https://github.com/rails/rails/blob/v8.0.2.1/activejob/CHANGELOG.md
107
- documentation_uri: https://api.rubyonrails.org/v8.0.2.1/
110
+ changelog_uri: https://github.com/rails/rails/blob/v8.1.0.beta1/activejob/CHANGELOG.md
111
+ documentation_uri: https://api.rubyonrails.org/v8.1.0.beta1/
108
112
  mailing_list_uri: https://discuss.rubyonrails.org/c/rubyonrails-talk
109
- source_code_uri: https://github.com/rails/rails/tree/v8.0.2.1/activejob
113
+ source_code_uri: https://github.com/rails/rails/tree/v8.1.0.beta1/activejob
110
114
  rubygems_mfa_required: 'true'
111
115
  rdoc_options: []
112
116
  require_paths:
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "sucker_punch"
4
-
5
- module ActiveJob
6
- module QueueAdapters
7
- # = Sucker Punch adapter for Active Job
8
- #
9
- # Sucker Punch is a single-process Ruby asynchronous processing library.
10
- # This reduces the cost of hosting on a service like Heroku along
11
- # with the memory footprint of having to maintain additional jobs if
12
- # hosting on a dedicated server. All queues can run within a
13
- # single application (e.g. \Rails, Sinatra, etc.) process.
14
- #
15
- # Read more about Sucker Punch {here}[https://github.com/brandonhilkert/sucker_punch].
16
- #
17
- # To use Sucker Punch set the queue_adapter config to +:sucker_punch+.
18
- #
19
- # Rails.application.config.active_job.queue_adapter = :sucker_punch
20
- class SuckerPunchAdapter < AbstractAdapter
21
- def check_adapter
22
- ActiveJob.deprecator.warn <<~MSG.squish
23
- The `sucker_punch` adapter is deprecated and will be removed in Rails 8.1.
24
- Please use the `async` adapter instead.
25
- MSG
26
- end
27
-
28
- def enqueue(job) # :nodoc:
29
- if JobWrapper.respond_to?(:perform_async)
30
- # sucker_punch 2.0 API
31
- JobWrapper.perform_async job.serialize
32
- else
33
- # sucker_punch 1.0 API
34
- JobWrapper.new.async.perform job.serialize
35
- end
36
- end
37
-
38
- def enqueue_at(job, timestamp) # :nodoc:
39
- if JobWrapper.respond_to?(:perform_in)
40
- delay = timestamp - Time.current.to_f
41
- JobWrapper.perform_in delay, job.serialize
42
- else
43
- raise NotImplementedError, "sucker_punch 1.0 does not support `enqueue_at`. Please upgrade to version ~> 2.0.0 to enable this behavior."
44
- end
45
- end
46
-
47
- class JobWrapper # :nodoc:
48
- include SuckerPunch::Job
49
-
50
- def perform(job_data)
51
- Base.execute job_data
52
- end
53
- end
54
- end
55
- end
56
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveJob
4
- module Timezones # :nodoc:
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- around_perform do |job, block|
9
- Time.use_zone(job.timezone, &block)
10
- end
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveJob
4
- module Translation # :nodoc:
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- around_perform do |job, block|
9
- I18n.with_locale(job.locale, &block)
10
- end
11
- end
12
- end
13
- end