Skip to content

Commit

Permalink
Pass exceptions raised by the worker into the Failure backend
Browse files Browse the repository at this point in the history
Conflicts:

	test/worker_test.rb
  • Loading branch information
trevorturk authored and steveklabnik committed Sep 24, 2012
1 parent 5057366 commit 515887a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/resque/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ def work(interval = 5.0, &block)
end
end

ensure
unregister_worker
rescue Exception => exception
unregister_worker(exception)
end

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

# Unregisters ourself as a worker. Useful when shutting down.
def unregister_worker
def unregister_worker(exception = nil)
# If we're still processing a job, make sure it gets logged as a
# failure.
if (hash = processing) && !hash.empty?
job = Job.new(hash['queue'], hash['payload'])
# Ensure the proper worker is attached to this job, even if
# it's not the precise instance that died.
job.worker = self
job.fail(DirtyExit.new)
job.fail(exception || DirtyExit.new)
end

redis.srem(:workers, self)
Expand Down
11 changes: 10 additions & 1 deletion test/worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@
end
end

it "fails uncompleted jobs on exit" do
it "fails uncompleted jobs with DirtyExit by default on exit" do
job = Resque::Job.new(:jobs, {'class' => 'GoodJob', 'args' => "blah"})
@worker.working_on(job)
@worker.unregister_worker
assert_equal 1, Resque::Failure.count
assert_equal('Resque::DirtyExit', Resque::Failure.all['exception'])
end

test "fails uncompleted jobs with worker exception on exit" do
job = Resque::Job.new(:jobs, {'class' => 'GoodJob', 'args' => "blah"})
@worker.working_on(job)
@worker.unregister_worker(StandardError.new)
assert_equal 1, Resque::Failure.count
assert_equal('StandardError', Resque::Failure.all['exception'])
end

class ::SimpleJobWithFailureHandling
Expand Down

0 comments on commit 515887a

Please sign in to comment.