Skip to content

Commit c3627fc

Browse files
committed
Merge pull request resque#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.
2 parents baf7204 + 6a22519 commit c3627fc

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/resque/job.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Job
3232
def initialize(queue, payload)
3333
@queue = queue
3434
@payload = payload
35+
@failure_hooks_already_ran = false
3536
end
3637

3738
# Creates a job by placing it on a queue. Expects a string queue
@@ -210,14 +211,16 @@ def after_hooks
210211
@after_hooks ||= Plugin.after_hooks(payload_class)
211212
end
212213

213-
def failure_hooks
214+
def failure_hooks
214215
@failure_hooks ||= Plugin.failure_hooks(payload_class)
215216
end
216-
217+
217218
def run_failure_hooks(exception)
218-
job_args = args || []
219-
failure_hooks.each { |hook| payload_class.send(hook, exception, *job_args) }
219+
begin
220+
failure_hooks.each { |hook| payload_class.send(hook, exception, *Array.wrap(args)) } unless @failure_hooks_already_ran
221+
ensure
222+
@failure_hooks_already_ran = true
223+
end
220224
end
221-
222225
end
223226
end

test/worker_test.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ::SimpleJobWithFailureHandling
4343
def self.on_failure_record_failure(exception, *job_args)
4444
@@exception = exception
4545
end
46-
46+
4747
def self.exception
4848
@@exception
4949
end
@@ -57,6 +57,29 @@ def self.exception
5757
assert(SimpleJobWithFailureHandling.exception.kind_of?(Resque::DirtyExit))
5858
end
5959

60+
class ::SimpleFailingJob
61+
@@exception_count = 0
62+
63+
def self.on_failure_record_failure(exception, *job_args)
64+
@@exception_count += 1
65+
end
66+
67+
def self.exception_count
68+
@@exception_count
69+
end
70+
71+
def self.perform
72+
raise Exception.new
73+
end
74+
end
75+
76+
test "only calls failure hook once on exception" do
77+
job = Resque::Job.new(:jobs, {'class' => 'SimpleFailingJob', 'args' => ""})
78+
@worker.perform(job)
79+
assert_equal 1, Resque::Failure.count
80+
assert_equal 1, SimpleFailingJob.exception_count
81+
end
82+
6083
test "can peek at failed jobs" do
6184
10.times { Resque::Job.create(:jobs, BadJob) }
6285
@worker.work(0)
@@ -388,7 +411,7 @@ def self.exception
388411
test "returns PID of running process" do
389412
assert_equal @worker.to_s.split(":")[1].to_i, @worker.pid
390413
end
391-
414+
392415
test "requeue failed queue" do
393416
queue = 'good_job'
394417
Resque::Failure.create(:exception => Exception.new, :worker => Resque::Worker.new(queue), :queue => queue, :payload => {'class' => 'GoodJob'})

0 commit comments

Comments
 (0)