Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean scope before every Sidekiq job execution #421

Merged
merged 1 commit into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/rollbar/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ module Rollbar
class Sidekiq
PARAM_BLACKLIST = %w[backtrace error_backtrace error_message error_class]

class ClearScope
def call(worker, msg, queue)
Rollbar.reset_notifier!

yield
end
end

def self.handle_exception(msg_or_context, e)
return if skip_report?(msg_or_context, e)

Expand All @@ -14,11 +22,13 @@ def self.handle_exception(msg_or_context, e)
end

def self.skip_report?(msg_or_context, e)
msg_or_context.is_a?(Hash) && msg_or_context["retry"] &&
msg_or_context.is_a?(Hash) && msg_or_context["retry"] &&
msg_or_context["retry_count"] && msg_or_context["retry_count"] < ::Rollbar.configuration.sidekiq_threshold
end

def call(worker, msg, queue)
Rollbar.reset_notifier!

yield
rescue Exception => e
Rollbar::Sidekiq.handle_exception(msg, e)
Expand All @@ -28,12 +38,13 @@ def call(worker, msg, queue)
end

Sidekiq.configure_server do |config|
if Sidekiq::VERSION < '3'
if Sidekiq::VERSION.split('.')[0].to_i < 3
config.server_middleware do |chain|
chain.add Rollbar::Sidekiq
end
else
config.error_handlers << Proc.new do |e, context|
chain.add Rollbar::Sidekiq::ClearScope
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chain is undefined here, see #422.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @phlipper, I'll release a new version in few minutes. Sorry for this.

config.error_handlers << proc do |e, context|
Rollbar::Sidekiq.handle_exception(context, e)
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/rollbar/sidekig/clear_scope_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

unless RUBY_VERSION == '1.8.7'
require 'sidekiq'
require 'rollbar/sidekiq'
end

describe Rollbar::Sidekiq::ClearScope, :reconfigure_notifier => false do
describe '#call' do
let(:middleware_block) { proc{} }

it 'sends the error to Rollbar::Sidekiq.handle_exception' do
expect(Rollbar).to receive(:reset_notifier!)

subject.call(nil, nil, nil, &middleware_block)
end
end
end unless RUBY_VERSION == '1.8.7'
5 changes: 3 additions & 2 deletions spec/rollbar/sidekiq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

described_class.handle_exception(msg_or_context, exception)
end

it 'does not blow up and sends the error to rollbar if retry is true but there is no retry count' do
allow(Rollbar).to receive(:scope).and_return(rollbar)
expect(rollbar).to receive(:error)
Expand All @@ -70,7 +70,7 @@
described_class.handle_exception(msg_or_context, exception)
}.to_not raise_error
end

end
end

Expand All @@ -82,6 +82,7 @@
subject { Rollbar::Sidekiq.new }

it 'sends the error to Rollbar::Sidekiq.handle_exception' do
expect(Rollbar).to receive(:reset_notifier!)
expect(Rollbar::Sidekiq).to receive(:handle_exception).with(msg, exception)

expect { subject.call(nil, msg, nil, &middleware_block) }.to raise_error(exception)
Expand Down