diff options
Diffstat (limited to 'activejob')
-rw-r--r-- | activejob/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activejob/lib/active_job/arguments.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/exceptions.rb | 28 | ||||
-rw-r--r-- | activejob/lib/active_job/logging.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/test_helper.rb | 25 | ||||
-rw-r--r-- | activejob/test/cases/logging_test.rb | 5 | ||||
-rw-r--r-- | activejob/test/cases/test_helper_test.rb | 13 | ||||
-rw-r--r-- | activejob/test/jobs/multiple_kwargs_job.rb | 9 | ||||
-rw-r--r-- | activejob/test/support/integration/adapters/que.rb | 4 | ||||
-rw-r--r-- | activejob/test/support/integration/adapters/queue_classic.rb | 4 | ||||
-rw-r--r-- | activejob/test/support/integration/helper.rb | 2 |
11 files changed, 71 insertions, 27 deletions
diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md index c47465cb43..57a62e963d 100644 --- a/activejob/CHANGELOG.md +++ b/activejob/CHANGELOG.md @@ -1,3 +1,7 @@ +* `ActionDispatch::IntegrationTest` includes `ActiveJob::TestHelper` module by default. + + *Ricardo Díaz* + * Added `enqueue_retry.active_job`, `retry_stopped.active_job`, and `discard.active_job` hooks. *steves* diff --git a/activejob/lib/active_job/arguments.rb b/activejob/lib/active_job/arguments.rb index ba7f9456f9..b344c44aef 100644 --- a/activejob/lib/active_job/arguments.rb +++ b/activejob/lib/active_job/arguments.rb @@ -34,7 +34,7 @@ module ActiveJob arguments.map { |argument| serialize_argument(argument) } end - # Deserializes a set of arguments. Instrinsic types that can safely be + # Deserializes a set of arguments. Intrinsic types that can safely be # deserialized without mutation are returned as-is. Arrays/Hashes are # deserialized element by element. All other types are deserialized using # GlobalID. diff --git a/activejob/lib/active_job/exceptions.rb b/activejob/lib/active_job/exceptions.rb index d8384c81b6..bb25afbca6 100644 --- a/activejob/lib/active_job/exceptions.rb +++ b/activejob/lib/active_job/exceptions.rb @@ -46,18 +46,15 @@ module ActiveJob # end def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil) rescue_from(*exceptions) do |error| - payload = { - job: self, - adapter: self.class.queue_adapter, - error: error, - wait: wait - } - if executions < attempts - ActiveSupport::Notifications.instrument("enqueue_retry.active_job", payload) do - retry_job wait: determine_delay(wait), queue: queue, priority: priority - end + retry_job wait: determine_delay(wait), queue: queue, priority: priority, error: error else + payload = { + job: self, + adapter: self.class.queue_adapter, + error: error + } + if block_given? ActiveSupport::Notifications.instrument("retry_stopped.active_job", payload) do yield self, error @@ -127,7 +124,16 @@ module ActiveJob # end # end def retry_job(options = {}) - enqueue options + payload = { + job: self, + adapter: self.class.queue_adapter, + error: options[:error], + wait: options[:wait] + } + + ActiveSupport::Notifications.instrument("enqueue_retry.active_job", payload) do + enqueue options + end end private diff --git a/activejob/lib/active_job/logging.rb b/activejob/lib/active_job/logging.rb index 96a3e6bf48..0abee4ed98 100644 --- a/activejob/lib/active_job/logging.rb +++ b/activejob/lib/active_job/logging.rb @@ -94,7 +94,7 @@ module ActiveJob wait = event.payload[:wait] error do - "Retrying #{job.class} in #{wait} seconds, due to a #{ex.class}. The original exception was #{ex.cause.inspect}." + "Retrying #{job.class} in #{wait.inspect} seconds, due to a #{ex&.class.inspect}. The original exception was #{ex&.cause.inspect}." end end diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb index bb9e3e6ca4..9efc8c0c12 100644 --- a/activejob/lib/active_job/test_helper.rb +++ b/activejob/lib/active_job/test_helper.rb @@ -345,7 +345,7 @@ module ActiveJob # end def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil) expected = { job: job, args: args, at: at, queue: queue }.compact - serialized_args = serialize_args_for_assertion(expected) + expected_args = prepare_args_for_assertion(expected) if block_given? original_enqueued_jobs_count = enqueued_jobs.count @@ -358,7 +358,8 @@ module ActiveJob end matching_job = jobs.find do |enqueued_job| - serialized_args.all? { |key, value| value == enqueued_job[key] } + deserialized_job = deserialize_args_for_assertion(enqueued_job) + expected_args.all? { |key, value| value == deserialized_job[key] } end assert matching_job, "No enqueued job found with #{expected}" @@ -396,7 +397,7 @@ module ActiveJob # end def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, &block) expected = { job: job, args: args, at: at, queue: queue }.compact - serialized_args = serialize_args_for_assertion(expected) + expected_args = prepare_args_for_assertion(expected) if block_given? original_performed_jobs_count = performed_jobs.count @@ -408,8 +409,9 @@ module ActiveJob jobs = performed_jobs end - matching_job = jobs.find do |performed_job| - serialized_args.all? { |key, value| value == performed_job[key] } + matching_job = jobs.find do |enqueued_job| + deserialized_job = deserialize_args_for_assertion(enqueued_job) + expected_args.all? { |key, value| value == deserialized_job[key] } end assert matching_job, "No performed job found with #{expected}" @@ -552,10 +554,15 @@ module ActiveJob end end - 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] + def prepare_args_for_assertion(args) + args.dup.tap do |arguments| + arguments[:at] = arguments[:at].to_f if arguments[:at] + end + end + + def deserialize_args_for_assertion(job) + job.dup.tap do |new_job| + new_job[:args] = ActiveJob::Arguments.deserialize(new_job[:args]) if new_job[:args] end end diff --git a/activejob/test/cases/logging_test.rb b/activejob/test/cases/logging_test.rb index 2e8d2ef7c0..b5bf40c83b 100644 --- a/activejob/test/cases/logging_test.rb +++ b/activejob/test/cases/logging_test.rb @@ -173,6 +173,11 @@ class LoggingTest < ActiveSupport::TestCase end end + def test_enqueue_retry_logging_on_retry_job + perform_enqueued_jobs { RescueJob.perform_later "david" } + assert_match(/Retrying RescueJob in nil seconds, due to a nil\. The original exception was nil\./, @logger.messages) + end + def test_retry_stopped_logging perform_enqueued_jobs do RetryJob.perform_later "CustomCatchError", 6 diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb index 160b876e85..1e3fcf1fc2 100644 --- a/activejob/test/cases/test_helper_test.rb +++ b/activejob/test/cases/test_helper_test.rb @@ -8,6 +8,7 @@ require "jobs/logging_job" require "jobs/nested_job" require "jobs/rescue_job" require "jobs/inherited_job" +require "jobs/multiple_kwargs_job" require "models/person" class EnqueuedJobsTest < ActiveJob::TestCase @@ -555,6 +556,12 @@ class EnqueuedJobsTest < ActiveJob::TestCase assert_enqueued_with(job: HelloJob, at: Date.tomorrow.noon) end + def test_assert_enqueued_with_with_hash_arg + assert_enqueued_with(job: MultipleKwargsJob, args: [{ argument1: 1, argument2: { a: 1, b: 2 } }]) do + MultipleKwargsJob.perform_later(argument2: { b: 2, a: 1 }, argument1: 1) + end + end + def test_assert_enqueued_with_with_global_id_args ricardo = Person.new(9) assert_enqueued_with(job: HelloJob, args: [ricardo]) do @@ -1566,6 +1573,12 @@ class PerformedJobsTest < ActiveJob::TestCase end end + def test_assert_performed_with_with_hash_arg + assert_performed_with(job: MultipleKwargsJob, args: [{ argument1: 1, argument2: { a: 1, b: 2 } }]) do + MultipleKwargsJob.perform_later(argument2: { b: 2, a: 1 }, argument1: 1) + end + end + def test_assert_performed_with_with_global_id_args ricardo = Person.new(9) assert_performed_with(job: HelloJob, args: [ricardo]) do diff --git a/activejob/test/jobs/multiple_kwargs_job.rb b/activejob/test/jobs/multiple_kwargs_job.rb new file mode 100644 index 0000000000..b355c4ce1a --- /dev/null +++ b/activejob/test/jobs/multiple_kwargs_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require_relative "../support/job_buffer" + +class MultipleKwargsJob < ActiveJob::Base + def perform(argument1:, argument2:) + JobBuffer.add("Job with argument1: #{argument1}, argument2: #{argument2}") + end +end diff --git a/activejob/test/support/integration/adapters/que.rb b/activejob/test/support/integration/adapters/que.rb index 2a771b08c7..2e7d327b37 100644 --- a/activejob/test/support/integration/adapters/que.rb +++ b/activejob/test/support/integration/adapters/que.rb @@ -18,8 +18,8 @@ module QueJobsManager user = uri.user || ENV["USER"] pass = uri.password db = uri.path[1..-1] - %x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -c 'drop database if exists "#{db}"' -U #{user} -t template1} - %x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -c 'create database "#{db}"' -U #{user} -t template1} + %x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -X -c 'drop database if exists "#{db}"' -U #{user} -t template1} + %x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -X -c 'create database "#{db}"' -U #{user} -t template1} Que.connection = Sequel.connect(que_url) Que.migrate! diff --git a/activejob/test/support/integration/adapters/queue_classic.rb b/activejob/test/support/integration/adapters/queue_classic.rb index 1b0685a971..dbbdc12b9d 100644 --- a/activejob/test/support/integration/adapters/queue_classic.rb +++ b/activejob/test/support/integration/adapters/queue_classic.rb @@ -17,8 +17,8 @@ module QueueClassicJobsManager user = uri.user || ENV["USER"] pass = uri.password db = uri.path[1..-1] - %x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -c 'drop database if exists "#{db}"' -U #{user} -t template1} - %x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -c 'create database "#{db}"' -U #{user} -t template1} + %x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -X -c 'drop database if exists "#{db}"' -U #{user} -t template1} + %x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -X -c 'create database "#{db}"' -U #{user} -t template1} QC::Setup.create QC.default_conn_adapter.disconnect diff --git a/activejob/test/support/integration/helper.rb b/activejob/test/support/integration/helper.rb index a02d874e2e..c5fa2b136f 100644 --- a/activejob/test/support/integration/helper.rb +++ b/activejob/test/support/integration/helper.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -puts "\n\n*** rake aj:integration:#{ENV['AJ_ADAPTER']} ***\n" +puts "\n\n*** rake test:integration:#{ENV['AJ_ADAPTER']} ***\n" ENV["RAILS_ENV"] = "test" ActiveJob::Base.queue_name_prefix = nil |