Skip to content

Commit 4dc603b

Browse files
authored
Disable background worker when executing rake tasks (#1509)
* Add Hub#with_background_worker_disabled This helper temporarily disables background event dispatching when executing the block. * Wrap rake task's execution with Hub#with_background_worker_disabled This makes sure all events, whether manually triggered or handled by the SDK, can be sent synchronously. * Update changelog
1 parent 157f958 commit 4dc603b

5 files changed

Lines changed: 77 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- Declare `delayed_job` and `sidekiq` as integration gem's dependency [#1506](https://github.com/getsentry/sentry-ruby/pull/1506)
55
- `DSN#server` shouldn't include path [#1505](https://github.com/getsentry/sentry-ruby/pull/1505)
66
- Fix `sentry-rails`' `backtrace_cleanup_callback` injection [#1510](https://github.com/getsentry/sentry-ruby/pull/1510)
7+
- Disable background worker when executing rake tasks [#1509](https://github.com/getsentry/sentry-ruby/pull/1509)
8+
- Fixes [#1508](https://github.com/getsentry/sentry-ruby/issues/1508)
79

810
## 4.6.1
911

sentry-ruby/examples/rake/Rakefile.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77
task :raise_exception do
88
1/0
99
end
10+
11+
task :send_message do
12+
Sentry.capture_message("message from rake")
13+
end

sentry-ruby/lib/sentry/hub.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ def add_breadcrumb(breadcrumb, hint: {})
144144
current_scope.add_breadcrumb(breadcrumb)
145145
end
146146

147+
# this doesn't do anything to the already initialized background worker
148+
# but it temporarily disables dispatching events to it
149+
def with_background_worker_disabled(&block)
150+
original_background_worker_threads = configuration.background_worker_threads
151+
configuration.background_worker_threads = 0
152+
153+
block.call
154+
ensure
155+
configuration.background_worker_threads = original_background_worker_threads
156+
end
157+
147158
private
148159

149160
def current_layer

sentry-ruby/lib/sentry/rake.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
module Rake
55
class Application
6+
67
alias orig_display_error_messsage display_error_message
78
def display_error_message(ex)
89
Sentry.capture_exception(ex, hint: { background: false }) do |scope|
@@ -14,4 +15,16 @@ def display_error_message(ex)
1415
orig_display_error_messsage(ex)
1516
end
1617
end
18+
19+
class Task
20+
alias orig_execute execute
21+
22+
def execute(args=nil)
23+
return orig_execute unless Sentry.initialized? && Sentry.get_current_hub
24+
25+
Sentry.get_current_hub.with_background_worker_disabled do
26+
orig_execute
27+
end
28+
end
29+
end
1730
end

sentry-ruby/spec/sentry/hub_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,51 @@
397397
expect(subject.last_event_id).to eq(event.event_id)
398398
end
399399
end
400+
401+
describe "#with_background_worker_disabled" do
402+
before do
403+
configuration.background_worker_threads = 5
404+
Sentry.background_worker = Sentry::BackgroundWorker.new(configuration)
405+
configuration.before_send = lambda do |event, _hint|
406+
sleep 0.5
407+
event
408+
end
409+
end
410+
411+
after do
412+
Sentry.background_worker = nil
413+
end
414+
415+
it "disables async event sending temporarily" do
416+
subject.with_background_worker_disabled do
417+
subject.capture_message("foo")
418+
end
419+
420+
expect(transport.events.count).to eq(1)
421+
end
422+
423+
it "returns the original execution result" do
424+
result = subject.with_background_worker_disabled do
425+
"foo"
426+
end
427+
428+
expect(result).to eq("foo")
429+
end
430+
431+
it "doesn't interfere events outside of the block" do
432+
subject.with_background_worker_disabled {}
433+
434+
subject.capture_message("foo")
435+
expect(transport.events.count).to eq(0)
436+
end
437+
438+
it "resumes the backgrounding state even with exception" do
439+
subject.with_background_worker_disabled do
440+
raise "foo"
441+
end rescue nil
442+
443+
subject.capture_message("foo")
444+
expect(transport.events.count).to eq(0)
445+
end
446+
end
400447
end

0 commit comments

Comments
 (0)