Skip to content

Commit ea845f5

Browse files
committed
Properly detach retry_stopped.active_job subscriber
Otherwise specs would be failing randomly
1 parent 9306c59 commit ea845f5

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,17 @@ def capture_exception(job, e)
6868
end
6969

7070
def register_retry_stopped_subscriber
71-
ActiveSupport::Notifications.subscribe(NOTIFICATION_NAME) do |*args|
72-
retry_stopped_handler(*args)
71+
unless @retry_stopped_subscriber
72+
@retry_stopped_subscriber = ActiveSupport::Notifications.subscribe(NOTIFICATION_NAME) do |*args|
73+
retry_stopped_handler(*args)
74+
end
75+
end
76+
end
77+
78+
def detach_retry_stopped_subscriber
79+
if @retry_stopped_subscriber
80+
ActiveSupport::Notifications.unsubscribe(@retry_stopped_subscriber)
81+
@retry_stopped_subscriber = nil
7382
end
7483
end
7584

sentry-rails/lib/sentry/rails/railtie.rb

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require "sentry/rails/rescued_exception_interceptor"
55
require "sentry/rails/backtrace_cleaner"
66

7+
require "active_job/railtie" rescue LoadError
8+
79
module Sentry
810
class Railtie < ::Rails::Railtie
911
# middlewares can't be injected after initialize

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def perform(event, hint)
463463
allow(Sentry::Rails::ActiveJobExtensions::SentryReporter)
464464
.to receive(:capture_exception).and_call_original
465465

466-
assert_performed_jobs 1 do
466+
assert_performed_jobs 3 do
467467
FailedJobWithRetryOn.perform_later rescue nil
468468
end
469469

sentry-rails/spec/spec_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
expect(Sentry::Rails::Tracing.subscribed_tracing_events).to be_empty
5353
Sentry::Rails::Tracing.remove_active_support_notifications_patch
5454

55+
if defined?(Sentry::Rails::ActiveJobExtensions::SentryReporter)
56+
Sentry::Rails::ActiveJobExtensions::SentryReporter.detach_retry_stopped_subscriber
57+
end
58+
5559
reset_sentry_globals!
5660
end
5761

0 commit comments

Comments
 (0)