Skip to content

Commit e586e28

Browse files
committed
Pass exceptions raised by the worker into the Failure backend
1 parent 39aa466 commit e586e28

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
@@ -164,8 +164,9 @@ def work(interval = 5.0, &block)
164164
end
165165
end
166166

167-
ensure
168167
unregister_worker
168+
rescue Exception => exception
169+
unregister_worker(exception)
169170
end
170171

171172
# DEPRECATED. Processes a single job. If none is given, it will
@@ -425,15 +426,15 @@ def run_hook(name, *args)
425426
end
426427

427428
# Unregisters ourself as a worker. Useful when shutting down.
428-
def unregister_worker
429+
def unregister_worker(exception = nil)
429430
# If we're still processing a job, make sure it gets logged as a
430431
# failure.
431432
if (hash = processing) && !hash.empty?
432433
job = Job.new(hash['queue'], hash['payload'])
433434
# Ensure the proper worker is attached to this job, even if
434435
# it's not the precise instance that died.
435436
job.worker = self
436-
job.fail(DirtyExit.new)
437+
job.fail(exception || DirtyExit.new)
437438
end
438439

439440
redis.srem(:workers, self)

test/worker_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@
3232
end
3333
end
3434

35-
test "fails uncompleted jobs on exit" do
35+
test "fails uncompleted jobs with DirtyExit by default on exit" do
3636
job = Resque::Job.new(:jobs, {'class' => 'GoodJob', 'args' => "blah"})
3737
@worker.working_on(job)
3838
@worker.unregister_worker
3939
assert_equal 1, Resque::Failure.count
40+
assert_equal('Resque::DirtyExit', Resque::Failure.all['exception'])
41+
end
42+
43+
test "fails uncompleted jobs with worker exception on exit" do
44+
job = Resque::Job.new(:jobs, {'class' => 'GoodJob', 'args' => "blah"})
45+
@worker.working_on(job)
46+
@worker.unregister_worker(StandardError.new)
47+
assert_equal 1, Resque::Failure.count
48+
assert_equal('StandardError', Resque::Failure.all['exception'])
4049
end
4150

4251
class ::SimpleJobWithFailureHandling

0 commit comments

Comments
 (0)