Skip to content

Commit 46e7072

Browse files
authored
Gracefully shutdown background worker before the process exits (#1617)
* Support BackgroundWorker#shutdown * Gracefully shutdown background worker and remove manual synchronizing code * Update changelog
1 parent 94b1a5e commit 46e7072

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
- Fix `HTTPTransport`'s `ssl` configuration [#1626](https://github.com/getsentry/sentry-ruby/pull/1626)
88
- Log errors happened in `BackgroundWorker#perform` [#1624](https://github.com/getsentry/sentry-ruby/pull/1624)
99
- Fixes [#1618](https://github.com/getsentry/sentry-ruby/issues/1618)
10+
- Gracefully shutdown background worker before the process exits [#1617](https://github.com/getsentry/sentry-ruby/pull/1617)
11+
- Fixes [#1612](https://github.com/getsentry/sentry-ruby/issues/1612)
1012

1113
### Refactoring
1214

1315
- Extract envelope construction logic from Transport [#1616](https://github.com/getsentry/sentry-ruby/pull/1616)
16+
- Add frozen string literal comment to sentry-ruby [#1623](https://github.com/getsentry/sentry-ruby/pull/1623)
1417

1518
## 4.8.0
1619

sentry-ruby/lib/sentry-ruby.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def init(&block)
110110
if config.capture_exception_frame_locals
111111
exception_locals_tp.enable
112112
end
113+
114+
at_exit do
115+
@background_worker.shutdown
116+
end
113117
end
114118

115119
# Returns an uri for security policy reporting that's generated from the given DSN

sentry-ruby/lib/sentry/background_worker.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ class BackgroundWorker
99
include LoggingHelper
1010

1111
attr_reader :max_queue, :number_of_threads, :logger
12+
attr_accessor :shutdown_timeout
1213

1314
def initialize(configuration)
1415
@max_queue = 30
16+
@shutdown_timeout = 1
1517
@number_of_threads = configuration.background_worker_threads
1618
@logger = configuration.logger
1719
@debug = configuration.debug
20+
@shutdown_callback = nil
1821

1922
@executor =
2023
if configuration.async
@@ -26,12 +29,19 @@ def initialize(configuration)
2629
else
2730
log_debug("initialized a background worker with #{@number_of_threads} threads")
2831

29-
Concurrent::ThreadPoolExecutor.new(
32+
executor = Concurrent::ThreadPoolExecutor.new(
3033
min_threads: 0,
3134
max_threads: @number_of_threads,
3235
max_queue: @max_queue,
3336
fallback_policy: :discard
3437
)
38+
39+
@shutdown_callback = proc do
40+
executor.shutdown
41+
executor.wait_for_termination(@shutdown_timeout)
42+
end
43+
44+
executor
3545
end
3646
end
3747

@@ -46,6 +56,10 @@ def perform(&block)
4656
end
4757
end
4858

59+
def shutdown
60+
@shutdown_callback&.call
61+
end
62+
4963
private
5064

5165
def _perform(&block)

sentry-ruby/lib/sentry/rake.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Sentry
77
module Rake
88
module Application
99
def display_error_message(ex)
10-
Sentry.capture_exception(ex, hint: { background: false }) do |scope|
10+
Sentry.capture_exception(ex) do |scope|
1111
task_name = top_level_tasks.join(' ')
1212
scope.set_transaction_name(task_name)
1313
scope.set_tag("rake_task", task_name)
@@ -21,9 +21,7 @@ module Task
2121
def execute(args=nil)
2222
return super unless Sentry.initialized? && Sentry.get_current_hub
2323

24-
Sentry.get_current_hub.with_background_worker_disabled do
25-
super
26-
end
24+
super
2725
end
2826
end
2927
end

0 commit comments

Comments
 (0)