Skip to content

Commit 193289b

Browse files
darkhelmetdefunkt
authored andcommitted
after_enqueue hook
1 parent f15aedb commit 193289b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/resque/job.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ def self.create(queue, klass, *args)
4848
raise NoClassError.new("Jobs must be given a class.")
4949
end
5050

51-
Resque.push(queue, :class => klass.to_s, :args => args)
51+
ret = Resque.push(queue, :class => klass.to_s, :args => args)
52+
Plugin.after_enqueue_hooks(klass).each do |hook|
53+
klass.send(hook, *args)
54+
end
55+
ret
5256
end
5357

5458
# Removes a job from a queue. Expects a string queue name, a

lib/resque/plugin.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@ def after_hooks(job)
4242
def failure_hooks(job)
4343
job.methods.grep(/^on_failure/).sort
4444
end
45+
46+
# Given an object, returns a list `after_enqueue` hook names.
47+
def after_enqueue_hooks(job)
48+
job.methods.grep(/^after_enqueue/).sort
49+
end
4550
end
4651
end

test/job_hooks_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,27 @@ def self.on_failure_record_failure(exception, history)
228228
end
229229
end
230230

231+
context "Resque::Job after_enqueue" do
232+
include PerformJob
233+
234+
class ::AfterEnqueueJob
235+
def self.after_enqueue_record_history(history)
236+
history << :after_enqueue
237+
end
238+
239+
def self.perform(history)
240+
end
241+
end
242+
243+
test "the after enqueue hook should run" do
244+
history = []
245+
@worker = Resque::Worker.new(:jobs)
246+
Resque::Job.create(:jobs, AfterEnqueueJob, history)
247+
@worker.work(0)
248+
assert_equal history, [:after_enqueue], "after_enqueue was not run"
249+
end
250+
end
251+
231252
context "Resque::Job all hooks" do
232253
include PerformJob
233254

0 commit comments

Comments
 (0)