Skip to content

Commit 0e7c089

Browse files
jonhymanhone
authored andcommitted
Fixes issue resque#406: supports calling perform hooks when Resque.inline = true.
1 parent 46d2466 commit 0e7c089

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/resque/job.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def self.create(queue, klass, *args)
4444
Resque.validate(klass, queue)
4545

4646
if Resque.inline?
47-
constantize(klass).perform(*decode(encode(args)))
47+
# Instantiating a Resque::Job and calling perform on it so callbacks run
48+
new(:inline, {'class' => klass, 'args' => args}).perform
4849
else
4950
Resque.push(queue, :class => klass.to_s, :args => args)
5051
end

test/job_hooks_test.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,45 @@ def self.on_failure_record_history(exception, history)
420420
"oh no"
421421
]
422422
end
423+
424+
class ::CallbacksInline
425+
@queue = :callbacks_inline
426+
427+
def self.before_perform_record_history(history, count)
428+
history << :before_perform
429+
count['count'] += 1
430+
end
431+
432+
def self.after_perform_record_history(history, count)
433+
history << :after_perform
434+
count['count'] += 1
435+
end
436+
437+
def self.around_perform_record_history(history, count)
438+
history << :start_around_perform
439+
count['count'] += 1
440+
yield
441+
history << :finish_around_perform
442+
count['count'] += 1
443+
end
444+
445+
def self.perform(history, count)
446+
history << :perform
447+
$history = history
448+
$count = count
449+
end
450+
end
451+
452+
test "it runs callbacks when inline is true" do
453+
begin
454+
Resque.inline = true
455+
# Sending down two parameters that can be passed and updated by reference
456+
result = Resque.enqueue(CallbacksInline, [], {'count' => 0})
457+
assert_equal true, result, "perform returned true"
458+
assert_equal $history, [:before_perform, :start_around_perform, :perform, :finish_around_perform, :after_perform]
459+
assert_equal 4, $count['count']
460+
ensure
461+
Resque.inline = false
462+
end
463+
end
423464
end

0 commit comments

Comments
 (0)