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

feat: add warn_after option to allow warning not only for the last one #5

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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