Skip to content

Commit 39b0692

Browse files
authored
Merge pull request rails#50817 from joshuay03/add-queue-with-priority-to-activejob-docs
[Docs] Add section on Queue Priority to `ActiveJob` docs
2 parents 95bb5cc + 9691cf6 commit 39b0692

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

guides/source/active_job_basics.md

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ What is Active Job?
2121
Active Job is a framework for declaring jobs and making them run on a variety
2222
of queuing backends. These jobs can be everything from regularly scheduled
2323
clean-ups, to billing charges, to mailings. Anything that can be chopped up
24-
into small units of work and run in parallel, really.
24+
into small units of work and run in parallel.
2525

2626

2727
The Purpose of Active Job
@@ -196,7 +196,7 @@ Here is a noncomprehensive list of documentation:
196196
Queues
197197
------
198198

199-
Most of the adapters support multiple queues. With Active Job you can schedule
199+
Most adapters support multiple queues. With Active Job you can schedule
200200
the job to run on a specific queue using [`queue_as`][]:
201201

202202
```ruby
@@ -268,13 +268,6 @@ end
268268
# on your staging environment
269269
```
270270

271-
If you want more control on what queue a job will be run you can pass a `:queue`
272-
option to `set`:
273-
274-
```ruby
275-
MyJob.set(queue: :another_queue).perform_later(record)
276-
```
277-
278271
To control the queue from the job level you can pass a block to `queue_as`. The
279272
block will be executed in the job context (so it can access `self.arguments`),
280273
and it must return the queue name:
@@ -300,13 +293,67 @@ end
300293
ProcessVideoJob.perform_later(Video.last)
301294
```
302295

296+
If you want more control on what queue a job will be run you can pass a `:queue`
297+
option to `set`:
298+
299+
```ruby
300+
MyJob.set(queue: :another_queue).perform_later(record)
301+
```
302+
303303
NOTE: Make sure your queuing backend "listens" on your queue name. For some
304304
backends you need to specify the queues to listen to.
305305

306306
[`config.active_job.queue_name_delimiter`]: configuring.html#config-active-job-queue-name-delimiter
307307
[`config.active_job.queue_name_prefix`]: configuring.html#config-active-job-queue-name-prefix
308308
[`queue_as`]: https://api.rubyonrails.org/classes/ActiveJob/QueueName/ClassMethods.html#method-i-queue_as
309309

310+
Priority
311+
--------------
312+
313+
Some adapters support priorities at the job level, where jobs can be prioritised relative to others in the queue or across all queues.
314+
315+
You can schedule a job to run with a specific priority using [`queue_with_priority`][]:
316+
317+
```ruby
318+
class GuestsCleanupJob < ApplicationJob
319+
queue_with_priority 10
320+
# ...
321+
end
322+
```
323+
324+
Note that this will not have any effect with adapters that do not support priorities.
325+
326+
Similar to `queue_as`, you can also pass a block to `queue_with_priority` to be evaluated in the job context:
327+
328+
```ruby
329+
class ProcessVideoJob < ApplicationJob
330+
queue_with_priority do
331+
video = self.arguments.first
332+
if video.owner.premium?
333+
0
334+
else
335+
10
336+
end
337+
end
338+
339+
def perform(video)
340+
# Process video
341+
end
342+
end
343+
```
344+
345+
```ruby
346+
ProcessVideoJob.perform_later(Video.last)
347+
```
348+
349+
You can also pass a `:priority` option to `set`:
350+
351+
```ruby
352+
MyJob.set(priority: 50).perform_later(record)
353+
```
354+
355+
[`queue_with_priority`]: https://api.rubyonrails.org/classes/ActiveJob/QueuePriority/ClassMethods.html#method-i-queue_with_priority
356+
310357
Callbacks
311358
---------
312359

0 commit comments

Comments
 (0)