Skip to content

Commit 3110cae

Browse files
Allow passing multiple exceptions to retry_on/discard_on
1 parent 8324791 commit 3110cae

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

activejob/lib/active_job/exceptions.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ module ClassMethods
4242
# # Might raise Net::OpenTimeout when the remote service is down
4343
# end
4444
# end
45-
def retry_on(exception, wait: 3.seconds, attempts: 5, queue: nil, priority: nil)
46-
rescue_from exception do |error|
45+
def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil)
46+
rescue_from(*exceptions) do |error|
4747
if executions < attempts
48-
logger.error "Retrying #{self.class} in #{wait} seconds, due to a #{exception}. The original exception was #{error.cause.inspect}."
48+
logger.error "Retrying #{self.class} in #{wait} seconds, due to a #{error.class}. The original exception was #{error.cause.inspect}."
4949
retry_job wait: determine_delay(wait), queue: queue, priority: priority
5050
else
5151
if block_given?
5252
yield self, error
5353
else
54-
logger.error "Stopped retrying #{self.class} due to a #{exception}, which reoccurred on #{executions} attempts. The original exception was #{error.cause.inspect}."
54+
logger.error "Stopped retrying #{self.class} due to a #{error.class}, which reoccurred on #{executions} attempts. The original exception was #{error.cause.inspect}."
5555
raise error
5656
end
5757
end
@@ -76,12 +76,12 @@ def retry_on(exception, wait: 3.seconds, attempts: 5, queue: nil, priority: nil)
7676
# # Might raise CustomAppException for something domain specific
7777
# end
7878
# end
79-
def discard_on(exception)
80-
rescue_from exception do |error|
79+
def discard_on(*exceptions)
80+
rescue_from(*exceptions) do |error|
8181
if block_given?
8282
yield self, error
8383
else
84-
logger.error "Discarded #{self.class} due to a #{exception}. The original exception was #{error.cause.inspect}."
84+
logger.error "Discarded #{self.class} due to a #{error.class}. The original exception was #{error.cause.inspect}."
8585
end
8686
end
8787
end

activejob/test/cases/exceptions_test.rb

+18
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,22 @@ class ExceptionsTest < ActiveJob::TestCase
113113
end
114114
end
115115
end
116+
117+
test "successfully retry job throwing one of two retryable exceptions" do
118+
perform_enqueued_jobs do
119+
RetryJob.perform_later "SecondRetryableErrorOfTwo", 3
120+
121+
assert_equal [
122+
"Raised SecondRetryableErrorOfTwo for the 1st time",
123+
"Raised SecondRetryableErrorOfTwo for the 2nd time",
124+
"Successfully completed job" ], JobBuffer.values
125+
end
126+
end
127+
128+
test "discard job throwing one of two discardable exceptions" do
129+
perform_enqueued_jobs do
130+
RetryJob.perform_later "SecondDiscardableErrorOfTwo", 2
131+
assert_equal [ "Raised SecondDiscardableErrorOfTwo for the 1st time" ], JobBuffer.values
132+
end
133+
end
116134
end

activejob/test/jobs/retry_job.rb

+7
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@
44
require "active_support/core_ext/integer/inflections"
55

66
class DefaultsError < StandardError; end
7+
class FirstRetryableErrorOfTwo < StandardError; end
8+
class SecondRetryableErrorOfTwo < StandardError; end
79
class LongWaitError < StandardError; end
810
class ShortWaitTenAttemptsError < StandardError; end
911
class ExponentialWaitTenAttemptsError < StandardError; end
1012
class CustomWaitTenAttemptsError < StandardError; end
1113
class CustomCatchError < StandardError; end
1214
class DiscardableError < StandardError; end
15+
class FirstDiscardableErrorOfTwo < StandardError; end
16+
class SecondDiscardableErrorOfTwo < StandardError; end
1317
class CustomDiscardableError < StandardError; end
1418

1519
class RetryJob < ActiveJob::Base
1620
retry_on DefaultsError
21+
retry_on FirstRetryableErrorOfTwo, SecondRetryableErrorOfTwo
1722
retry_on LongWaitError, wait: 1.hour, attempts: 10
1823
retry_on ShortWaitTenAttemptsError, wait: 1.second, attempts: 10
1924
retry_on ExponentialWaitTenAttemptsError, wait: :exponentially_longer, attempts: 10
2025
retry_on CustomWaitTenAttemptsError, wait: ->(executions) { executions * 2 }, attempts: 10
2126
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}") }
27+
2228
discard_on DiscardableError
29+
discard_on FirstDiscardableErrorOfTwo, SecondDiscardableErrorOfTwo
2330
discard_on(CustomDiscardableError) { |job, error| JobBuffer.add("Dealt with a job that was discarded in a custom way. Message: #{error.message}") }
2431

2532
def perform(raising, attempts)

0 commit comments

Comments
 (0)