diff options
Diffstat (limited to 'activejob/lib/active_job')
-rw-r--r-- | activejob/lib/active_job/arguments.rb | 18 | ||||
-rw-r--r-- | activejob/lib/active_job/callbacks.rb | 4 | ||||
-rw-r--r-- | activejob/lib/active_job/configured_job.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/core.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/enqueuing.rb | 6 | ||||
-rw-r--r-- | activejob/lib/active_job/exceptions.rb | 4 | ||||
-rw-r--r-- | activejob/lib/active_job/gem_version.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/logging.rb | 15 | ||||
-rw-r--r-- | activejob/lib/active_job/queue_adapter.rb | 31 | ||||
-rw-r--r-- | activejob/lib/active_job/queue_adapters/qu_adapter.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/queue_adapters/test_adapter.rb | 1 | ||||
-rw-r--r-- | activejob/lib/active_job/queue_name.rb | 9 | ||||
-rw-r--r-- | activejob/lib/active_job/queue_priority.rb | 6 | ||||
-rw-r--r-- | activejob/lib/active_job/railtie.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/test_helper.rb | 135 |
15 files changed, 147 insertions, 92 deletions
diff --git a/activejob/lib/active_job/arguments.rb b/activejob/lib/active_job/arguments.rb index 41ce5f863b..523a0e7f33 100644 --- a/activejob/lib/active_job/arguments.rb +++ b/activejob/lib/active_job/arguments.rb @@ -5,22 +5,10 @@ module ActiveJob # # Wraps the original exception raised as +cause+. class DeserializationError < StandardError - def initialize(e = nil) #:nodoc: - if e - ActiveSupport::Deprecation.warn("Passing #original_exception is deprecated and has no effect. " \ - "Exceptions will automatically capture the original exception.", caller) - end - + def initialize #:nodoc: super("Error while trying to deserialize arguments: #{$!.message}") set_backtrace $!.backtrace end - - # The original exception that was raised during deserialization of job - # arguments. - def original_exception - ActiveSupport::Deprecation.warn("#original_exception is deprecated. Use #cause instead.", caller) - cause - end end # Raised when an unsupported argument type is set as a job argument. We @@ -34,8 +22,8 @@ module ActiveJob module Arguments extend self # :nodoc: - # Calls #uniq since Integer, Fixnum, and Bignum are all the same class on Ruby 2.4+ - TYPE_WHITELIST = [ NilClass, String, Integer, Fixnum, Bignum, Float, BigDecimal, TrueClass, FalseClass ].uniq + TYPE_WHITELIST = [ NilClass, String, Integer, Float, BigDecimal, TrueClass, FalseClass ] + TYPE_WHITELIST.push(Fixnum, Bignum) unless 1.class == Integer # Serializes a set of arguments. Whitelisted types are returned # as-is. Arrays/Hashes are serialized element by element. diff --git a/activejob/lib/active_job/callbacks.rb b/activejob/lib/active_job/callbacks.rb index d5b17de8b5..9aebc880a5 100644 --- a/activejob/lib/active_job/callbacks.rb +++ b/activejob/lib/active_job/callbacks.rb @@ -4,7 +4,7 @@ module ActiveJob # = Active Job Callbacks # # Active Job provides hooks during the life cycle of a job. Callbacks allow you - # to trigger logic during the life cycle of a job. Available callbacks are: + # to trigger logic during this cycle. Available callbacks are: # # * <tt>before_enqueue</tt> # * <tt>around_enqueue</tt> @@ -13,6 +13,8 @@ module ActiveJob # * <tt>around_perform</tt> # * <tt>after_perform</tt> # + # NOTE: Calling the same callback multiple times will overwrite previous callback definitions. + # module Callbacks extend ActiveSupport::Concern include ActiveSupport::Callbacks diff --git a/activejob/lib/active_job/configured_job.rb b/activejob/lib/active_job/configured_job.rb index 979280b910..2ff31f2dae 100644 --- a/activejob/lib/active_job/configured_job.rb +++ b/activejob/lib/active_job/configured_job.rb @@ -1,6 +1,6 @@ module ActiveJob class ConfiguredJob #:nodoc: - def initialize(job_class, options={}) + def initialize(job_class, options = {}) @options = options @job_class = job_class end diff --git a/activejob/lib/active_job/core.rb b/activejob/lib/active_job/core.rb index a338061766..548ec89ee2 100644 --- a/activejob/lib/active_job/core.rb +++ b/activejob/lib/active_job/core.rb @@ -59,7 +59,7 @@ module ActiveJob # VideoJob.set(queue: :some_queue, wait: 5.minutes).perform_later(Video.last) # VideoJob.set(queue: :some_queue, wait_until: Time.now.tomorrow).perform_later(Video.last) # VideoJob.set(queue: :some_queue, wait: 5.minutes, priority: 10).perform_later(Video.last) - def set(options={}) + def set(options = {}) ConfiguredJob.new(self, options) end end diff --git a/activejob/lib/active_job/enqueuing.rb b/activejob/lib/active_job/enqueuing.rb index 18051a7d65..c73117e7f3 100644 --- a/activejob/lib/active_job/enqueuing.rb +++ b/activejob/lib/active_job/enqueuing.rb @@ -18,8 +18,8 @@ module ActiveJob job_or_instantiate(*args).enqueue end - protected - def job_or_instantiate(*args) + private + def job_or_instantiate(*args) # :doc: args.first.is_a?(self) ? args.first : new(*args) end end @@ -39,7 +39,7 @@ module ActiveJob # my_job_instance.enqueue queue: :important # my_job_instance.enqueue wait_until: Date.tomorrow.midnight # my_job_instance.enqueue priority: 10 - def enqueue(options={}) + def enqueue(options = {}) self.scheduled_at = options[:wait].seconds.from_now.to_f if options[:wait] self.scheduled_at = options[:wait_until].to_f if options[:wait_until] self.queue_name = self.class.queue_name_from_part(options[:queue]) if options[:queue] diff --git a/activejob/lib/active_job/exceptions.rb b/activejob/lib/active_job/exceptions.rb index d236f03d53..c1b5d35313 100644 --- a/activejob/lib/active_job/exceptions.rb +++ b/activejob/lib/active_job/exceptions.rb @@ -17,7 +17,7 @@ module ActiveJob # ==== Options # * <tt>:wait</tt> - Re-enqueues the job with a delay specified either in seconds (default: 3 seconds), # as a computing proc that the number of executions so far as an argument, or as a symbol reference of - # <tt>:exponentially_longer<>, which applies the wait algorithm of <tt>(executions ** 4) + 2</tt> + # <tt>:exponentially_longer</tt>, which applies the wait algorithm of <tt>(executions ** 4) + 2</tt> # (first wait 3s, then 18s, then 83s, etc) # * <tt>:attempts</tt> - Re-enqueues the job the specified number of times (default: 5 attempts) # * <tt>:queue</tt> - Re-enqueues the job on a different queue @@ -104,7 +104,7 @@ module ActiveJob def determine_delay(seconds_or_duration_or_algorithm) case seconds_or_duration_or_algorithm when :exponentially_longer - (executions ** 4) + 2 + (executions**4) + 2 when ActiveSupport::Duration duration = seconds_or_duration_or_algorithm duration.to_i diff --git a/activejob/lib/active_job/gem_version.rb b/activejob/lib/active_job/gem_version.rb index 0d50c27938..bf81f37e81 100644 --- a/activejob/lib/active_job/gem_version.rb +++ b/activejob/lib/active_job/gem_version.rb @@ -6,7 +6,7 @@ module ActiveJob module VERSION MAJOR = 5 - MINOR = 1 + MINOR = 2 TINY = 0 PRE = "alpha" diff --git a/activejob/lib/active_job/logging.rb b/activejob/lib/active_job/logging.rb index aa97ab2e22..f46d5c68a8 100644 --- a/activejob/lib/active_job/logging.rb +++ b/activejob/lib/active_job/logging.rb @@ -69,14 +69,21 @@ module ActiveJob def perform_start(event) info do job = event.payload[:job] - "Performing #{job.class.name} from #{queue_name(event)}" + args_info(job) + "Performing #{job.class.name} (Job ID: #{job.job_id}) from #{queue_name(event)}" + args_info(job) end end def perform(event) - info do - job = event.payload[:job] - "Performed #{job.class.name} from #{queue_name(event)} in #{event.duration.round(2)}ms" + job = event.payload[:job] + ex = event.payload[:exception_object] + if ex + error do + "Error performing #{job.class.name} (Job ID: #{job.job_id}) from #{queue_name(event)} in #{event.duration.round(2)}ms: #{ex.class} (#{ex.message}):\n" + Array(ex.backtrace).join("\n") + end + else + info do + "Performed #{job.class.name} (Job ID: #{job.job_id}) from #{queue_name(event)} in #{event.duration.round(2)}ms" + end end end diff --git a/activejob/lib/active_job/queue_adapter.rb b/activejob/lib/active_job/queue_adapter.rb index 7f9a2da4b0..b22d8b8347 100644 --- a/activejob/lib/active_job/queue_adapter.rb +++ b/activejob/lib/active_job/queue_adapter.rb @@ -1,5 +1,3 @@ -require "active_job/queue_adapters/inline_adapter" -require "active_support/core_ext/class/attribute" require "active_support/core_ext/string/inflections" module ActiveJob @@ -9,6 +7,7 @@ module ActiveJob extend ActiveSupport::Concern included do + class_attribute :_queue_adapter_name, instance_accessor: false, instance_predicate: false class_attribute :_queue_adapter, instance_accessor: false, instance_predicate: false self.queue_adapter = :async end @@ -21,11 +20,15 @@ module ActiveJob _queue_adapter end + def queue_adapter_name + _queue_adapter_name + end + # Specify the backend queue provider. The default queue adapter # is the +:async+ queue. See QueueAdapters for more # information. def queue_adapter=(name_or_adapter_or_class) - self._queue_adapter = interpret_adapter(name_or_adapter_or_class) + interpret_adapter(name_or_adapter_or_class) end private @@ -33,31 +36,29 @@ module ActiveJob def interpret_adapter(name_or_adapter_or_class) case name_or_adapter_or_class when Symbol, String - ActiveJob::QueueAdapters.lookup(name_or_adapter_or_class).new + assign_adapter(name_or_adapter_or_class.to_s, + ActiveJob::QueueAdapters.lookup(name_or_adapter_or_class).new) else if queue_adapter?(name_or_adapter_or_class) - name_or_adapter_or_class - elsif queue_adapter_class?(name_or_adapter_or_class) - ActiveSupport::Deprecation.warn "Passing an adapter class is deprecated " \ - "and will be removed in Rails 5.1. Please pass an adapter name " \ - "(.queue_adapter = :#{name_or_adapter_or_class.name.demodulize.remove('Adapter').underscore}) " \ - "or an instance (.queue_adapter = #{name_or_adapter_or_class.name}.new) instead." - name_or_adapter_or_class.new + adapter_name = "#{name_or_adapter_or_class.class.name.demodulize.remove('Adapter').underscore}" + assign_adapter(adapter_name, + name_or_adapter_or_class) else raise ArgumentError end end end + def assign_adapter(adapter_name, queue_adapter) + self._queue_adapter_name = adapter_name + self._queue_adapter = queue_adapter + end + QUEUE_ADAPTER_METHODS = [:enqueue, :enqueue_at].freeze def queue_adapter?(object) QUEUE_ADAPTER_METHODS.all? { |meth| object.respond_to?(meth) } end - - def queue_adapter_class?(object) - object.is_a?(Class) && QUEUE_ADAPTER_METHODS.all? { |meth| object.public_method_defined?(meth) } - end end end end diff --git a/activejob/lib/active_job/queue_adapters/qu_adapter.rb b/activejob/lib/active_job/queue_adapters/qu_adapter.rb index 20cc97ebc7..e8994533e4 100644 --- a/activejob/lib/active_job/queue_adapters/qu_adapter.rb +++ b/activejob/lib/active_job/queue_adapters/qu_adapter.rb @@ -32,7 +32,7 @@ module ActiveJob class JobWrapper < Qu::Job #:nodoc: def initialize(job_data) - @job_data = job_data + @job_data = job_data end def perform diff --git a/activejob/lib/active_job/queue_adapters/test_adapter.rb b/activejob/lib/active_job/queue_adapters/test_adapter.rb index da042cfebf..1b633b210e 100644 --- a/activejob/lib/active_job/queue_adapters/test_adapter.rb +++ b/activejob/lib/active_job/queue_adapters/test_adapter.rb @@ -38,7 +38,6 @@ module ActiveJob end private - def job_to_hash(job, extras = {}) { job: job.class, args: job.serialize.fetch("arguments"), queue: job.queue_name }.merge!(extras) end diff --git a/activejob/lib/active_job/queue_name.rb b/activejob/lib/active_job/queue_name.rb index 143fac9888..d83113af60 100644 --- a/activejob/lib/active_job/queue_name.rb +++ b/activejob/lib/active_job/queue_name.rb @@ -16,7 +16,7 @@ module ActiveJob # post.to_feed! # end # end - def queue_as(part_name=nil, &block) + def queue_as(part_name = nil, &block) if block_given? self.queue_name = block else @@ -32,11 +32,8 @@ module ActiveJob end included do - class_attribute :queue_name, instance_accessor: false - class_attribute :queue_name_delimiter, instance_accessor: false - - self.queue_name = default_queue_name - self.queue_name_delimiter = "_" # set default delimiter to '_' + class_attribute :queue_name, instance_accessor: false, default: default_queue_name + class_attribute :queue_name_delimiter, instance_accessor: false, default: "_" end # Returns the name of the queue the job will be run on. diff --git a/activejob/lib/active_job/queue_priority.rb b/activejob/lib/active_job/queue_priority.rb index a48e53b0ef..db8d9178a4 100644 --- a/activejob/lib/active_job/queue_priority.rb +++ b/activejob/lib/active_job/queue_priority.rb @@ -17,7 +17,7 @@ module ActiveJob # end # # Specify either an argument or a block. - def queue_with_priority(priority=nil, &block) + def queue_with_priority(priority = nil, &block) if block_given? self.priority = block else @@ -27,9 +27,7 @@ module ActiveJob end included do - class_attribute :priority, instance_accessor: false - - self.priority = default_priority + class_attribute :priority, instance_accessor: false, default: default_priority end # Returns the priority that the job will be created with diff --git a/activejob/lib/active_job/railtie.rb b/activejob/lib/active_job/railtie.rb index e4198a40a5..4a8bf04d70 100644 --- a/activejob/lib/active_job/railtie.rb +++ b/activejob/lib/active_job/railtie.rb @@ -15,7 +15,7 @@ module ActiveJob options.queue_adapter ||= :async ActiveSupport.on_load(:active_job) do - options.each { |k,v| send("#{k}=", v) } + options.each { |k, v| send("#{k}=", v) } end end diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb index bbd2a0c06c..a61e4f59a5 100644 --- a/activejob/lib/active_job/test_helper.rb +++ b/activejob/lib/active_job/test_helper.rb @@ -8,16 +8,35 @@ module ActiveJob :performed_jobs, :performed_jobs=, to: :queue_adapter + module TestQueueAdapter + extend ActiveSupport::Concern + + included do + class_attribute :_test_adapter, instance_accessor: false, instance_predicate: false + end + + module ClassMethods + def queue_adapter + self._test_adapter.nil? ? super : self._test_adapter + end + + def disable_test_adapter + self._test_adapter = nil + end + + def enable_test_adapter(test_adapter) + self._test_adapter = test_adapter + end + end + end + + ActiveJob::Base.include(TestQueueAdapter) + def before_setup # :nodoc: test_adapter = queue_adapter_for_test - @old_queue_adapters = (ActiveJob::Base.descendants << ActiveJob::Base).select do |klass| - # only override explicitly set adapters, a quirk of `class_attribute` - klass.singleton_class.public_instance_methods(false).include?(:_queue_adapter) - end.map do |klass| - [klass, klass.queue_adapter].tap do - klass.queue_adapter = test_adapter - end + queue_adapter_changed_jobs.each do |klass| + klass.enable_test_adapter(test_adapter) end clear_enqueued_jobs @@ -27,9 +46,8 @@ module ActiveJob def after_teardown # :nodoc: super - @old_queue_adapters.each do |(klass, adapter)| - klass.queue_adapter = adapter - end + + queue_adapter_changed_jobs.each { |klass| klass.disable_test_adapter } end # Specifies the queue adapter to use with all active job test helpers. @@ -55,7 +73,7 @@ module ActiveJob # assert_enqueued_jobs 2 # end # - # If a block is passed, that block should cause the specified number of + # If a block is passed, that block will cause the specified number of # jobs to be enqueued. # # def test_jobs_again @@ -77,14 +95,23 @@ module ActiveJob # HelloJob.perform_later('jeremy') # end # end - def assert_enqueued_jobs(number, only: nil) + # + # The number of times a job is enqueued to a specific queue can also be asserted. + # + # def test_logging_job + # assert_enqueued_jobs 2, queue: 'default' do + # LoggingJob.perform_later + # HelloJob.perform_later('elfassy') + # end + # end + def assert_enqueued_jobs(number, only: nil, queue: nil) if block_given? - original_count = enqueued_jobs_size(only: only) + original_count = enqueued_jobs_size(only: only, queue: queue) yield - new_count = enqueued_jobs_size(only: only) + new_count = enqueued_jobs_size(only: only, queue: queue) assert_equal number, new_count - original_count, "#{number} jobs expected, but #{new_count - original_count} were enqueued" else - actual_count = enqueued_jobs_size(only: only) + actual_count = enqueued_jobs_size(only: only, queue: queue) assert_equal number, actual_count, "#{number} jobs expected, but #{actual_count} were enqueued" end end @@ -232,16 +259,16 @@ module ActiveJob # MyJob.set(wait_until: Date.tomorrow.noon).perform_later # end # end - def assert_enqueued_with(args = {}) + def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil) original_enqueued_jobs_count = enqueued_jobs.count - args.assert_valid_keys(:job, :args, :at, :queue) - serialized_args = serialize_args_for_assertion(args) + expected = { job: job, args: args, at: at, queue: queue }.compact + serialized_args = serialize_args_for_assertion(expected) yield in_block_jobs = enqueued_jobs.drop(original_enqueued_jobs_count) - matching_job = in_block_jobs.find do |job| - serialized_args.all? { |key, value| value == job[key] } + matching_job = in_block_jobs.find do |in_block_job| + serialized_args.all? { |key, value| value == in_block_job[key] } end - assert matching_job, "No enqueued job found with #{args}" + assert matching_job, "No enqueued job found with #{expected}" instantiate_job(matching_job) end @@ -256,19 +283,38 @@ module ActiveJob # MyJob.set(wait_until: Date.tomorrow.noon).perform_later # end # end - def assert_performed_with(args = {}) + def assert_performed_with(job: nil, args: nil, at: nil, queue: nil) original_performed_jobs_count = performed_jobs.count - args.assert_valid_keys(:job, :args, :at, :queue) - serialized_args = serialize_args_for_assertion(args) + expected = { job: job, args: args, at: at, queue: queue }.compact + serialized_args = serialize_args_for_assertion(expected) perform_enqueued_jobs { yield } in_block_jobs = performed_jobs.drop(original_performed_jobs_count) - matching_job = in_block_jobs.find do |job| - serialized_args.all? { |key, value| value == job[key] } + matching_job = in_block_jobs.find do |in_block_job| + serialized_args.all? { |key, value| value == in_block_job[key] } end - assert matching_job, "No performed job found with #{args}" + assert matching_job, "No performed job found with #{expected}" instantiate_job(matching_job) end + # Performs all enqueued jobs in the duration of the block. + # + # def test_perform_enqueued_jobs + # perform_enqueued_jobs do + # MyJob.perform_later(1, 2, 3) + # end + # assert_performed_jobs 1 + # end + # + # This method also supports filtering. If the +:only+ option is specified, + # then only the listed job(s) will be performed. + # + # def test_perform_enqueued_jobs_with_only + # perform_enqueued_jobs(only: MyJob) do + # MyJob.perform_later(1, 2, 3) # will be performed + # HelloJob.perform_later(1, 2, 3) # will not be performed + # end + # assert_performed_jobs 1 + # end def perform_enqueued_jobs(only: nil) old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs old_perform_enqueued_at_jobs = queue_adapter.perform_enqueued_at_jobs @@ -286,39 +332,56 @@ module ActiveJob end end + # Accesses the queue_adapter set by ActiveJob::Base. + # + # def test_assert_job_has_custom_queue_adapter_set + # assert_instance_of CustomQueueAdapter, HelloJob.queue_adapter + # end def queue_adapter ActiveJob::Base.queue_adapter end private - def clear_enqueued_jobs # :nodoc: + def clear_enqueued_jobs enqueued_jobs.clear end - def clear_performed_jobs # :nodoc: + def clear_performed_jobs performed_jobs.clear end - def enqueued_jobs_size(only: nil) # :nodoc: - if only - enqueued_jobs.count { |job| Array(only).include?(job.fetch(:job)) } - else - enqueued_jobs.count + def enqueued_jobs_size(only: nil, queue: nil) + enqueued_jobs.count do |job| + job_class = job.fetch(:job) + if only + next false unless Array(only).include?(job_class) + end + if queue + next false unless queue.to_s == job.fetch(:queue, job_class.queue_name) + end + true end end - def serialize_args_for_assertion(args) # :nodoc: + def serialize_args_for_assertion(args) args.dup.tap do |serialized_args| serialized_args[:args] = ActiveJob::Arguments.serialize(serialized_args[:args]) if serialized_args[:args] serialized_args[:at] = serialized_args[:at].to_f if serialized_args[:at] end end - def instantiate_job(payload) # :nodoc: + def instantiate_job(payload) job = payload[:job].new(*payload[:args]) job.scheduled_at = Time.at(payload[:at]) if payload.key?(:at) job.queue_name = payload[:queue] job end + + def queue_adapter_changed_jobs + (ActiveJob::Base.descendants << ActiveJob::Base).select do |klass| + # only override explicitly set adapters, a quirk of `class_attribute` + klass.singleton_class.public_instance_methods(false).include?(:_queue_adapter) + end + end end end |