diff --git a/CHANGELOG.md b/CHANGELOG.md index f1bcb1271..be04a94a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/sentry-rails/lib/sentry/rails/background_worker.rb b/sentry-rails/lib/sentry/rails/background_worker.rb index 93e60809d..771ded58e 100644 --- a/sentry-rails/lib/sentry/rails/background_worker.rb +++ b/sentry-rails/lib/sentry/rails/background_worker.rb @@ -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 diff --git a/sentry-ruby/lib/sentry/background_worker.rb b/sentry-ruby/lib/sentry/background_worker.rb index 51360ce53..80cbc039b 100644 --- a/sentry-ruby/lib/sentry/background_worker.rb +++ b/sentry-ruby/lib/sentry/background_worker.rb @@ -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 @@ -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 diff --git a/sentry-ruby/spec/sentry/background_worker_spec.rb b/sentry-ruby/spec/sentry/background_worker_spec.rb index f5d408f6a..024db0594 100644 --- a/sentry-ruby/spec/sentry/background_worker_spec.rb +++ b/sentry-ruby/spec/sentry/background_worker_spec.rb @@ -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 {} @@ -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