Permalink
Browse files

after_enqueue hook

  • Loading branch information...
1 parent f15aedb commit 193289ba94c3c6b31eee8a5beff89b7ceb5734db @darkhelmet darkhelmet committed with defunkt Jul 30, 2010
Showing with 31 additions and 1 deletion.
  1. +5 −1 lib/resque/job.rb
  2. +5 −0 lib/resque/plugin.rb
  3. +21 −0 test/job_hooks_test.rb
View
@@ -48,7 +48,11 @@ def self.create(queue, klass, *args)
raise NoClassError.new("Jobs must be given a class.")
end
- Resque.push(queue, :class => klass.to_s, :args => args)
+ ret = Resque.push(queue, :class => klass.to_s, :args => args)
+ Plugin.after_enqueue_hooks(klass).each do |hook|
+ klass.send(hook, *args)
+ end
+ ret
end
# Removes a job from a queue. Expects a string queue name, a
View
@@ -42,5 +42,10 @@ def after_hooks(job)
def failure_hooks(job)
job.methods.grep(/^on_failure/).sort
end
+
+ # Given an object, returns a list `after_enqueue` hook names.
+ def after_enqueue_hooks(job)
+ job.methods.grep(/^after_enqueue/).sort
+ end
end
end
View
@@ -228,6 +228,27 @@ def self.on_failure_record_failure(exception, history)
end
end
+context "Resque::Job after_enqueue" do
+ include PerformJob
+
+ class ::AfterEnqueueJob
+ def self.after_enqueue_record_history(history)
+ history << :after_enqueue
+ end
+
+ def self.perform(history)
+ end
+ end
+
+ test "the after enqueue hook should run" do
+ history = []
+ @worker = Resque::Worker.new(:jobs)
+ Resque::Job.create(:jobs, AfterEnqueueJob, history)
+ @worker.work(0)
+ assert_equal history, [:after_enqueue], "after_enqueue was not run"
+ end
+end
+
context "Resque::Job all hooks" do
include PerformJob

0 comments on commit 193289b

Please sign in to comment.