Navigation Menu

Skip to content

Commit

Permalink
Make Resque.enqueue return something deterministic
Browse files Browse the repository at this point in the history
Currently Resque.enqueue returns an empty [] or the results of the
after_enqueue_hooks that may have been run. If the before_enqueue_hook rejected
the operation we get back a nil.

This commit simply changes enqueue to return nil on rejection and true if the
job is actually queued.
  • Loading branch information
raykrueger committed Sep 9, 2011
1 parent 192d347 commit 1f7411d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/resque.rb
Expand Up @@ -228,13 +228,15 @@ def enqueue(klass, *args)
before_hooks = Plugin.before_enqueue_hooks(klass).collect do |hook|
klass.send(hook, *args)
end
return if before_hooks.any? { |result| result == false }
return nil if before_hooks.any? { |result| result == false }

Job.create(queue_from_class(klass), klass, *args)
job = Job.create(queue_from_class(klass), klass, *args)

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

return true
end

# This method can be used to conveniently remove a job from a queue.
Expand Down
4 changes: 2 additions & 2 deletions test/job_hooks_test.rb
Expand Up @@ -277,15 +277,15 @@ def self.perform(history)
test "the before enqueue hook should run" do
history = []
@worker = Resque::Worker.new(:jobs)
Resque.enqueue(BeforeEnqueueJob, history)
assert Resque.enqueue(BeforeEnqueueJob, history)
@worker.work(0)
assert_equal history, [:before_enqueue], "before_enqueue was not run"
end

test "a before enqueue hook that returns false should prevent the job from getting queued" do
history = []
@worker = Resque::Worker.new(:jobs)
Resque.enqueue(BeforeEnqueueJobAbort, history)
assert_nil Resque.enqueue(BeforeEnqueueJobAbort, history)
assert_equal 0, Resque.size(:jobs)
end
end
Expand Down

0 comments on commit 1f7411d

Please sign in to comment.