Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sidekiq_retry_in to return a range #4957

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes.md
Expand Up @@ -5,6 +5,7 @@
HEAD
---------

- Sidekiq::Worker `sidekiq_retry_in` block can now return a range [#4957]
- Minimize scheduler load on Redis at scale [#4882]
- Improve logging of delay jobs [#4904, BuonOno]
- Minor CSS improvements for buttons and tables, design PRs always welcome!
Expand Down
7 changes: 6 additions & 1 deletion lib/sidekiq/job_retry.rb
Expand Up @@ -215,7 +215,12 @@ def retry_attempts_from(msg_retry, default)

def delay_for(worker, count, exception)
if worker&.sidekiq_retry_in_block
custom_retry_in = retry_in(worker, count, exception).to_i
range_or_interval = retry_in(worker, count, exception)
custom_retry_in = if range_or_interval.is_a?(Range)
rand(range_or_interval)
else
range_or_interval
end.to_i
return custom_retry_in if custom_retry_in > 0
end
seconds_to_delay(count)
Expand Down
6 changes: 6 additions & 0 deletions test/test_retry.rb
Expand Up @@ -287,6 +287,8 @@ class CustomWorkerWithException
Sidekiq::JobRetry::USE_DEFAULT_RETRY_FORMULA
when ArgumentError
count * 4
when ZeroDivisionError
(count..count * 2)
else
count * 2
end
Expand All @@ -313,6 +315,10 @@ class ErrorWorker
assert_equal 4, handler.__send__(:delay_for, CustomWorkerWithException, 2, StandardError.new)
end

it "retries with a custom delay and exception 3" do
assert_includes 2..4, handler.__send__(:delay_for, CustomWorkerWithException, 2, ZeroDivisionError.new)
end

it "retries with a default delay and exception in case of configured with nil" do
refute_equal 8, handler.__send__(:delay_for, CustomWorkerWithException, 2, SpecialError.new)
refute_equal 4, handler.__send__(:delay_for, CustomWorkerWithException, 2, SpecialError.new)
Expand Down