Skip to content

Commit 515887a

Browse files
trevorturksteveklabnik
authored andcommitted
Pass exceptions raised by the worker into the Failure backend
Conflicts: test/worker_test.rb
1 parent 5057366 commit 515887a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/resque/worker.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,9 @@ def work(interval = 5.0, &block)
163163
end
164164
end
165165

166-
ensure
167166
unregister_worker
167+
rescue Exception => exception
168+
unregister_worker(exception)
168169
end
169170

170171
# DEPRECATED. Processes a single job. If none is given, it will
@@ -411,15 +412,15 @@ def run_hook(name, *args)
411412
end
412413

413414
# Unregisters ourself as a worker. Useful when shutting down.
414-
def unregister_worker
415+
def unregister_worker(exception = nil)
415416
# If we're still processing a job, make sure it gets logged as a
416417
# failure.
417418
if (hash = processing) && !hash.empty?
418419
job = Job.new(hash['queue'], hash['payload'])
419420
# Ensure the proper worker is attached to this job, even if
420421
# it's not the precise instance that died.
421422
job.worker = self
422-
job.fail(DirtyExit.new)
423+
job.fail(exception || DirtyExit.new)
423424
end
424425

425426
redis.srem(:workers, self)

test/worker_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,20 @@
4343
end
4444
end
4545

46-
it "fails uncompleted jobs on exit" do
46+
it "fails uncompleted jobs with DirtyExit by default on exit" do
4747
job = Resque::Job.new(:jobs, {'class' => 'GoodJob', 'args' => "blah"})
4848
@worker.working_on(job)
4949
@worker.unregister_worker
5050
assert_equal 1, Resque::Failure.count
51+
assert_equal('Resque::DirtyExit', Resque::Failure.all['exception'])
52+
end
53+
54+
test "fails uncompleted jobs with worker exception on exit" do
55+
job = Resque::Job.new(:jobs, {'class' => 'GoodJob', 'args' => "blah"})
56+
@worker.working_on(job)
57+
@worker.unregister_worker(StandardError.new)
58+
assert_equal 1, Resque::Failure.count
59+
assert_equal('StandardError', Resque::Failure.all['exception'])
5160
end
5261

5362
class ::SimpleJobWithFailureHandling

0 commit comments

Comments
 (0)