Skip to content

Commit 0fc2c43

Browse files
committed
add tests
1 parent 2d2621c commit 0fc2c43

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-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: 44 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" do
7275
it "runs job" do
@@ -79,6 +82,9 @@ class FailedJobWithCron < FailedJob
7982
end
8083

8184
RSpec.describe "ActiveJob integration" do
85+
include ActiveJob::TestHelper
86+
include ActiveSupport::Testing::TaggedLogging
87+
8288
before do
8389
make_basic_app
8490
end
@@ -318,7 +324,6 @@ def perform(event, hint)
318324

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

0 commit comments

Comments
 (0)