Permalink
Browse files

Merge pull request #546 from jakemack/resque

---

No hooks should be ran twice, otherwise you cant write a hook that modifies a value or the value will be modified twice.
  • Loading branch information...
2 parents baf7204 + 6a22519 commit c3627fce0605db736450a2c5073dc6aff14c5ded @hone hone committed May 31, 2012
Showing with 33 additions and 7 deletions.
  1. +8 −5 lib/resque/job.rb
  2. +25 −2 test/worker_test.rb
View
@@ -32,6 +32,7 @@ class Job
def initialize(queue, payload)
@queue = queue
@payload = payload
+ @failure_hooks_already_ran = false
end
# Creates a job by placing it on a queue. Expects a string queue
@@ -210,14 +211,16 @@ def after_hooks
@after_hooks ||= Plugin.after_hooks(payload_class)
end
- def failure_hooks
+ 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) }
+ begin
+ failure_hooks.each { |hook| payload_class.send(hook, exception, *Array.wrap(args)) } unless @failure_hooks_already_ran
+ ensure
+ @failure_hooks_already_ran = true
+ end
end
-
end
end
View
@@ -43,7 +43,7 @@ class ::SimpleJobWithFailureHandling
def self.on_failure_record_failure(exception, *job_args)
@@exception = exception
end
-
+
def self.exception
@@exception
end
@@ -57,6 +57,29 @@ def self.exception
assert(SimpleJobWithFailureHandling.exception.kind_of?(Resque::DirtyExit))
end
+ class ::SimpleFailingJob
+ @@exception_count = 0
+
+ def self.on_failure_record_failure(exception, *job_args)
+ @@exception_count += 1
+ end
+
+ def self.exception_count
+ @@exception_count
+ end
+
+ def self.perform
+ raise Exception.new
+ end
+ end
+
+ test "only calls failure hook once on exception" do
+ job = Resque::Job.new(:jobs, {'class' => 'SimpleFailingJob', 'args' => ""})
+ @worker.perform(job)
+ assert_equal 1, Resque::Failure.count
+ assert_equal 1, SimpleFailingJob.exception_count
+ end
+
test "can peek at failed jobs" do
10.times { Resque::Job.create(:jobs, BadJob) }
@worker.work(0)
@@ -388,7 +411,7 @@ def self.exception
test "returns PID of running process" do
assert_equal @worker.to_s.split(":")[1].to_i, @worker.pid
end
-
+
test "requeue failed queue" do
queue = 'good_job'
Resque::Failure.create(:exception => Exception.new, :worker => Resque::Worker.new(queue), :queue => queue, :payload => {'class' => 'GoodJob'})

0 comments on commit c3627fc

Please sign in to comment.