Skip to content
Browse files

Notify jobs when they're killed due to DirtyExit

This will lead to being able to cleanup locks in the resque-lock plugin :)
  • Loading branch information...
1 parent 1f7411d commit 7113b0df5314e5464a440512b77ca90d04ac5f0c @raykrueger raykrueger committed Sep 17, 2011
Showing with 43 additions and 7 deletions.
  1. +24 −6 lib/resque/job.rb
  2. +19 −1 test/worker_test.rb
View
30 lib/resque/job.rb
@@ -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.
@@ -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
@@ -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,
@@ -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
View
20 test/worker_test.rb
@@ -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)

0 comments on commit 7113b0d

Please sign in to comment.
Something went wrong with that request. Please try again.