Skip to content

Commit

Permalink
Notify jobs when they're killed due to DirtyExit
Browse files Browse the repository at this point in the history
This will lead to being able to cleanup locks in the resque-lock plugin :)
  • Loading branch information
raykrueger committed Sep 17, 2011
1 parent 1f7411d commit 7113b0d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
30 changes: 24 additions & 6 deletions lib/resque/job.rb
Expand Up @@ -106,11 +106,6 @@ def perform
job_args = args || []
job_was_performed = false

before_hooks = Plugin.before_hooks(job)
around_hooks = Plugin.around_hooks(job)
after_hooks = Plugin.after_hooks(job)
failure_hooks = Plugin.failure_hooks(job)

begin
# Execute before_perform hook. Abort the job gracefully if
# Resque::DontPerform is raised.
Expand Down Expand Up @@ -158,7 +153,7 @@ def perform
# If an exception occurs during the job execution, look for an
# on_failure hook then re-raise.
rescue Object => e
failure_hooks.each { |hook| job.send(hook, e, *job_args) }
run_failure_hooks(e)
raise e
end
end
Expand All @@ -176,6 +171,7 @@ def args
# Given an exception object, hands off the needed parameters to
# the Failure module.
def fail(exception)
run_failure_hooks(exception)
Failure.create \
:payload => payload,
:exception => exception,
Expand All @@ -201,5 +197,27 @@ def ==(other)
payload_class == other.payload_class &&
args == other.args
end

def before_hooks
@before_hooks ||= Plugin.before_hooks(payload_class)
end

def around_hooks
@around_hooks ||= Plugin.around_hooks(payload_class)
end

def after_hooks
@after_hooks ||= Plugin.after_hooks(payload_class)
end

def failure_hooks
@failure_hooks ||= Plugin.failure_hooks(payload_class)
end

def run_failure_hooks(exception)
job_args = args || []
failure_hooks.each { |hook| payload_class.send(hook, exception, *job_args) }
end

end
end
20 changes: 19 additions & 1 deletion test/worker_test.rb
Expand Up @@ -33,12 +33,30 @@
end

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

class ::SimpleJobWithFailureHandling
def self.on_failure_record_failure(exception)
@@exception = exception
end

def self.exception
@@exception
end
end

test "fails uncompleted jobs on exit, and calls failure hook" do
job = Resque::Job.new(:jobs, {'class' => 'SimpleJobWithFailureHandling', 'args' => ""})
@worker.working_on(job)
@worker.unregister_worker
assert_equal 1, Resque::Failure.count
assert(SimpleJobWithFailureHandling.exception.kind_of?(Resque::DirtyExit))
end

test "can peek at failed jobs" do
10.times { Resque::Job.create(:jobs, BadJob) }
@worker.work(0)
Expand Down

0 comments on commit 7113b0d

Please sign in to comment.