Skip to content

Commit daee3d8

Browse files
committed
add tests
1 parent 6d0cc35 commit daee3d8

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

sentry-rails/lib/sentry/rails/active_job.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def already_supported_by_sentry_integration?
2020
class SentryReporter
2121
OP_NAME = "queue.active_job"
2222
SPAN_ORIGIN = "auto.queue.active_job"
23-
23+
NOTIFICATION_NAME = "retry_stopped.active_job"
2424
class << self
2525
def record(job, &block)
2626
Sentry.with_scope do |scope|
@@ -67,17 +67,18 @@ def capture_exception(job, e)
6767
end
6868

6969
def register_retry_stopped_subscriber
70-
ActiveSupport::Notifications.subscribe("retry_stopped.active_job") do |*args|
70+
ActiveSupport::Notifications.subscribe(NOTIFICATION_NAME) do |*args|
7171
retry_stopped_handler(*args)
7272
end
7373
end
7474

7575
def retry_stopped_handler(*args)
76-
return if !Sentry.initialized? || already_supported_by_sentry_integration?
77-
return unless Sentry.configuration.rails.active_job_report_after_job_retries
7876
event = ActiveSupport::Notifications::Event.new(*args)
7977
job = event.payload[:job]
8078
error = event.payload[:error]
79+
80+
return if !Sentry.initialized? || job.already_supported_by_sentry_integration?
81+
return unless Sentry.configuration.rails.active_job_report_after_job_retries
8182
capture_exception(job, error)
8283
end
8384

sentry-rails/spec/sentry/rails/activejob_spec.rb

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class FailedJobWithCron < FailedJob
6767
sentry_monitor_check_ins slug: "failed_job", monitor_config: Sentry::Cron::MonitorConfig.from_crontab("5 * * * *")
6868
end
6969

70+
class FailedJobWithRetryOn < FailedJob
71+
retry_on StandardError, attempts: 3, wait: 0
72+
end
7073

7174
RSpec.describe "without Sentry initialized", type: :job do
7275
it "runs job" do
@@ -318,7 +321,6 @@ def perform(event, hint)
318321

319322
it "does not trigger sentry and re-raises" do
320323
expect { FailedJob.perform_now }.to raise_error(FailedJob::TestError)
321-
322324
expect(transport.events.size).to eq(0)
323325
end
324326
end
@@ -386,4 +388,42 @@ def perform(event, hint)
386388
end
387389
end
388390
end
391+
392+
describe "active_job_report_after_job_retries" do
393+
before do
394+
allow(Sentry::Rails::ActiveJobExtensions::SentryReporter)
395+
.to receive(:capture_exception)
396+
.and_call_original
397+
end
398+
context "when active_job_report_after_job_retries is false" do
399+
it "reports 3 exceptions" do
400+
assert_performed_jobs 3 do
401+
FailedJobWithRetryOn.perform_later rescue nil
402+
end
403+
404+
expect(Sentry::Rails::ActiveJobExtensions::SentryReporter)
405+
.to have_received(:capture_exception)
406+
.exactly(3).times
407+
end
408+
end
409+
context "when active_job_report_after_job_retries is true" do
410+
before do
411+
Sentry.configuration.rails.active_job_report_after_job_retries = true
412+
end
413+
414+
after do
415+
Sentry.configuration.rails.active_job_report_after_job_retries = false
416+
end
417+
418+
it "reports 1 exception" do
419+
assert_performed_jobs 3 do
420+
FailedJobWithRetryOn.perform_later rescue nil
421+
end
422+
423+
expect(Sentry::Rails::ActiveJobExtensions::SentryReporter)
424+
.to have_received(:capture_exception)
425+
.exactly(1).times
426+
end
427+
end
428+
end
389429
end

0 commit comments

Comments
 (0)