Skip to content

Commit

Permalink
feat: add warn_after option to allow warning not only for the last one
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavodiel committed Jul 4, 2024
1 parent ee63afd commit 979ebb4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ In your Sidekiq job class, configure the silent_retry option to control silent r
- `true`: Always enabled, no matter the error.
- Some class / Array of classes: Only exceptions of said class(es) will be silently retried.

You can also set `warn_after` option to start raise warnings after a number of retries instead of only the last one.

```ruby
class MyJob
include Sidekiq::Job
Expand All @@ -54,7 +56,7 @@ end
class MyJob
include Sidekiq::Job

sidekiq_options silent_retry: [VeryCommonNotImportantError, VeryCommonNotImportantError2]
sidekiq_options silent_retry: [CommonError, CommonError2], warn_after: 2

def perform(*args)
# Your job logic here
Expand Down
18 changes: 11 additions & 7 deletions lib/sidekiq/silent_retry/server_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ class ServerMiddleware

def call(_job_instance, job_payload, _queue)
yield
rescue => error
raise error unless silent_retry_enabled?(job_payload, error)
raise error if retries_exhausted?(job_payload) # if it's the last retry, raise the original error
rescue StandardError => e
raise e unless silent_retry_enabled?(job_payload, e)
raise e if should_warn?(job_payload)

raise Sidekiq::SilentRetry.silent_retry_error_class, error.message
raise Sidekiq::SilentRetry.silent_retry_error_class, e.message
end

private

def retries_exhausted?(job_payload)
job_payload['retry_count'] == job_payload['retry'] - 1
def should_warn?(job_payload)
job_payload["retry_count"] >= warn_after(job_payload)
end

def warn_after(job_payload)
job_payload["warn_after"]&.to_i || job_payload["retry"] - 1
end

def silent_retry_enabled?(job_payload, error)
option = job_payload['silent_retry']
option = job_payload["silent_retry"]

case option
when TrueClass, FalseClass
Expand Down
35 changes: 35 additions & 0 deletions spec/sidekiq/silent_retry/server_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,41 @@
expect { subject }.to raise_error(StandardError, "some message")
end
end

context 'when the job has a warn_after option' do
let(:job_payload) do
{
"retry_count" => retry_count,
"retry" => 2,
"silent_retry" => silent_retry,
"warn_after" => 1
}
end

context 'when the retry count is less than warn_after' do
let(:retry_count) { 0 }

it 'silents the error' do
expect { subject }.to raise_error(Sidekiq::SilentRetry.silent_retry_error_class, "some message")
end
end

context 'when the retry count is equal to warn_after' do
let(:retry_count) { 1 }

it 'raises original error' do
expect { subject }.to raise_error(StandardError, "some message")
end
end

context 'when the retry count is greater than warn_after' do
let(:retry_count) { 2 }

it 'raises original error' do
expect { subject }.to raise_error(StandardError, "some message")
end
end
end
end

context "when silent retry is for specific classes" do
Expand Down

0 comments on commit 979ebb4

Please sign in to comment.