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.
66
66
67
67
The available hooks are:
68
68
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
+
69
72
* ` after_enqueue ` : Called with the job args after a job is placed on the queue.
70
73
Any exception raised propagates up to the code which queued the job.
71
74
Original file line number Diff line number Diff line change @@ -224,6 +224,12 @@ def watch_queue(queue)
224
224
#
225
225
# This method is considered part of the `stable` API.
226
226
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
+
227
233
Job . create ( queue_from_class ( klass ) , klass , *args )
228
234
229
235
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)
47
47
def after_enqueue_hooks ( job )
48
48
job . methods . grep ( /^after_enqueue/ ) . sort
49
49
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
50
55
end
51
56
end
Original file line number Diff line number Diff line change @@ -250,6 +250,46 @@ def self.perform(history)
250
250
end
251
251
end
252
252
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
+
253
293
context "Resque::Job all hooks" do
254
294
include PerformJob
255
295
You can’t perform that action at this time.
0 commit comments