Skip to content

Commit d6b63bb

Browse files
authored
Fix report_after_job_retries's decision logic (#1704)
* Fix report_after_job_retries's decision logic * Update changelog
1 parent a7f7450 commit d6b63bb

File tree

4 files changed

+99
-39
lines changed

4 files changed

+99
-39
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
- Respect port info provided in user's DSN [#1702](https://github.com/getsentry/sentry-ruby/pull/1702)
44
- Fixes [#1699](https://github.com/getsentry/sentry-ruby/issues/1699)
55
- Capture transaction tags [#1701](https://github.com/getsentry/sentry-ruby/pull/1701)
6+
- Fix `report_after_job_retries`'s decision logic [#1704](https://github.com/getsentry/sentry-ruby/pull/1704)
7+
- Fixes [#1698](https://github.com/getsentry/sentry-ruby/issues/1698)
68

79
## 5.0.1
810

sentry-sidekiq/lib/sentry/sidekiq/error_handler.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ def call(ex, context)
1111
scope = Sentry.get_current_scope
1212
scope.set_transaction_name(context_filter.transaction_name) unless scope.transaction_name
1313

14-
retry_option = context.dig(:job, "retry")
15-
16-
if Sentry.configuration.sidekiq.report_after_job_retries && retry_option.is_a?(Integer) && retry_option.positive?
14+
if Sentry.configuration.sidekiq.report_after_job_retries && retryable?(context)
1715
retry_count = context.dig(:job, "retry_count")
18-
if retry_count.nil? || retry_count < retry_option - 1
16+
if retry_count.nil? || retry_count < retry_limit(context) - 1
1917
return
2018
end
2119
end
@@ -26,6 +24,27 @@ def call(ex, context)
2624
hint: { background: false }
2725
)
2826
end
27+
28+
private
29+
30+
def retryable?(context)
31+
retry_option = context.dig(:job, "retry")
32+
# when `retry` is not specified, it's default is `true` and it means 25 retries.
33+
retry_option == true || (retry_option.is_a?(Integer) && retry_option.positive?)
34+
end
35+
36+
def retry_limit(context)
37+
limit = context.dig(:job, "retry")
38+
39+
case limit
40+
when Integer
41+
limit
42+
when TrueClass
43+
::Sidekiq.options[:max_retries] || 25
44+
else
45+
0
46+
end
47+
end
2948
end
3049
end
3150
end

sentry-sidekiq/spec/sentry/sidekiq_spec.rb

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,90 @@
9494
Sentry.configuration.sidekiq.report_after_job_retries = true
9595
end
9696

97-
it "doesn't report the error until retries are exhuasted" do
98-
execute_worker(processor, RetryWorker)
99-
100-
expect(transport.events.count).to eq(0)
101-
102-
expect(retry_set.count).to eq(1)
103-
97+
def retry_last_failed_job
10498
retry_set.first.add_to_queue
10599
job = queue.first
106100
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', job.value)
107101
process_work(processor, work)
108-
expect(transport.events.count).to eq(1)
109102
end
110103

111-
it "doesn't affect no-retry jobs" do
112-
execute_worker(processor, SadWorker)
104+
context "when retry: is specified" do
105+
it "doesn't report the error until retries are exhuasted" do
106+
worker = Class.new(SadWorker)
107+
worker.sidekiq_options retry: 5
108+
execute_worker(processor, worker)
109+
expect(transport.events.count).to eq(0)
110+
expect(retry_set.count).to eq(1)
111+
112+
4.times do |i|
113+
retry_last_failed_job
114+
expect(transport.events.count).to eq(0)
115+
end
116+
117+
retry_last_failed_job
118+
expect(transport.events.count).to eq(1)
119+
end
120+
end
113121

114-
expect(transport.events.count).to eq(1)
115-
expect(retry_set.count).to eq(1)
122+
context "when the job has 0 retries" do
123+
it "reports on the first failure" do
124+
worker = Class.new(SadWorker)
125+
worker.sidekiq_options retry: 0
126+
127+
execute_worker(processor, worker)
128+
129+
expect(transport.events.count).to eq(1)
130+
end
116131
end
117132

118-
it "doesn't affect jobs with zero retries" do
119-
execute_worker(processor, ZeroRetryWorker)
133+
context "when the job has retry: false" do
134+
it "reports on the first failure" do
135+
worker = Class.new(SadWorker)
136+
worker.sidekiq_options retry: false
120137

121-
expect(transport.events.count).to eq(1)
138+
execute_worker(processor, worker)
139+
140+
expect(transport.events.count).to eq(1)
141+
end
142+
end
143+
144+
context "when retry is not specified on the worker" do
145+
before do
146+
# this is required for Sidekiq to assign default options to the worker
147+
SadWorker.sidekiq_options
148+
end
149+
150+
it "reports on the 25th retry" do
151+
execute_worker(processor, SadWorker)
152+
expect(transport.events.count).to eq(0)
153+
expect(retry_set.count).to eq(1)
154+
155+
24.times do |i|
156+
retry_last_failed_job
157+
expect(transport.events.count).to eq(0)
158+
end
159+
160+
retry_last_failed_job
161+
expect(transport.events.count).to eq(1)
162+
end
163+
164+
context "when Sidekiq.options[:max_retries] is set" do
165+
it "respects the set limit" do
166+
Sidekiq.options[:max_retries] = 5
167+
168+
execute_worker(processor, SadWorker)
169+
expect(transport.events.count).to eq(0)
170+
expect(retry_set.count).to eq(1)
171+
172+
4.times do |i|
173+
retry_last_failed_job
174+
expect(transport.events.count).to eq(0)
175+
end
176+
177+
retry_last_failed_job
178+
expect(transport.events.count).to eq(1)
179+
end
180+
end
122181
end
123182
end
124183

sentry-sidekiq/spec/spec_helper.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,26 +133,6 @@ def perform
133133
end
134134
end
135135

136-
class RetryWorker
137-
include Sidekiq::Worker
138-
139-
sidekiq_options retry: 1
140-
141-
def perform
142-
1/0
143-
end
144-
end
145-
146-
class ZeroRetryWorker
147-
include Sidekiq::Worker
148-
149-
sidekiq_options retry: 0
150-
151-
def perform
152-
1/0
153-
end
154-
end
155-
156136
class TagsWorker
157137
include Sidekiq::Worker
158138

0 commit comments

Comments
 (0)