Skip to content

Commit

Permalink
Fix sentry-delayed_job: respect custom max_attempts (#2177)
Browse files Browse the repository at this point in the history
* sentry-delayed_job: respect custom max_attempts

* Update sentry-delayed_job/spec/sentry/delayed_job_spec.rb

Co-authored-by: Stan Lo <[email protected]>

* Changelog for delayed_job max_attempts fix

---------

Co-authored-by: Stan Lo <[email protected]>
  • Loading branch information
natikgadzhi and st0012 authored Nov 23, 2023
1 parent 7793e97 commit 19251ea
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

- Fixed a deprecation in `sidekiq-ruby` error handler [#2160](https://github.com/getsentry/sentry-ruby/pull/2160)
- Avoid invoking ActiveSupport::BroadcastLogger if not defined [#2169](https://github.com/getsentry/sentry-ruby/pull/2169)
- Respect custom `Delayed::Job.max_attempts` if it's defined [#2176](https://github.com/getsentry/sentry-ruby/pull/2176)
## 5.13.0
Expand Down
3 changes: 2 additions & 1 deletion sentry-delayed_job/lib/sentry/delayed_job/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def self.report?(job)

# We use the predecessor because the job's attempts haven't been increased to the new
# count at this point.
job.attempts >= Delayed::Worker.max_attempts.pred
max_attempts = job&.max_attempts&.pred || Delayed::Worker.max_attempts.pred
job.attempts >= max_attempts
end

def self.finish_transaction(transaction, status)
Expand Down
16 changes: 16 additions & 0 deletions sentry-delayed_job/spec/sentry/delayed_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ def self.class_do_nothing
expect(transport.events.count).to eq(1)
end

# Default max_attemps is defined on Delayed::Worker.max_attempts == 25.
# However, users can customize max_attempts on the job class, and DelayedJob
# will respect that.
# Sentry needs to report an exception if report_after_retries is true and
# custom job-level max_attempts is reached.
# See https://github.com/collectiveidea/delayed_job#custom-jobs
it "reports exception after the job's custom max_attempts" do
enqueued_job.update(attempts: 2)
allow(enqueued_job).to receive(:max_attempts).and_return(3)

expect do
enqueued_job.invoke_job
end.to raise_error(ZeroDivisionError)
expect(transport.events.count).to eq(1)
end

it "skips report if not on the last retry" do
enqueued_job.update(attempts: 0)

Expand Down
1 change: 0 additions & 1 deletion sentry-ruby/spec/sentry/transport/http_transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@
stub_request(error_response)

expect { subject.send_data(data) }.to raise_error(Sentry::ExternalError, /error_in_header/)

end
end
end
Expand Down

0 comments on commit 19251ea

Please sign in to comment.