Skip to content

Commit

Permalink
Merge pull request #5 from trusted/add-warn-after-option
Browse files Browse the repository at this point in the history
feat: add warn_after option to allow warning not only for the last one
  • Loading branch information
gustavodiel authored Jul 4, 2024
2 parents 808b285 + a987667 commit e7dcdb2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sidekiq-silent-retry (0.1.0)
sidekiq-silent-retry (0.2.0)
sidekiq (>= 6.0)

GEM
Expand Down
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
2 changes: 1 addition & 1 deletion lib/sidekiq/silent_retry/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Sidekiq
module SilentRetry
VERSION = "0.1.0"
VERSION = "0.2.0"
end
end
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 e7dcdb2

Please sign in to comment.