Skip to content

Commit aa24424

Browse files
jhuckabeedefunkt
authored andcommitted
Added a before_enqueue hook.
1 parent 40f8b45 commit aa24424

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

docs/HOOKS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ An unnamed hook (`before_perform`) will be executed first.
6666

6767
The available hooks are:
6868

69+
* `before _enqueue`: Called with the job args before a job is placed on the queue.
70+
If the hook returns `false`, the job will not be placed on the queue.
71+
6972
* `after_enqueue`: Called with the job args after a job is placed on the queue.
7073
Any exception raised propagates up to the code which queued the job.
7174

lib/resque.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ def watch_queue(queue)
224224
#
225225
# This method is considered part of the `stable` API.
226226
def enqueue(klass, *args)
227+
# Perform before_enqueue hooks. Don't perform enqueue if any hook returns false
228+
before_hooks = Plugin.before_enqueue_hooks(klass).collect do |hook|
229+
klass.send(hook, *args)
230+
end
231+
return if before_hooks.any? { |result| result == false }
232+
227233
Job.create(queue_from_class(klass), klass, *args)
228234

229235
Plugin.after_enqueue_hooks(klass).each do |hook|

lib/resque/plugin.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,10 @@ def failure_hooks(job)
4747
def after_enqueue_hooks(job)
4848
job.methods.grep(/^after_enqueue/).sort
4949
end
50+
51+
# Given an object, returns a list `before_enqueue` hook names.
52+
def before_enqueue_hooks(job)
53+
job.methods.grep(/^before_enqueue/).sort
54+
end
5055
end
5156
end

test/job_hooks_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,46 @@ def self.perform(history)
250250
end
251251
end
252252

253+
254+
context "Resque::Job before_enqueue" do
255+
include PerformJob
256+
257+
class ::BeforeEnqueueJob
258+
@queue = :jobs
259+
def self.before_enqueue_record_history(history)
260+
history << :before_enqueue
261+
end
262+
263+
def self.perform(history)
264+
end
265+
end
266+
267+
class ::BeforeEnqueueJobAbort
268+
@queue = :jobs
269+
def self.before_enqueue_abort(history)
270+
false
271+
end
272+
273+
def self.perform(history)
274+
end
275+
end
276+
277+
test "the before enqueue hook should run" do
278+
history = []
279+
@worker = Resque::Worker.new(:jobs)
280+
Resque.enqueue(BeforeEnqueueJob, history)
281+
@worker.work(0)
282+
assert_equal history, [:before_enqueue], "before_enqueue was not run"
283+
end
284+
285+
test "a before enqueue hook that returns false should prevent the job from getting queued" do
286+
history = []
287+
@worker = Resque::Worker.new(:jobs)
288+
Resque.enqueue(BeforeEnqueueJobAbort, history)
289+
assert_equal 0, Resque.size(:jobs)
290+
end
291+
end
292+
253293
context "Resque::Job all hooks" do
254294
include PerformJob
255295

0 commit comments

Comments
 (0)