Skip to content

Commit e2cdffc

Browse files
committed
Add config option for ActiveJob::Base.default_retry_jitter
1 parent 774e77c commit e2cdffc

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

activejob/lib/active_job/exceptions.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ module ActiveJob
77
module Exceptions
88
extend ActiveSupport::Concern
99

10+
included do
11+
class_attribute :default_retry_jitter, instance_accessor: false, instance_predicate: false
12+
self.default_retry_jitter = 0.15
13+
end
14+
1015
module ClassMethods
1116
# Catch the exception and reschedule job for re-execution after so many seconds, for a specific number of attempts.
1217
# If the exception keeps getting raised beyond the specified number of attempts, the exception is allowed to
@@ -49,7 +54,7 @@ module ClassMethods
4954
# # Might raise Net::OpenTimeout or Timeout::Error when the remote service is down
5055
# end
5156
# end
52-
def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil, jitter: 0.15)
57+
def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil, jitter: nil)
5358
rescue_from(*exceptions) do |error|
5459
executions = executions_for(exceptions)
5560
if executions < attempts
@@ -122,7 +127,8 @@ def retry_job(options = {})
122127
end
123128

124129
private
125-
def determine_delay(seconds_or_duration_or_algorithm:, executions:, jitter:)
130+
def determine_delay(seconds_or_duration_or_algorithm:, executions:, jitter: nil)
131+
jitter ||= self.class.default_retry_jitter
126132
case seconds_or_duration_or_algorithm
127133
when :exponentially_longer
128134
((executions**4) + (Kernel.rand((executions**4) * jitter))) + 2

activejob/test/cases/exceptions_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,31 @@ class ExceptionsTest < ActiveSupport::TestCase
129129
end
130130
end
131131

132+
test "retry jitter uses value from ActiveJob::Base.default_retry_jitter by default" do
133+
old_jitter = ActiveJob::Base.default_retry_jitter
134+
ActiveJob::Base.default_retry_jitter = 4.0
135+
136+
travel_to Time.now
137+
138+
Kernel.stub(:rand, ->(arg) { arg }) do
139+
RetryJob.perform_later "ExponentialWaitTenAttemptsError", 5, :log_scheduled_at
140+
141+
assert_equal [
142+
"Raised ExponentialWaitTenAttemptsError for the 1st time",
143+
"Next execution scheduled at #{(Time.now + 7.seconds).to_f}",
144+
"Raised ExponentialWaitTenAttemptsError for the 2nd time",
145+
"Next execution scheduled at #{(Time.now + 82.seconds).to_f}",
146+
"Raised ExponentialWaitTenAttemptsError for the 3rd time",
147+
"Next execution scheduled at #{(Time.now + 407.seconds).to_f}",
148+
"Raised ExponentialWaitTenAttemptsError for the 4th time",
149+
"Next execution scheduled at #{(Time.now + 1282.seconds).to_f}",
150+
"Successfully completed job"
151+
], JobBuffer.values
152+
end
153+
ensure
154+
ActiveJob::Base.default_retry_jitter = old_jitter
155+
end
156+
132157
test "custom wait retrying job" do
133158
travel_to Time.now
134159

railties/lib/rails/application/configuration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ def load_defaults(target_version)
156156
when "6.1"
157157
load_defaults "6.0"
158158

159+
if respond_to?(:active_job)
160+
active_job.default_retry_jitter = 0.15
161+
end
162+
159163
if respond_to?(:active_record)
160164
active_record.has_many_inversing = true
161165
end

railties/test/application/configuration_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,20 @@ class ::DummySerializer < ActiveJob::Serializers::ObjectSerializer; end
22622262
end
22632263
end
22642264

2265+
test "ActiveJob::Base.default_retry_jitter is 0.15 by default" do
2266+
app "development"
2267+
2268+
assert_equal 0.15, ActiveJob::Base.default_retry_jitter
2269+
end
2270+
2271+
test "ActiveJob::Base.default_retry_jitter can be set by config" do
2272+
app "development"
2273+
2274+
Rails.application.config.active_job.default_retry_jitter = 0.22
2275+
2276+
assert_equal 0.22, ActiveJob::Base.default_retry_jitter
2277+
end
2278+
22652279
test "ActiveJob::Base.return_false_on_aborted_enqueue is true by default" do
22662280
app "development"
22672281

0 commit comments

Comments
 (0)