Skip to content

Commit a1e4c19

Browse files
author
David Heinemeier Hansson
committed
Yield the job instance so you have access to things like job.arguments on the custom logic after retries fail
1 parent e1aabee commit a1e4c19

4 files changed

Lines changed: 10 additions & 6 deletions

File tree

activejob/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Rails 5.1.0.alpha ##
22

3+
* Yield the job instance so you have access to things like `job.arguments` on the custom logic after retries fail.
4+
5+
*DHH*
6+
37
* Added declarative exception handling via `ActiveJob::Base.retry_on` and `ActiveJob::Base.discard_on`.
48

59
Examples:

activejob/lib/active_job/exceptions.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module ClassMethods
1212
# holding queue for inspection.
1313
#
1414
# You can also pass a block that'll be invoked if the retry attempts fail for custom logic rather than letting
15-
# the exception bubble up.
15+
# the exception bubble up. This block is yielded with the job instance as the first and the error instance as the second parameter.
1616
#
1717
# ==== Options
1818
# * <tt>:wait</tt> - Re-enqueues the job with a delay specified either in seconds (default: 3 seconds),
@@ -28,7 +28,7 @@ module ClassMethods
2828
# class RemoteServiceJob < ActiveJob::Base
2929
# retry_on CustomAppException # defaults to 3s wait, 5 attempts
3030
# retry_on AnotherCustomAppException, wait: ->(executions) { executions * 2 }
31-
# retry_on(YetAnotherCustomAppException) do |exception|
31+
# retry_on(YetAnotherCustomAppException) do |job, exception|
3232
# ExceptionNotifier.caught(exception)
3333
# end
3434
# retry_on ActiveRecord::StatementInvalid, wait: 5.seconds, attempts: 3
@@ -47,7 +47,7 @@ def retry_on(exception, wait: 3.seconds, attempts: 5, queue: nil, priority: nil)
4747
retry_job wait: determine_delay(wait), queue: queue, priority: priority
4848
else
4949
if block_given?
50-
yield exception
50+
yield self, exception
5151
else
5252
logger.error "Stopped retrying #{self.class} due to a #{exception}, which reoccurred on #{executions} attempts. The original exception was #{error.cause.inspect}."
5353
raise error

activejob/test/cases/exceptions_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class ExceptionsTest < ActiveJob::TestCase
5858

5959
test "custom handling of job that exceeds retry attempts" do
6060
perform_enqueued_jobs do
61-
RetryJob.perform_later "CustomCatchError", 6
62-
assert_equal "Dealt with a job that failed to retry in a custom way", JobBuffer.last_value
61+
RetryJob.perform_later 'CustomCatchError', 6
62+
assert_equal "Dealt with a job that failed to retry in a custom way after 6 attempts", JobBuffer.last_value
6363
end
6464
end
6565

activejob/test/jobs/retry_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RetryJob < ActiveJob::Base
1515
retry_on ShortWaitTenAttemptsError, wait: 1.second, attempts: 10
1616
retry_on ExponentialWaitTenAttemptsError, wait: :exponentially_longer, attempts: 10
1717
retry_on CustomWaitTenAttemptsError, wait: ->(executions) { executions * 2 }, attempts: 10
18-
retry_on(CustomCatchError) { |exception| JobBuffer.add("Dealt with a job that failed to retry in a custom way") }
18+
retry_on(CustomCatchError) { |job, exception| JobBuffer.add("Dealt with a job that failed to retry in a custom way after #{job.arguments.second} attempts") }
1919
discard_on DiscardableError
2020

2121
def perform(raising, attempts)

0 commit comments

Comments
 (0)