diff options
Diffstat (limited to 'activejob/lib/active_job')
-rw-r--r-- | activejob/lib/active_job/arguments.rb | 6 | ||||
-rw-r--r-- | activejob/lib/active_job/core.rb | 40 | ||||
-rw-r--r-- | activejob/lib/active_job/queue_adapter.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/queue_adapters.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/queue_adapters/async_adapter.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/test_helper.rb | 78 |
6 files changed, 88 insertions, 42 deletions
diff --git a/activejob/lib/active_job/arguments.rb b/activejob/lib/active_job/arguments.rb index b344c44aef..8dcf588f35 100644 --- a/activejob/lib/active_job/arguments.rb +++ b/activejob/lib/active_job/arguments.rb @@ -47,11 +47,11 @@ module ActiveJob private # :nodoc: - GLOBALID_KEY = "_aj_globalid".freeze + GLOBALID_KEY = "_aj_globalid" # :nodoc: - SYMBOL_KEYS_KEY = "_aj_symbol_keys".freeze + SYMBOL_KEYS_KEY = "_aj_symbol_keys" # :nodoc: - WITH_INDIFFERENT_ACCESS_KEY = "_aj_hash_with_indifferent_access".freeze + WITH_INDIFFERENT_ACCESS_KEY = "_aj_hash_with_indifferent_access" # :nodoc: OBJECT_SERIALIZER_KEY = "_aj_serialized" diff --git a/activejob/lib/active_job/core.rb b/activejob/lib/active_job/core.rb index 61d402cfca..62bb5861bb 100644 --- a/activejob/lib/active_job/core.rb +++ b/activejob/lib/active_job/core.rb @@ -6,35 +6,33 @@ module ActiveJob module Core extend ActiveSupport::Concern - included do - # Job arguments - attr_accessor :arguments - attr_writer :serialized_arguments + # Job arguments + attr_accessor :arguments + attr_writer :serialized_arguments - # Timestamp when the job should be performed - attr_accessor :scheduled_at + # Timestamp when the job should be performed + attr_accessor :scheduled_at - # Job Identifier - attr_accessor :job_id + # Job Identifier + attr_accessor :job_id - # Queue in which the job will reside. - attr_writer :queue_name + # Queue in which the job will reside. + attr_writer :queue_name - # Priority that the job will have (lower is more priority). - attr_writer :priority + # Priority that the job will have (lower is more priority). + attr_writer :priority - # ID optionally provided by adapter - attr_accessor :provider_job_id + # ID optionally provided by adapter + attr_accessor :provider_job_id - # Number of times this job has been executed (which increments on every retry, like after an exception). - attr_accessor :executions + # Number of times this job has been executed (which increments on every retry, like after an exception). + attr_accessor :executions - # I18n.locale to be used during the job. - attr_accessor :locale + # I18n.locale to be used during the job. + attr_accessor :locale - # Timezone to be used during the job. - attr_accessor :timezone - end + # Timezone to be used during the job. + attr_accessor :timezone # These methods will be included into any Active Job object, adding # helpers for de/serialization and creation of job instances. diff --git a/activejob/lib/active_job/queue_adapter.rb b/activejob/lib/active_job/queue_adapter.rb index 006a683b85..954bfd1dd1 100644 --- a/activejob/lib/active_job/queue_adapter.rb +++ b/activejob/lib/active_job/queue_adapter.rb @@ -22,6 +22,8 @@ module ActiveJob _queue_adapter end + # Returns string denoting the name of the configured queue adapter. + # By default returns +"async"+. def queue_adapter_name _queue_adapter_name end diff --git a/activejob/lib/active_job/queue_adapters.rb b/activejob/lib/active_job/queue_adapters.rb index 00c7b407b1..3e3a474fbb 100644 --- a/activejob/lib/active_job/queue_adapters.rb +++ b/activejob/lib/active_job/queue_adapters.rb @@ -121,7 +121,7 @@ module ActiveJob autoload :SuckerPunchAdapter autoload :TestAdapter - ADAPTER = "Adapter".freeze + ADAPTER = "Adapter" private_constant :ADAPTER class << self diff --git a/activejob/lib/active_job/queue_adapters/async_adapter.rb b/activejob/lib/active_job/queue_adapters/async_adapter.rb index ebf6f384e3..53a7e3d53e 100644 --- a/activejob/lib/active_job/queue_adapters/async_adapter.rb +++ b/activejob/lib/active_job/queue_adapters/async_adapter.rb @@ -31,7 +31,7 @@ module ActiveJob # jobs. Since jobs share a single thread pool, long-running jobs will block # short-lived jobs. Fine for dev/test; bad for production. class AsyncAdapter - # See {Concurrent::ThreadPoolExecutor}[https://ruby-concurrency.github.io/concurrent-ruby/Concurrent/ThreadPoolExecutor.html] for executor options. + # See {Concurrent::ThreadPoolExecutor}[https://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/ThreadPoolExecutor.html] for executor options. def initialize(**executor_options) @scheduler = Scheduler.new(**executor_options) end diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb index 9efc8c0c12..0deb68d0d2 100644 --- a/activejob/lib/active_job/test_helper.rb +++ b/activejob/lib/active_job/test_helper.rb @@ -75,7 +75,7 @@ module ActiveJob # assert_enqueued_jobs 2 # end # - # If a block is passed, that block will cause the specified number of + # If a block is passed, asserts that the block will cause the specified number of # jobs to be enqueued. # # def test_jobs_again @@ -89,7 +89,7 @@ module ActiveJob # end # end # - # The number of times a specific job was enqueued can be asserted. + # Asserts the number of times a specific job was enqueued by passing +:only+ option. # # def test_logging_job # assert_enqueued_jobs 1, only: LoggingJob do @@ -98,7 +98,7 @@ module ActiveJob # end # end # - # The number of times a job except specific class was enqueued can be asserted. + # Asserts the number of times a job except specific class was enqueued by passing +:except+ option. # # def test_logging_job # assert_enqueued_jobs 1, except: HelloJob do @@ -107,7 +107,7 @@ module ActiveJob # end # end # - # The number of times a job is enqueued to a specific queue can also be asserted. + # Asserts the number of times a job is enqueued to a specific queue by passing +:queue+ option. # # def test_logging_job # assert_enqueued_jobs 2, queue: 'default' do @@ -139,7 +139,7 @@ module ActiveJob # assert_enqueued_jobs 1 # end # - # If a block is passed, that block should not cause any job to be enqueued. + # If a block is passed, asserts that the block will not cause any job to be enqueued. # # def test_jobs_again # assert_no_enqueued_jobs do @@ -147,7 +147,7 @@ module ActiveJob # end # end # - # It can be asserted that no jobs of a specific kind are enqueued: + # Asserts that no jobs of a specific kind are enqueued by passing +:only+ option. # # def test_no_logging # assert_no_enqueued_jobs only: LoggingJob do @@ -155,7 +155,7 @@ module ActiveJob # end # end # - # It can be asserted that no jobs except specific class are enqueued: + # Asserts that no jobs except specific class are enqueued by passing +:except+ option. # # def test_no_logging # assert_no_enqueued_jobs except: HelloJob do @@ -163,7 +163,7 @@ module ActiveJob # end # end # - # It can be asserted that no jobs are enqueued to a specific queue: + # Asserts that no jobs are enqueued to a specific queue by passing +:queue+ option # # def test_no_logging # assert_no_enqueued_jobs queue: 'default' do @@ -197,7 +197,7 @@ module ActiveJob # assert_performed_jobs 2 # end # - # If a block is passed, that block should cause the specified number of + # If a block is passed, asserts that the block will cause the specified number of # jobs to be performed. # # def test_jobs_again @@ -279,7 +279,7 @@ module ActiveJob # end # end # - # If a block is passed, that block should not cause any job to be performed. + # If a block is passed, asserts that the block will not cause any job to be performed. # # def test_jobs_again # assert_no_performed_jobs do @@ -331,7 +331,23 @@ module ActiveJob # assert_enqueued_with(job: MyJob, at: Date.tomorrow.noon) # end # - # If a block is passed, that block should cause the job to be + # + # The +args+ argument also accepts a proc which will get passed the actual + # job's arguments. Your proc needs to returns a boolean value determining if + # the job's arguments matches your expectation. This is useful to check only + # for a subset of arguments. + # + # def test_assert_enqueued_with + # expected_args = ->(job_args) do + # assert job_args.first.key?(:foo) + # end + # + # MyJob.perform_later(foo: 'bar', other_arg: 'No need to check in the test') + # assert_enqueued_with(job: MyJob, args: expected_args, queue: 'low') + # end + # + # + # If a block is passed, asserts that the block will cause the job to be # enqueued with the given arguments. # # def test_assert_enqueued_with @@ -359,7 +375,14 @@ module ActiveJob matching_job = jobs.find do |enqueued_job| deserialized_job = deserialize_args_for_assertion(enqueued_job) - expected_args.all? { |key, value| value == deserialized_job[key] } + + expected_args.all? do |key, value| + if value.respond_to?(:call) + value.call(deserialized_job[key]) + else + value == deserialized_job[key] + end + end end assert matching_job, "No enqueued job found with #{expected}" @@ -382,6 +405,22 @@ module ActiveJob # assert_performed_with(job: MyJob, at: Date.tomorrow.noon) # end # + # The +args+ argument also accepts a proc which will get passed the actual + # job's arguments. Your proc needs to returns a boolean value determining if + # the job's arguments matches your expectation. This is useful to check only + # for a subset of arguments. + # + # def test_assert_performed_with + # expected_args = ->(job_args) do + # assert job_args.first.key?(:foo) + # end + # MyJob.perform_later(foo: 'bar', other_arg: 'No need to check in the test') + # + # perform_enqueued_jobs + # + # assert_performed_with(job: MyJob, args: expected_args, queue: 'high') + # end + # # If a block is passed, that block performs all of the jobs that were # enqueued throughout the duration of the block and asserts that # the job has been performed with the given arguments in the block. @@ -411,7 +450,14 @@ module ActiveJob matching_job = jobs.find do |enqueued_job| deserialized_job = deserialize_args_for_assertion(enqueued_job) - expected_args.all? { |key, value| value == deserialized_job[key] } + + expected_args.all? do |key, value| + if value.respond_to?(:call) + value.call(deserialized_job[key]) + else + value == deserialized_job[key] + end + end end assert matching_job, "No performed job found with #{expected}" @@ -548,8 +594,7 @@ module ActiveJob def flush_enqueued_jobs(only: nil, except: nil, queue: nil) enqueued_jobs_with(only: only, except: except, queue: queue) do |payload| - args = ActiveJob::Arguments.deserialize(payload[:args]) - instantiate_job(payload.merge(args: args)).perform_now + instantiate_job(payload).perform_now queue_adapter.performed_jobs << payload end end @@ -567,7 +612,8 @@ module ActiveJob end def instantiate_job(payload) - job = payload[:job].new(*payload[:args]) + args = ActiveJob::Arguments.deserialize(payload[:args]) + job = payload[:job].new(*args) job.scheduled_at = Time.at(payload[:at]) if payload.key?(:at) job.queue_name = payload[:queue] job |