skip to content

In a Rails job class, how do queue_as, queue_with_priority and set(wait:, wait_until:, queue:, priority:) decide where and when an Active Job runs?

level: middleimportance: should knowfreq 45%

answer

  1. class defaults versus one enqueue
  2. queue_as defaults to "default"
  3. wait: becomes scheduled_at
  4. priority meaning is adapter-defined
  5. set(wait:) then perform_now runs now

basics

~20 s

queue_as and queue_with_priority set a job class's default queue and priority; set(...) overrides them for one enqueue and adds a delay through wait: or wait_until:, which becomes scheduled_at. How priority is honoured is up to the adapter.

solid answer

~40 s

`queue_as :certificates` fixes the class's queue (otherwise `"default"`), and accepts a block evaluated against the job when the queue depends on its arguments; `config.active_job.queue_name_prefix` is joined on with `_`. `queue_with_priority` sets a default priority the same way. `GenerateCertificateJob.set(wait: 10.minutes, queue: :urgent, priority: 5)` returns a configured job for one call: `wait:` and `wait_until:` set `scheduled_at`, so the adapter receives `enqueue_at` instead of `enqueue`, `queue:` is prefixed like `queue_as`, and `priority:` is stored as an integer. Active Job does not define what a priority number means; Solid Queue treats a smaller number as more urgent but only within a queue. `set(wait:)` followed by `perform_now` runs immediately: the delay only matters to an adapter.

go deeper

for a junior

Know queue_as for the default queue and set(wait:) for a delayed enqueue, with the default queue named default.

for a middle

Explain how set builds a configured job, how wait becomes scheduled_at and enqueue_at, and that priority semantics belong to the adapter.

for a senior

Design queues around worker capacity, know Solid Queue orders priority only within a queue, and spot delays used to hide transaction races.

for a principal

Decide how many queues and priorities a system really needs and how they map to workers before priorities turn into an arms race.

## Two layers: class defaults and per-enqueue options Every Active Job instance carries a **queue name**, a **priority** and an optional **`scheduled_at`**. They are filled in two layers: - **Class-level defaults**, declared once in the job class with `queue_as` and `queue_with_priority`. - **Per-enqueue overrides**, passed through `set(...)` for one call. The adapter then decides what those values mean in practice. Active Job only carries them. ## `queue_as`: the default queue ```ruby class GenerateCertificateJob < ApplicationJob queue_as :certificates def perform(enrollment) CertificatePdf.new(enrollment).attach! end end ``` - Without `queue_as`, a job uses `ActiveJob::Base.default_queue_name`, which is `"default"`. - `queue_as` also takes a **block**, evaluated against the job instance, so it can read `arguments`. The returned name is used for that job. - `config.active_job.queue_name_prefix` (often `Rails.env`) is joined to the name with `queue_name_delimiter`, `"_"` by default: `production_certificates`. A job can opt out with `self.queue_name_prefix = nil`. ## `queue_with_priority`: the default priority `queue_with_priority 10` (or a block) sets the class's default priority, which is `nil` unless configured. Active Job does not say whether 0 beats 10; its guide only encourages adapters to treat a **lower number as more important**. Solid Queue does: priority 0 is its default, smaller numbers are picked first, and priority only orders jobs **within a queue**, because the order of queues a worker polls takes precedence. ## `set(...)`: one enqueue, different options `set` returns an `ActiveJob::ConfiguredJob` that remembers its options until you call `perform_later` (or `perform_now`) on it: | Option | What it does | |---|---| | `wait:` | a duration; `scheduled_at` becomes now plus that duration | | `wait_until:` | a time; `scheduled_at` becomes that time | | `queue:` | overrides the queue, with the same prefix rules as `queue_as` | | `priority:` | overrides the priority, converted with `to_i` | ```ruby # Give the grading pipeline ten minutes to settle before rendering GenerateCertificateJob.set(wait: 10.minutes).perform_later(enrollment) # Premium courses jump the line on the same queue GenerateCertificateJob.set(priority: 0).perform_later(enrollment) if course.premium? # Render all certificates when the cohort officially ends GenerateCertificateJob.set(wait_until: cohort.ends_at).perform_later(enrollment) ``` When `scheduled_at` is set, Active Job calls the adapter's `enqueue_at(job, timestamp)` instead of `enqueue(job)`. The **delay is the adapter's job**: Solid Queue keeps the row as a scheduled execution until it is due; `:async` keeps it in a timer in memory, where a restart loses it. The same options are accepted by an instance: `job.enqueue(wait: 5.minutes)`, or `GenerateCertificateJob.new(enrollment).set(wait: 1.hour)` before handing a batch to `ActiveJob.perform_all_later`. ## Where the values end up Each value is copied into the job instance and then into its serialized payload as `queue_name`, `priority` and `scheduled_at`, which is how the adapter and any dashboard see them. You can read them back from the job `perform_later` returns: ```ruby job = GenerateCertificateJob.set(wait: 10.minutes, priority: 5).perform_later(enrollment) job.queue_name # => "certificates" job.priority # => 5 job.scheduled_at # => about ten minutes from now ``` Because `set` only touches that one job, the class defaults stay untouched for every other caller. ## Traps worth naming 1. **`set(wait:)` with `perform_now`.** `ConfiguredJob#perform_now` applies the options and then runs `perform` immediately. Nothing waits; there is no adapter in the path to honour the delay. 2. **Delays are "not before", not "exactly at".** A scheduled job becomes eligible at `scheduled_at`; when it actually starts depends on how busy the workers are. 3. **Priority across queues.** Giving one job priority 0 does not make it overtake jobs in a queue that its workers poll first. 4. **Priority numbers are backend-specific.** Moving from one adapter to another can invert the meaning, so check the backend's documentation before relying on them. ## When to use which - Use **`queue_as`** to route a whole class of work to workers sized for it, such as a `certificates` queue served by a worker with few threads because PDF rendering is memory-heavy. - Use **`set(queue:)`** for the exception, not the rule: if every call passes the same queue, it belongs in `queue_as`. - Use **`set(wait:)` / `set(wait_until:)`** for real business delays; do not use them to paper over a race with an uncommitted transaction, which is solved by enqueueing after commit.

  • How do you enqueue certificate jobs for an entire cohort in one call, and what do you give up?
    Build instances and pass them to `ActiveJob.perform_all_later(enrollments.map { |e| GenerateCertificateJob.new(e) })`; each instance can carry its own `set(...)` options. Adapters with `enqueue_all`, such as Solid Queue, insert them in bulk. It returns `nil` rather than jobs, and it does not run the individual `before_enqueue`/`around_enqueue`/`after_enqueue` callbacks, so check `successfully_enqueued?` on the instances instead.
  • What does a block passed to queue_as let you do?
    It is evaluated against the job instance when the queue name is first needed, so it can read `arguments`. For example, certificates for enterprise customers can go to a separate queue served by dedicated workers. The block's return value is still prefixed with `queue_name_prefix` like a static name.

  • set(wait: 1.hour).perform_now schedules the job for an hour later
  • Active Job guarantees a lower priority number always runs first, on every backend
  • A priority-0 job in one queue overtakes jobs in queues the worker polls first
  • queue_as is ignored once config.active_job.queue_name_prefix is set
  • wait: makes the job start exactly at that moment