Skip to content

Commit

Permalink
Log errors happened in BackgroundWorker#perform (#1624)
Browse files Browse the repository at this point in the history
* Log exceptions happened in background worker

* Provide a safer background worker method for monkey-patching

* Update changelog
  • Loading branch information
st0012 authored Nov 22, 2021
1 parent 1f96dce commit bf37772
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Merge context with the same key instead of replacing the old value. [#1621](https://github.com/getsentry/sentry-ruby/pull/1621)
- Fixes [#1619](https://github.com/getsentry/sentry-ruby/issues/1619)
- Fix `HTTPTransport`'s `ssl` configuration [#1626](https://github.com/getsentry/sentry-ruby/pull/1626)
- Log errors happened in `BackgroundWorker#perform` [#1624](https://github.com/getsentry/sentry-ruby/pull/1624)
- Fixes [#1618](https://github.com/getsentry/sentry-ruby/issues/1618)

### Refactoring

Expand Down
10 changes: 4 additions & 6 deletions sentry-rails/lib/sentry/rails/background_worker.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module Sentry
class BackgroundWorker
def perform(&block)
@executor.post do
# make sure the background worker returns AR connection if it accidentally acquire one during serialization
ActiveRecord::Base.connection_pool.with_connection do
block.call
end
def _perform(&block)
# make sure the background worker returns AR connection if it accidentally acquire one during serialization
ActiveRecord::Base.connection_pool.with_connection do
block.call
end
end
end
Expand Down
14 changes: 13 additions & 1 deletion sentry-ruby/lib/sentry/background_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(configuration)
@max_queue = 30
@number_of_threads = configuration.background_worker_threads
@logger = configuration.logger
@debug = configuration.debug

@executor =
if configuration.async
Expand All @@ -32,10 +33,21 @@ def initialize(configuration)
end
end

# if you want to monkey-patch this method, please override `_perform` instead
def perform(&block)
@executor.post do
block.call
begin
_perform(&block)
rescue Exception => e
log_error("exception happened in background worker", e, debug: @debug)
end
end
end

private

def _perform(&block)
block.call
end
end
end
25 changes: 20 additions & 5 deletions sentry-ruby/spec/sentry/background_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
RSpec.describe Sentry::BackgroundWorker do
let(:string_io) { StringIO.new }

describe "#initialize" do
let(:configuration) do
Sentry::Configuration.new.tap do |config|
config.logger = Logger.new(string_io)
end
let(:configuration) do
Sentry::Configuration.new.tap do |config|
config.logger = Logger.new(string_io)
end
end

describe "#initialize" do
context "when config.async is set" do
before do
configuration.async = proc {}
Expand Down Expand Up @@ -67,4 +67,19 @@
end
end
end

describe "#perform" do
before { configuration.background_worker_threads = 1 }

it "logs error message when failed" do
worker = described_class.new(configuration)

worker.perform do
1/0
end

sleep(0.1)
expect(string_io.string).to match(/exception happened in background worker: divided by 0/)
end
end
end

0 comments on commit bf37772

Please sign in to comment.