Skip to content

Commit

Permalink
Merge pull request #35956 from mrhead/fix-per-exception-retry-counter
Browse files Browse the repository at this point in the history
Use individual execution counters when calculating retry delay
  • Loading branch information
georgeclaghorn committed Apr 14, 2019
2 parents 533dd8a + 3a7fa10 commit 263bf76
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions activejob/CHANGELOG.md
@@ -1,3 +1,7 @@
* Use individual execution counters when calculating retry delay.

*Patrik Bóna*

* Make job argument assertions with `Time`, `ActiveSupport::TimeWithZone`, and `DateTime` work by dropping microseconds. Microsecond precision is lost during serialization.

*Gannon McGibbon*
Expand Down
4 changes: 2 additions & 2 deletions activejob/lib/active_job/exceptions.rb
Expand Up @@ -54,7 +54,7 @@ def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: ni
self.exception_executions[exceptions.to_s] = (exception_executions[exceptions.to_s] || 0) + 1

if exception_executions[exceptions.to_s] < attempts
retry_job wait: determine_delay(wait), queue: queue, priority: priority, error: error
retry_job wait: determine_delay(seconds_or_duration_or_algorithm: wait, executions: exception_executions[exceptions.to_s]), queue: queue, priority: priority, error: error
else
if block_given?
instrument :retry_stopped, error: error do
Expand Down Expand Up @@ -123,7 +123,7 @@ def retry_job(options = {})
end

private
def determine_delay(seconds_or_duration_or_algorithm)
def determine_delay(seconds_or_duration_or_algorithm:, executions:)
case seconds_or_duration_or_algorithm
when :exponentially_longer
(executions**4) + 2
Expand Down
20 changes: 20 additions & 0 deletions activejob/test/cases/exceptions_test.rb
Expand Up @@ -140,6 +140,26 @@ class ExceptionsTest < ActiveSupport::TestCase
], JobBuffer.values
end

test "use individual execution timers when calculating retry delay" do
travel_to Time.now

exceptions_to_raise = %w(ExponentialWaitTenAttemptsError CustomWaitTenAttemptsError ExponentialWaitTenAttemptsError CustomWaitTenAttemptsError)

RetryJob.perform_later exceptions_to_raise, 5, :log_scheduled_at

assert_equal [
"Raised ExponentialWaitTenAttemptsError for the 1st time",
"Next execution scheduled at #{(Time.now + 3.seconds).to_f}",
"Raised CustomWaitTenAttemptsError for the 2nd time",
"Next execution scheduled at #{(Time.now + 2.seconds).to_f}",
"Raised ExponentialWaitTenAttemptsError for the 3rd time",
"Next execution scheduled at #{(Time.now + 18.seconds).to_f}",
"Raised CustomWaitTenAttemptsError for the 4th time",
"Next execution scheduled at #{(Time.now + 4.seconds).to_f}",
"Successfully completed job"
], JobBuffer.values
end

test "successfully retry job throwing one of two retryable exceptions" do
RetryJob.perform_later "SecondRetryableErrorOfTwo", 3

Expand Down

0 comments on commit 263bf76

Please sign in to comment.