Skip to content

Commit 19251ea

Browse files
natikgadzhist0012
andauthored
Fix sentry-delayed_job: respect custom max_attempts (#2177)
* sentry-delayed_job: respect custom max_attempts * Update sentry-delayed_job/spec/sentry/delayed_job_spec.rb Co-authored-by: Stan Lo <stan001212@gmail.com> * Changelog for delayed_job max_attempts fix --------- Co-authored-by: Stan Lo <stan001212@gmail.com>
1 parent 7793e97 commit 19251ea

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
- Fixed a deprecation in `sidekiq-ruby` error handler [#2160](https://github.com/getsentry/sentry-ruby/pull/2160)
2626
- Avoid invoking ActiveSupport::BroadcastLogger if not defined [#2169](https://github.com/getsentry/sentry-ruby/pull/2169)
27+
- Respect custom `Delayed::Job.max_attempts` if it's defined [#2176](https://github.com/getsentry/sentry-ruby/pull/2176)
2728
2829
## 5.13.0
2930

sentry-delayed_job/lib/sentry/delayed_job/plugin.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def self.report?(job)
8787

8888
# We use the predecessor because the job's attempts haven't been increased to the new
8989
# count at this point.
90-
job.attempts >= Delayed::Worker.max_attempts.pred
90+
max_attempts = job&.max_attempts&.pred || Delayed::Worker.max_attempts.pred
91+
job.attempts >= max_attempts
9192
end
9293

9394
def self.finish_transaction(transaction, status)

sentry-delayed_job/spec/sentry/delayed_job_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ def self.class_do_nothing
143143
expect(transport.events.count).to eq(1)
144144
end
145145

146+
# Default max_attemps is defined on Delayed::Worker.max_attempts == 25.
147+
# However, users can customize max_attempts on the job class, and DelayedJob
148+
# will respect that.
149+
# Sentry needs to report an exception if report_after_retries is true and
150+
# custom job-level max_attempts is reached.
151+
# See https://github.com/collectiveidea/delayed_job#custom-jobs
152+
it "reports exception after the job's custom max_attempts" do
153+
enqueued_job.update(attempts: 2)
154+
allow(enqueued_job).to receive(:max_attempts).and_return(3)
155+
156+
expect do
157+
enqueued_job.invoke_job
158+
end.to raise_error(ZeroDivisionError)
159+
expect(transport.events.count).to eq(1)
160+
end
161+
146162
it "skips report if not on the last retry" do
147163
enqueued_job.update(attempts: 0)
148164

sentry-ruby/spec/sentry/transport/http_transport_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@
275275
stub_request(error_response)
276276

277277
expect { subject.send_data(data) }.to raise_error(Sentry::ExternalError, /error_in_header/)
278-
279278
end
280279
end
281280
end

0 commit comments

Comments
 (0)