aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrik Bóna <patrik.bona@mrhead.sk>2019-04-12 17:24:05 +0200
committerPatrik Bóna <patrik.bona@mrhead.sk>2019-04-12 17:47:08 +0200
commit3a7fa10603b6842251a6e8b22c302253ae7999ab (patch)
treefa7654e2edcafae7194c0385740a51bbcb30c6a7
parentef0c5fe0d98ad1b1b27fe3d6d19ae62aed3e8aa1 (diff)
downloadrails-3a7fa10603b6842251a6e8b22c302253ae7999ab.tar.gz
rails-3a7fa10603b6842251a6e8b22c302253ae7999ab.tar.bz2
rails-3a7fa10603b6842251a6e8b22c302253ae7999ab.zip
Use individual execution counters when calculating retry delay
Individual execution counters were introduced in #34352. However `#determine_delay` which is used to calculate retry delay still uses the global counter. This commit fixes it.
-rw-r--r--activejob/CHANGELOG.md4
-rw-r--r--activejob/lib/active_job/exceptions.rb4
-rw-r--r--activejob/test/cases/exceptions_test.rb20
3 files changed, 26 insertions, 2 deletions
diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md
index 138c8a8ef4..c4e21b48ac 100644
--- a/activejob/CHANGELOG.md
+++ b/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*
diff --git a/activejob/lib/active_job/exceptions.rb b/activejob/lib/active_job/exceptions.rb
index 9e00942a1c..35c1476368 100644
--- a/activejob/lib/active_job/exceptions.rb
+++ b/activejob/lib/active_job/exceptions.rb
@@ -54,7 +54,7 @@ module ActiveJob
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
@@ -123,7 +123,7 @@ module ActiveJob
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
diff --git a/activejob/test/cases/exceptions_test.rb b/activejob/test/cases/exceptions_test.rb
index c88162bf58..840f4d40b5 100644
--- a/activejob/test/cases/exceptions_test.rb
+++ b/activejob/test/cases/exceptions_test.rb
@@ -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