From 9d8d4ee05e08b928dbb25bb14e49ea28f30d14c6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 1 Aug 2016 16:51:11 -0700 Subject: [PATCH] Allow for custom handling of exceptions that persist beyond the retry attempts --- activejob/lib/active_job/exceptions.rb | 18 ++++++++++++++---- activejob/test/cases/exceptions_test.rb | 5 +++++ activejob/test/jobs/retry_job.rb | 2 ++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/activejob/lib/active_job/exceptions.rb b/activejob/lib/active_job/exceptions.rb index d4c973d3c1..33388816e9 100644 --- a/activejob/lib/active_job/exceptions.rb +++ b/activejob/lib/active_job/exceptions.rb @@ -11,6 +11,9 @@ module ClassMethods # bubble up to the underlying queuing system, which may have its own retry mechanism or place it in a # holding queue for inspection. # + # You can also pass a block that'll be invoked if the retry attempts fail for custom logic rather than letting + # the exception bubble up. + # # ==== Options # * :wait - 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 @@ -25,11 +28,14 @@ module ClassMethods # class RemoteServiceJob < ActiveJob::Base # retry_on CustomAppException # defaults to 3s wait, 5 attempts # retry_on AnotherCustomAppException, wait: ->(executions) { executions * 2 } + # retry_on(YetAnotherCustomAppException) do |exception| + # ExceptionNotifier.caught(exception) + # end # retry_on ActiveRecord::StatementInvalid, wait: 5.seconds, attempts: 3 # retry_on Net::OpenTimeout, wait: :exponentially_longer, attempts: 10 # # def perform(*args) - # # Might raise CustomAppException or AnotherCustomAppException for something domain specific + # # Might raise CustomAppException, AnotherCustomAppException, or YetAnotherCustomAppException for something domain specific # # Might raise ActiveRecord::StatementInvalid when a local db deadlock is detected # # Might raise Net::OpenTimeout when the remote service is down # end @@ -39,9 +45,13 @@ def retry_on(exception, wait: 3.seconds, attempts: 5, queue: nil, priority: nil) if executions < attempts logger.error "Retrying #{self.class} in #{wait} seconds, due to a #{exception}. The original exception was #{error.cause.inspect}." retry_job wait: determine_delay(wait), queue: queue, priority: priority - else - logger.error "Stopped retrying #{self.class} due to a #{exception}, which reoccurred on #{executions} attempts. The original exception was #{error.cause.inspect}." - raise error + else + if block_given? + yield exception + else + logger.error "Stopped retrying #{self.class} due to a #{exception}, which reoccurred on #{executions} attempts. The original exception was #{error.cause.inspect}." + raise error + end end end end diff --git a/activejob/test/cases/exceptions_test.rb b/activejob/test/cases/exceptions_test.rb index 0bc1d82623..d2757c9074 100644 --- a/activejob/test/cases/exceptions_test.rb +++ b/activejob/test/cases/exceptions_test.rb @@ -45,6 +45,11 @@ class ExceptionsTest < ActiveSupport::TestCase RetryJob.perform_later 'DiscardableError', 2 assert_equal "Raised DiscardableError for the 1st time", JobBuffer.last_value end + + test "custom handling of job that exceeds retry attempts" do + RetryJob.perform_later 'CustomCatchError', 6 + assert_equal "Dealt with a job that failed to retry in a custom way", JobBuffer.last_value + end end class ExponentiallyBackoffExceptionsTest < ActiveJob::TestCase diff --git a/activejob/test/jobs/retry_job.rb b/activejob/test/jobs/retry_job.rb index 35177c5be5..321294f892 100644 --- a/activejob/test/jobs/retry_job.rb +++ b/activejob/test/jobs/retry_job.rb @@ -5,6 +5,7 @@ class DefaultsError < StandardError; end class ShortWaitTenAttemptsError < StandardError; end class ExponentialWaitTenAttemptsError < StandardError; end class CustomWaitTenAttemptsError < StandardError; end +class CustomCatchError < StandardError; end class DiscardableError < StandardError; end class RetryJob < ActiveJob::Base @@ -12,6 +13,7 @@ class RetryJob < ActiveJob::Base 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) { |exception| JobBuffer.add("Dealt with a job that failed to retry in a custom way") } discard_on DiscardableError def perform(raising, attempts) -- GitLab