aboutsummaryrefslogtreecommitdiffstats
path: root/activejob
diff options
context:
space:
mode:
Diffstat (limited to 'activejob')
-rw-r--r--activejob/CHANGELOG.md38
-rw-r--r--activejob/Rakefile4
-rw-r--r--activejob/lib/active_job/core.rb20
-rw-r--r--activejob/lib/active_job/exceptions.rb24
-rw-r--r--activejob/lib/active_job/queue_adapters.rb2
-rw-r--r--activejob/lib/active_job/test_helper.rb44
-rw-r--r--activejob/test/cases/exceptions_test.rb20
-rw-r--r--activejob/test/cases/job_serialization_test.rb12
-rw-r--r--activejob/test/cases/test_helper_test.rb160
-rw-r--r--activejob/test/jobs/retry_job.rb11
-rw-r--r--activejob/test/support/queue_classic/inline.rb7
-rw-r--r--activejob/test/support/sneakers/inline.rb3
12 files changed, 297 insertions, 48 deletions
diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md
index a3d13ad162..3ecbc2ff38 100644
--- a/activejob/CHANGELOG.md
+++ b/activejob/CHANGELOG.md
@@ -1,3 +1,41 @@
+* Allow `queue` option to `assert_no_enqueued_jobs`.
+
+ Example:
+ ```
+ def test_no_logging
+ assert_no_enqueued_jobs queue: 'default' do
+ LoggingJob.set(queue: :some_queue).perform_later
+ end
+ end
+ ```
+
+ *bogdanvlviv*
+
+* Allow call `assert_enqueued_with` with no block.
+
+ Example:
+ ```
+ def test_assert_enqueued_with
+ MyJob.perform_later(1,2,3)
+ assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low')
+
+ MyJob.set(wait_until: Date.tomorrow.noon).perform_later
+ assert_enqueued_with(job: MyJob, at: Date.tomorrow.noon)
+ end
+ ```
+
+ *bogdanvlviv*
+
+* Allow passing multiple exceptions to `retry_on`, and `discard_on`.
+
+ *George Claghorn*
+
+* Pass the error instance as the second parameter of block executed by `discard_on`.
+
+ Fixes #32853.
+
+ *Yuji Yaginuma*
+
* Remove support for Qu gem.
Reasons are that the Qu gem wasn't compatible since Rails 5.1,
diff --git a/activejob/Rakefile b/activejob/Rakefile
index b4da75adab..0f88b22e8d 100644
--- a/activejob/Rakefile
+++ b/activejob/Rakefile
@@ -38,7 +38,7 @@ namespace :test do
t.libs << "test"
t.test_files = FileList["test/cases/**/*_test.rb"]
t.verbose = true
- t.warning = false
+ t.warning = true
t.ruby_opts = ["--dev"] if defined?(JRUBY_VERSION)
end
@@ -56,7 +56,7 @@ namespace :test do
t.libs << "test"
t.test_files = FileList["test/integration/**/*_test.rb"]
t.verbose = true
- t.warning = false
+ t.warning = true
t.ruby_opts = ["--dev"] if defined?(JRUBY_VERSION)
end
end
diff --git a/activejob/lib/active_job/core.rb b/activejob/lib/active_job/core.rb
index da841ae45b..61d402cfca 100644
--- a/activejob/lib/active_job/core.rb
+++ b/activejob/lib/active_job/core.rb
@@ -88,7 +88,7 @@ module ActiveJob
"provider_job_id" => provider_job_id,
"queue_name" => queue_name,
"priority" => priority,
- "arguments" => serialize_arguments(arguments),
+ "arguments" => serialize_arguments_if_needed(arguments),
"executions" => executions,
"locale" => I18n.locale.to_s,
"timezone" => Time.zone.try(:name)
@@ -133,19 +133,31 @@ module ActiveJob
end
private
+ def serialize_arguments_if_needed(arguments)
+ if arguments_serialized?
+ @serialized_arguments
+ else
+ serialize_arguments(arguments)
+ end
+ end
+
def deserialize_arguments_if_needed
- if defined?(@serialized_arguments) && @serialized_arguments.present?
+ if arguments_serialized?
@arguments = deserialize_arguments(@serialized_arguments)
@serialized_arguments = nil
end
end
- def serialize_arguments(serialized_args)
- Arguments.serialize(serialized_args)
+ def serialize_arguments(arguments)
+ Arguments.serialize(arguments)
end
def deserialize_arguments(serialized_args)
Arguments.deserialize(serialized_args)
end
+
+ def arguments_serialized?
+ defined?(@serialized_arguments) && @serialized_arguments
+ end
end
end
diff --git a/activejob/lib/active_job/exceptions.rb b/activejob/lib/active_job/exceptions.rb
index ae700848d0..1e57dbcb1c 100644
--- a/activejob/lib/active_job/exceptions.rb
+++ b/activejob/lib/active_job/exceptions.rb
@@ -30,8 +30,8 @@ module ActiveJob
# class RemoteServiceJob < ActiveJob::Base
# retry_on CustomAppException # defaults to 3s wait, 5 attempts
# retry_on AnotherCustomAppException, wait: ->(executions) { executions * 2 }
- # retry_on(YetAnotherCustomAppException) do |job, exception|
- # ExceptionNotifier.caught(exception)
+ # retry_on(YetAnotherCustomAppException) do |job, error|
+ # ExceptionNotifier.caught(error)
# end
# retry_on ActiveRecord::Deadlocked, wait: 5.seconds, attempts: 3
# retry_on Net::OpenTimeout, wait: :exponentially_longer, attempts: 10
@@ -42,16 +42,16 @@ module ActiveJob
# # Might raise Net::OpenTimeout when the remote service is down
# end
# end
- def retry_on(exception, wait: 3.seconds, attempts: 5, queue: nil, priority: nil)
- rescue_from exception do |error|
+ def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil)
+ rescue_from(*exceptions) do |error|
if executions < attempts
- logger.error "Retrying #{self.class} in #{wait} seconds, due to a #{exception}. The original exception was #{error.cause.inspect}."
+ logger.error "Retrying #{self.class} in #{wait} seconds, due to a #{error.class}. The original exception was #{error.cause.inspect}."
retry_job wait: determine_delay(wait), queue: queue, priority: priority
else
if block_given?
yield self, error
else
- logger.error "Stopped retrying #{self.class} due to a #{exception}, which reoccurred on #{executions} attempts. The original exception was #{error.cause.inspect}."
+ logger.error "Stopped retrying #{self.class} due to a #{error.class}, which reoccurred on #{executions} attempts. The original exception was #{error.cause.inspect}."
raise error
end
end
@@ -67,8 +67,8 @@ module ActiveJob
#
# class SearchIndexingJob < ActiveJob::Base
# discard_on ActiveJob::DeserializationError
- # discard_on(CustomAppException) do |job, exception|
- # ExceptionNotifier.caught(exception)
+ # discard_on(CustomAppException) do |job, error|
+ # ExceptionNotifier.caught(error)
# end
#
# def perform(record)
@@ -76,12 +76,12 @@ module ActiveJob
# # Might raise CustomAppException for something domain specific
# end
# end
- def discard_on(exception)
- rescue_from exception do |error|
+ def discard_on(*exceptions)
+ rescue_from(*exceptions) do |error|
if block_given?
- yield self, exception
+ yield self, error
else
- logger.error "Discarded #{self.class} due to a #{exception}. The original exception was #{error.cause.inspect}."
+ logger.error "Discarded #{self.class} due to a #{error.class}. The original exception was #{error.cause.inspect}."
end
end
end
diff --git a/activejob/lib/active_job/queue_adapters.rb b/activejob/lib/active_job/queue_adapters.rb
index 7854467cc1..00c7b407b1 100644
--- a/activejob/lib/active_job/queue_adapters.rb
+++ b/activejob/lib/active_job/queue_adapters.rb
@@ -15,7 +15,7 @@ module ActiveJob
# * {Sucker Punch}[https://github.com/brandonhilkert/sucker_punch]
# * {Active Job Async Job}[http://api.rubyonrails.org/classes/ActiveJob/QueueAdapters/AsyncAdapter.html]
# * {Active Job Inline}[http://api.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]
- # * Please Note: We are not accepting pull requests for new adapters. See the README for more details.
+ # * Please Note: We are not accepting pull requests for new adapters. See the {README}[link:files/activejob/README_md.html] for more details.
#
# === Backends Features
#
diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb
index 1cd2c40c15..04cde28a96 100644
--- a/activejob/lib/active_job/test_helper.rb
+++ b/activejob/lib/active_job/test_helper.rb
@@ -159,11 +159,19 @@ module ActiveJob
# end
# end
#
+ # It can be asserted that no jobs are enqueued to a specific queue:
+ #
+ # def test_no_logging
+ # assert_no_enqueued_jobs queue: 'default' do
+ # LoggingJob.set(queue: :some_queue).perform_later
+ # end
+ # end
+ #
# Note: This assertion is simply a shortcut for:
#
# assert_enqueued_jobs 0, &block
- def assert_no_enqueued_jobs(only: nil, except: nil, &block)
- assert_enqueued_jobs 0, only: only, except: except, &block
+ def assert_no_enqueued_jobs(only: nil, except: nil, queue: nil, &block)
+ assert_enqueued_jobs 0, only: only, except: except, queue: queue, &block
end
# Asserts that the number of performed jobs matches the given number.
@@ -286,7 +294,18 @@ module ActiveJob
assert_performed_jobs 0, only: only, except: except, &block
end
- # Asserts that the job passed in the block has been enqueued with the given arguments.
+ # Asserts that the job has been enqueued with the given arguments.
+ #
+ # def test_assert_enqueued_with
+ # MyJob.perform_later(1,2,3)
+ # assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low')
+ #
+ # MyJob.set(wait_until: Date.tomorrow.noon).perform_later
+ # assert_enqueued_with(job: MyJob, at: Date.tomorrow.noon)
+ # end
+ #
+ # If a block is passed, that block should cause the job to be
+ # enqueued with the given arguments.
#
# def test_assert_enqueued_with
# assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low') do
@@ -298,14 +317,23 @@ module ActiveJob
# end
# end
def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
- original_enqueued_jobs_count = enqueued_jobs.count
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 |in_block_job|
- serialized_args.all? { |key, value| value == in_block_job[key] }
+
+ if block_given?
+ original_enqueued_jobs_count = enqueued_jobs.count
+
+ yield
+
+ jobs = enqueued_jobs.drop(original_enqueued_jobs_count)
+ else
+ jobs = enqueued_jobs
end
+
+ matching_job = jobs.find do |enqueued_job|
+ serialized_args.all? { |key, value| value == enqueued_job[key] }
+ end
+
assert matching_job, "No enqueued job found with #{expected}"
instantiate_job(matching_job)
end
diff --git a/activejob/test/cases/exceptions_test.rb b/activejob/test/cases/exceptions_test.rb
index bc33d79f61..47d4e3c0c2 100644
--- a/activejob/test/cases/exceptions_test.rb
+++ b/activejob/test/cases/exceptions_test.rb
@@ -61,7 +61,7 @@ class ExceptionsTest < ActiveJob::TestCase
test "custom handling of discarded job" do
perform_enqueued_jobs do
RetryJob.perform_later "CustomDiscardableError", 2
- assert_equal "Dealt with a job that was discarded in a custom way", JobBuffer.last_value
+ assert_equal "Dealt with a job that was discarded in a custom way. Message: CustomDiscardableError", JobBuffer.last_value
end
end
@@ -113,4 +113,22 @@ class ExceptionsTest < ActiveJob::TestCase
end
end
end
+
+ test "successfully retry job throwing one of two retryable exceptions" do
+ perform_enqueued_jobs do
+ RetryJob.perform_later "SecondRetryableErrorOfTwo", 3
+
+ assert_equal [
+ "Raised SecondRetryableErrorOfTwo for the 1st time",
+ "Raised SecondRetryableErrorOfTwo for the 2nd time",
+ "Successfully completed job" ], JobBuffer.values
+ end
+ end
+
+ test "discard job throwing one of two discardable exceptions" do
+ perform_enqueued_jobs do
+ RetryJob.perform_later "SecondDiscardableErrorOfTwo", 2
+ assert_equal [ "Raised SecondDiscardableErrorOfTwo for the 1st time" ], JobBuffer.values
+ end
+ end
end
diff --git a/activejob/test/cases/job_serialization_test.rb b/activejob/test/cases/job_serialization_test.rb
index 5c9994508e..86f3651564 100644
--- a/activejob/test/cases/job_serialization_test.rb
+++ b/activejob/test/cases/job_serialization_test.rb
@@ -23,16 +23,16 @@ class JobSerializationTest < ActiveSupport::TestCase
test "serialize and deserialize are symmetric" do
# Round trip a job in memory only
- h1 = HelloJob.new
- h1.deserialize(h1.serialize)
+ h1 = HelloJob.new("Rafael")
+ h2 = HelloJob.deserialize(h1.serialize)
+ assert_equal h1.serialize, h2.serialize
# Now verify it's identical to a JSON round trip.
# We don't want any non-native JSON elements in the job hash,
# like symbols.
- payload = JSON.dump(h1.serialize)
- h2 = HelloJob.new
- h2.deserialize(JSON.load(payload))
- assert_equal h1.serialize, h2.serialize
+ payload = JSON.dump(h2.serialize)
+ h3 = HelloJob.deserialize(JSON.load(payload))
+ assert_equal h2.serialize, h3.serialize
end
test "deserialize sets locale" do
diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb
index 66bcd8f3a0..d0a21a5da3 100644
--- a/activejob/test/cases/test_helper_test.rb
+++ b/activejob/test/cases/test_helper_test.rb
@@ -389,13 +389,91 @@ class EnqueuedJobsTest < ActiveJob::TestCase
assert_match(/`:only` and `:except`/, error.message)
end
- def test_assert_enqueued_job
+ def test_assert_no_enqueued_jobs_with_queue_option
+ assert_nothing_raised do
+ assert_no_enqueued_jobs queue: :default do
+ HelloJob.set(queue: :other_queue).perform_later
+ LoggingJob.set(queue: :other_queue).perform_later
+ end
+ end
+ end
+
+ def test_assert_no_enqueued_jobs_with_queue_option_failure
+ error = assert_raise ActiveSupport::TestCase::Assertion do
+ assert_no_enqueued_jobs queue: :other_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ end
+ end
+
+ assert_match(/0 .* but 1/, error.message)
+ end
+
+ def test_assert_no_enqueued_jobs_with_only_and_queue_option
+ assert_nothing_raised do
+ assert_no_enqueued_jobs only: HelloJob, queue: :some_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ HelloJob.set(queue: :other_queue).perform_later
+ LoggingJob.set(queue: :some_queue).perform_later
+ end
+ end
+ end
+
+ def test_assert_no_enqueued_jobs_with_only_and_queue_option_failure
+ error = assert_raise ActiveSupport::TestCase::Assertion do
+ assert_no_enqueued_jobs only: HelloJob, queue: :some_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ HelloJob.set(queue: :some_queue).perform_later
+ LoggingJob.set(queue: :some_queue).perform_later
+ end
+ end
+
+ assert_match(/0 .* but 1/, error.message)
+ end
+
+ def test_assert_no_enqueued_jobs_with_except_and_queue_option
+ assert_nothing_raised do
+ assert_no_enqueued_jobs except: LoggingJob, queue: :some_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ HelloJob.set(queue: :other_queue).perform_later
+ LoggingJob.set(queue: :some_queue).perform_later
+ end
+ end
+ end
+
+ def test_assert_no_enqueued_jobs_with_except_and_queue_option_failure
+ error = assert_raise ActiveSupport::TestCase::Assertion do
+ assert_no_enqueued_jobs except: LoggingJob, queue: :some_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ HelloJob.set(queue: :some_queue).perform_later
+ LoggingJob.set(queue: :some_queue).perform_later
+ end
+ end
+
+ assert_match(/0 .* but 1/, error.message)
+ end
+
+ def test_assert_no_enqueued_jobs_with_only_and_except_and_queue_option
+ error = assert_raise ArgumentError do
+ assert_no_enqueued_jobs only: HelloJob, except: HelloJob, queue: :some_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ end
+ end
+
+ assert_match(/`:only` and `:except`/, error.message)
+ end
+
+ def test_assert_enqueued_with
assert_enqueued_with(job: LoggingJob, queue: "default") do
LoggingJob.set(wait_until: Date.tomorrow.noon).perform_later
end
end
- def test_assert_enqueued_job_returns
+ def test_assert_enqueued_with_with_no_block
+ LoggingJob.set(wait_until: Date.tomorrow.noon).perform_later
+ assert_enqueued_with(job: LoggingJob, queue: "default")
+ end
+
+ def test_assert_enqueued_with_returns
job = assert_enqueued_with(job: LoggingJob) do
LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3)
end
@@ -406,13 +484,28 @@ class EnqueuedJobsTest < ActiveJob::TestCase
assert_equal [1, 2, 3], job.arguments
end
- def test_assert_enqueued_job_failure
+ def test_assert_enqueued_with_with_no_block_returns
+ LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3)
+ job = assert_enqueued_with(job: LoggingJob)
+
+ assert_instance_of LoggingJob, job
+ assert_in_delta 5.minutes.from_now, job.scheduled_at, 1
+ assert_equal "default", job.queue_name
+ assert_equal [1, 2, 3], job.arguments
+ end
+
+ def test_assert_enqueued_with_failure
assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_with(job: LoggingJob, queue: "default") do
NestedJob.perform_later
end
end
+ assert_raise ActiveSupport::TestCase::Assertion do
+ LoggingJob.perform_later
+ assert_enqueued_with(job: LoggingJob) {}
+ end
+
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_with(job: NestedJob, queue: "low") do
NestedJob.perform_later
@@ -422,7 +515,21 @@ class EnqueuedJobsTest < ActiveJob::TestCase
assert_equal 'No enqueued job found with {:job=>NestedJob, :queue=>"low"}', error.message
end
- def test_assert_enqueued_job_args
+ def test_assert_enqueued_with_with_no_block_failure
+ assert_raise ActiveSupport::TestCase::Assertion do
+ NestedJob.perform_later
+ assert_enqueued_with(job: LoggingJob, queue: "default")
+ end
+
+ error = assert_raise ActiveSupport::TestCase::Assertion do
+ NestedJob.perform_later
+ assert_enqueued_with(job: NestedJob, queue: "low")
+ end
+
+ assert_equal 'No enqueued job found with {:job=>NestedJob, :queue=>"low"}', error.message
+ end
+
+ def test_assert_enqueued_with_args
assert_raise ArgumentError do
assert_enqueued_with(class: LoggingJob) do
NestedJob.set(wait_until: Date.tomorrow.noon).perform_later
@@ -430,20 +537,38 @@ class EnqueuedJobsTest < ActiveJob::TestCase
end
end
- def test_assert_enqueued_job_with_at_option
+ def test_assert_enqueued_with_with_no_block_args
+ assert_raise ArgumentError do
+ NestedJob.set(wait_until: Date.tomorrow.noon).perform_later
+ assert_enqueued_with(class: LoggingJob)
+ end
+ end
+
+ def test_assert_enqueued_with_with_at_option
assert_enqueued_with(job: HelloJob, at: Date.tomorrow.noon) do
HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
end
end
- def test_assert_enqueued_job_with_global_id_args
+ def test_assert_enqueued_with_with_no_block_with_at_option
+ HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
+ assert_enqueued_with(job: HelloJob, at: Date.tomorrow.noon)
+ end
+
+ def test_assert_enqueued_with_with_global_id_args
ricardo = Person.new(9)
assert_enqueued_with(job: HelloJob, args: [ricardo]) do
HelloJob.perform_later(ricardo)
end
end
- def test_assert_enqueued_job_failure_with_global_id_args
+ def test_assert_enqueued_with_with_no_block_with_global_id_args
+ ricardo = Person.new(9)
+ HelloJob.perform_later(ricardo)
+ assert_enqueued_with(job: HelloJob, args: [ricardo])
+ end
+
+ def test_assert_enqueued_with_failure_with_global_id_args
ricardo = Person.new(9)
wilma = Person.new(11)
error = assert_raise ActiveSupport::TestCase::Assertion do
@@ -455,7 +580,18 @@ class EnqueuedJobsTest < ActiveJob::TestCase
assert_equal "No enqueued job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
end
- def test_assert_enqueued_job_does_not_change_jobs_count
+ def test_assert_enqueued_with_failure_with_no_block_with_global_id_args
+ ricardo = Person.new(9)
+ wilma = Person.new(11)
+ error = assert_raise ActiveSupport::TestCase::Assertion do
+ HelloJob.perform_later(ricardo)
+ assert_enqueued_with(job: HelloJob, args: [wilma])
+ end
+
+ assert_equal "No enqueued job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
+ end
+
+ def test_assert_enqueued_with_does_not_change_jobs_count
HelloJob.perform_later
assert_enqueued_with(job: HelloJob) do
HelloJob.perform_later
@@ -463,6 +599,14 @@ class EnqueuedJobsTest < ActiveJob::TestCase
assert_equal 2, queue_adapter.enqueued_jobs.count
end
+
+ def test_assert_enqueued_with_with_no_block_does_not_change_jobs_count
+ HelloJob.perform_later
+ HelloJob.perform_later
+ assert_enqueued_with(job: HelloJob)
+
+ assert_equal 2, queue_adapter.enqueued_jobs.count
+ end
end
class PerformedJobsTest < ActiveJob::TestCase
diff --git a/activejob/test/jobs/retry_job.rb b/activejob/test/jobs/retry_job.rb
index 82b80fe12b..1383fffd7d 100644
--- a/activejob/test/jobs/retry_job.rb
+++ b/activejob/test/jobs/retry_job.rb
@@ -4,23 +4,30 @@ require_relative "../support/job_buffer"
require "active_support/core_ext/integer/inflections"
class DefaultsError < StandardError; end
+class FirstRetryableErrorOfTwo < StandardError; end
+class SecondRetryableErrorOfTwo < StandardError; end
class LongWaitError < StandardError; end
class ShortWaitTenAttemptsError < StandardError; end
class ExponentialWaitTenAttemptsError < StandardError; end
class CustomWaitTenAttemptsError < StandardError; end
class CustomCatchError < StandardError; end
class DiscardableError < StandardError; end
+class FirstDiscardableErrorOfTwo < StandardError; end
+class SecondDiscardableErrorOfTwo < StandardError; end
class CustomDiscardableError < StandardError; end
class RetryJob < ActiveJob::Base
retry_on DefaultsError
+ retry_on FirstRetryableErrorOfTwo, SecondRetryableErrorOfTwo
retry_on LongWaitError, wait: 1.hour, attempts: 10
retry_on ShortWaitTenAttemptsError, wait: 1.second, attempts: 10
retry_on ExponentialWaitTenAttemptsError, wait: :exponentially_longer, attempts: 10
retry_on CustomWaitTenAttemptsError, wait: ->(executions) { executions * 2 }, attempts: 10
- retry_on(CustomCatchError) { |job, exception| JobBuffer.add("Dealt with a job that failed to retry in a custom way after #{job.arguments.second} attempts. Message: #{exception.message}") }
+ retry_on(CustomCatchError) { |job, error| JobBuffer.add("Dealt with a job that failed to retry in a custom way after #{job.arguments.second} attempts. Message: #{error.message}") }
+
discard_on DiscardableError
- discard_on(CustomDiscardableError) { |job, exception| JobBuffer.add("Dealt with a job that was discarded in a custom way") }
+ discard_on FirstDiscardableErrorOfTwo, SecondDiscardableErrorOfTwo
+ discard_on(CustomDiscardableError) { |job, error| JobBuffer.add("Dealt with a job that was discarded in a custom way. Message: #{error.message}") }
def perform(raising, attempts)
if executions < attempts
diff --git a/activejob/test/support/queue_classic/inline.rb b/activejob/test/support/queue_classic/inline.rb
index ca3cd4581b..0695a34c27 100644
--- a/activejob/test/support/queue_classic/inline.rb
+++ b/activejob/test/support/queue_classic/inline.rb
@@ -1,22 +1,23 @@
# frozen_string_literal: true
require "queue_classic"
+require "active_support/core_ext/module/redefine_method"
module QC
class Queue
- def enqueue(method, *args)
+ redefine_method(:enqueue) do |method, *args|
receiver_str, _, message = method.rpartition(".")
receiver = eval(receiver_str)
receiver.send(message, *args)
end
- def enqueue_in(seconds, method, *args)
+ redefine_method(:enqueue_in) do |seconds, method, *args|
receiver_str, _, message = method.rpartition(".")
receiver = eval(receiver_str)
receiver.send(message, *args)
end
- def enqueue_at(not_before, method, *args)
+ redefine_method(:enqueue_at) do |not_before, method, *args|
receiver_str, _, message = method.rpartition(".")
receiver = eval(receiver_str)
receiver.send(message, *args)
diff --git a/activejob/test/support/sneakers/inline.rb b/activejob/test/support/sneakers/inline.rb
index 92b69ee3bc..e772c68c6e 100644
--- a/activejob/test/support/sneakers/inline.rb
+++ b/activejob/test/support/sneakers/inline.rb
@@ -1,11 +1,12 @@
# frozen_string_literal: true
require "sneakers"
+require "active_support/core_ext/module/redefine_method"
module Sneakers
module Worker
module ClassMethods
- def enqueue(msg)
+ redefine_method(:enqueue) do |msg|
worker = new(nil, nil, {})
worker.work(*msg)
end