File tree Expand file tree Collapse file tree 4 files changed +54
-0
lines changed
Expand file tree Collapse file tree 4 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,9 @@ An unnamed hook (`before_perform`) will be executed first.
6666
6767The 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
Original file line number Diff line number Diff 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 |
Original file line number Diff line number Diff 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
5156end
Original file line number Diff line number Diff line change @@ -250,6 +250,46 @@ def self.perform(history)
250250 end
251251end
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+
253293context "Resque::Job all hooks" do
254294 include PerformJob
255295
You can’t perform that action at this time.
0 commit comments