From efa96a1a47d3644c0eb71cd34f3048415d707d25 Mon Sep 17 00:00:00 2001 From: maurycy <5383+maurycy@users.noreply.github.com> Date: Sun, 8 Aug 2021 00:05:53 +0200 Subject: [PATCH] retry_in range --- Changes.md | 1 + lib/sidekiq/job_retry.rb | 7 ++++++- test/test_retry.rb | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Changes.md b/Changes.md index eb4ac9acd..b24c7726a 100644 --- a/Changes.md +++ b/Changes.md @@ -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! diff --git a/lib/sidekiq/job_retry.rb b/lib/sidekiq/job_retry.rb index 552a75403..e43a9ba22 100644 --- a/lib/sidekiq/job_retry.rb +++ b/lib/sidekiq/job_retry.rb @@ -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) diff --git a/test/test_retry.rb b/test/test_retry.rb index bcaaf2e90..edfd57d6a 100644 --- a/test/test_retry.rb +++ b/test/test_retry.rb @@ -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 @@ -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)