Skip to content

Commit 8004387

Browse files
committed
Add middleware to clear context after every Sidekiq job
The error handler is only called after an exception so the context will carry over from previous jobs that completed successfully. Fixes getsentry#636
1 parent 1baa7f2 commit 8004387

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/raven/integrations/sidekiq.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
require 'sidekiq'
33

44
module Raven
5-
class Sidekiq
5+
class SidekiqCleanupMiddleware
6+
def call(_worker, _job, _queue)
7+
yield
8+
ensure
9+
Context.clear!
10+
BreadcrumbBuffer.clear!
11+
end
12+
end
13+
14+
class SidekiqErrorHandler
615
ACTIVEJOB_RESERVED_PREFIX = "_aj_".freeze
716

817
def call(ex, context)
@@ -13,9 +22,6 @@ def call(ex, context)
1322
:extra => { :sidekiq => context },
1423
:culprit => culprit_from_context(context)
1524
)
16-
ensure
17-
Context.clear!
18-
BreadcrumbBuffer.clear!
1925
end
2026

2127
private
@@ -59,6 +65,9 @@ def culprit_from_context(context)
5965

6066
if Sidekiq::VERSION > '3'
6167
Sidekiq.configure_server do |config|
62-
config.error_handlers << Raven::Sidekiq.new
68+
config.error_handlers << Raven::SidekiqErrorHandler.new
69+
config.server_middleware do |chain|
70+
chain.add Raven::SidekiqCleanupMiddleware
71+
end
6372
end
6473
end

spec/raven/integrations/sidekiq_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22
require 'raven/integrations/sidekiq'
33

4-
describe Raven::Sidekiq do
4+
describe Raven::SidekiqErrorHandler do
55
before(:all) do
66
Raven.configure do |config|
77
config.dsn = 'dummy://12345:[email protected]:3000/sentry/42'

0 commit comments

Comments
 (0)