Skip to content

Commit

Permalink
Call after_enqueue hook in Resque.enqueue. Fixes resque#169
Browse files Browse the repository at this point in the history
  • Loading branch information
defunkt committed Mar 17, 2011
1 parent 200ec0f commit b8220b2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/resque.rb
Expand Up @@ -215,6 +215,10 @@ def watch_queue(queue)
# This method is considered part of the `stable` API.
def enqueue(klass, *args)
Job.create(queue_from_class(klass), klass, *args)

Plugin.after_enqueue_hooks(klass).each do |hook|
klass.send(hook, *args)
end
end

# This method can be used to conveniently remove a job from a queue.
Expand Down
10 changes: 2 additions & 8 deletions lib/resque/job.rb
Expand Up @@ -43,16 +43,10 @@ def self.create(queue, klass, *args)
validate!(klass, queue)

if Resque.inline?
ret = constantize(klass).perform(*decode(encode(args)))
constantize(klass).perform(*decode(encode(args)))
else
ret = Resque.push(queue, :class => klass.to_s, :args => args)
Resque.push(queue, :class => klass.to_s, :args => args)
end

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
Expand Down
3 changes: 2 additions & 1 deletion test/job_hooks_test.rb
Expand Up @@ -232,6 +232,7 @@ def self.on_failure_record_failure(exception, history)
include PerformJob

class ::AfterEnqueueJob
@queue = :jobs
def self.after_enqueue_record_history(history)
history << :after_enqueue
end
Expand All @@ -243,7 +244,7 @@ def self.perform(history)
test "the after enqueue hook should run" do
history = []
@worker = Resque::Worker.new(:jobs)
Resque::Job.create(:jobs, 'AfterEnqueueJob', history)
Resque.enqueue(AfterEnqueueJob, history)
@worker.work(0)
assert_equal history, [:after_enqueue], "after_enqueue was not run"
end
Expand Down

0 comments on commit b8220b2

Please sign in to comment.