1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Pass job hash to handle_exception again (#3224)

When I moved the reloader inside the block so that any errors it raised
would be handled properly, the `job` local variable was pushed into a
nested scope, which meant it wasn't accessible from the rescue block any
more. This changed the meaning of `job` in that rescue block from the
local variable to the `attr_reader` with the same name.

We don't need to reload the application before parsing the job payload,
so we can move this work outside the reloader block so that the job hash
is accessible in the rescue block again.
This commit is contained in:
Eugene Kenny 2016-11-06 21:11:07 +00:00 committed by Mike Perham
parent dcc347b3f4
commit d4b012bd8c
2 changed files with 22 additions and 8 deletions

View file

@ -121,8 +121,8 @@ module Sidekiq
ack = false
begin
job = Sidekiq.load_json(jobstr)
@reloader.call do
job = Sidekiq.load_json(jobstr)
klass = job['class'.freeze].constantize
worker = klass.new
worker.jid = job['jid'.freeze]

View file

@ -67,7 +67,11 @@ class TestProcessor < Sidekiq::Test
describe 'exception handling' do
let(:errors) { [] }
let(:error_handler) { proc { |ex, _| errors << ex } }
let(:error_handler) do
proc do |exception, context|
errors << { exception: exception, context: context }
end
end
before do
Sidekiq.error_handlers << error_handler
@ -78,24 +82,34 @@ class TestProcessor < Sidekiq::Test
end
it 'handles exceptions raised by the job' do
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['boom'] })
job_hash = { 'class' => MockWorker.to_s, 'args' => ['boom'] }
msg = Sidekiq.dump_json(job_hash)
job = work(msg)
begin
@processor.process(work(msg))
@processor.instance_variable_set(:'@job', job)
@processor.process(job)
rescue TestException
end
assert_equal 1, errors.count
assert_instance_of TestException, errors.first
assert_instance_of TestException, errors.first[:exception]
assert_equal msg, errors.first[:context][:jobstr]
assert_equal job_hash, errors.first[:context][:job]
end
it 'handles exceptions raised by the reloader' do
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['myarg'] })
job_hash = { 'class' => MockWorker.to_s, 'args' => ['boom'] }
msg = Sidekiq.dump_json(job_hash)
@processor.instance_variable_set(:'@reloader', proc { raise TEST_EXCEPTION })
job = work(msg)
begin
@processor.process(work(msg))
@processor.instance_variable_set(:'@job', job)
@processor.process(job)
rescue TestException
end
assert_equal 1, errors.count
assert_instance_of TestException, errors.first
assert_instance_of TestException, errors.first[:exception]
assert_equal msg, errors.first[:context][:jobstr]
assert_equal job_hash, errors.first[:context][:job]
end
end