skip to content

With Solid Queue, how do you declare a nightly job in config/recurring.yml, and which process enqueues it when it is due?

level: middleimportance: should knowfreq 40%

answer

  1. keyed by environment, then task key
  2. class: or command:, plus schedule:
  3. Fugit parses the schedule
  4. the scheduler process enqueues it
  5. unique task key and run_at

basics

~20 s

Add a task under the environment key with class: (or command:), optional args:, queue: and priority:, and a schedule: such as every day at 6am. Solid Queue's scheduler process, started by the supervisor, enqueues the job each time it falls due.

solid answer

~50 s

`config/recurring.yml` is keyed by environment, then by task key. Each task names a job `class:` (or a Ruby `command:` string, evaluated inside `SolidQueue::RecurringJob` on the `solid_queue_recurring` queue) and a `schedule:` that Fugit parses, so `every day at 6am` and `0 6 * * *` both work, in `config.time_zone` unless the schedule names a zone. Optional keys are `args:`, `queue:`, `priority:` and `description:`. The supervisor starts a **scheduler** process whenever tasks exist; it enqueues each task's job at its time, and workers run it like any job. Several schedulers can share one schedule safely: each run is recorded in `solid_queue_recurring_executions` under a unique index on task key and run time, as long as finished jobs are preserved (the default). The installer ships one production task, `clear_solid_queue_finished_jobs`, every hour at minute 12. `bin/jobs check` validates the file and `SOLID_QUEUE_SKIP_RECURRING` turns tasks off.

go deeper

for a junior

Know that recurring.yml declares cron-like tasks with a class and a schedule, and that Solid Queue enqueues them for you.

for a middle

Explain class versus command tasks, Fugit schedules and time zones, the scheduler process, and the generated clean-up task.

for a senior

Run several schedulers safely, keep finished-job clean-up working, and debug late tasks by looking at queues and worker capacity.

for a principal

Decide which timed work belongs in the app's schedule and which needs a platform-level scheduler with its own guarantees.

## Cron, without cron A newsletter platform needs two kinds of timed work: a **nightly digest** for every newsletter, and an **hourly clean-up** that purges bounced subscribers. With Solid Queue there is no system crontab and no separate scheduler service: recurring tasks are declared in `config/recurring.yml` and enqueued by one of Solid Queue's own processes. ## The file ```yaml production: nightly_digests: class: BuildDigestsJob args: [ { edition: "daily" } ] queue: digests schedule: every day at 6am description: "Queue one digest per newsletter" purge_bounced_subscribers: command: "Subscriber.bounced.where(bounced_at: ...30.days.ago).delete_all" priority: 5 schedule: every hour at minute 30 clear_solid_queue_finished_jobs: command: "SolidQueue::Job.clear_finished_in_batches(sleep_between_batches: 0.3)" schedule: every hour at minute 12 ``` 1. The **top level is the environment**. Tasks under `production:` do not run in development or staging. 2. Each **task key** (`nightly_digests`) identifies the task; Solid Queue uses it to record runs. 3. A task names either a **`class:`**, an Active Job class enqueued with `args:`, or a **`command:`**, a Ruby string evaluated inside a `SolidQueue::RecurringJob` placed on the `solid_queue_recurring` queue. 4. **`schedule:`** is parsed by **Fugit**, so natural forms (`every day at 6am`, `every hour at minute 30`) and cron syntax (`0 6 * * *`) both work. Without an explicit zone it is read in the application's `config.time_zone`; `config.solid_queue.time_zone` changes that. 5. **`args:`, `queue:`, `priority:` and `description:`** are optional; `queue:` overrides the class's `queue_as` for these runs. ## The keys at a glance | Key | Required | Meaning | |---|---|---| | `class` | one of `class` / `command` | Active Job class to enqueue | | `command` | one of `class` / `command` | Ruby string run by `SolidQueue::RecurringJob` | | `schedule` | yes | Fugit expression: natural language or cron | | `args` | no | one argument, a hash, or an array (kwargs last) | | `queue` | no | overrides the class's queue for these runs | | `priority` | no | numeric priority for these runs | | `description` | no | free text shown with the task | A task without `schedule` is ignored, and an invalid one makes the configuration invalid, which `bin/jobs check` reports. ## Who enqueues it The supervisor starts a **scheduler** process only when the current environment has recurring tasks (or dynamic tasks are enabled). The scheduler keeps a timer for each task's next run; when it fires, it enqueues the task's job and schedules the following run. Its `polling_interval` (5 seconds) only matters for picking up dynamic tasks. From then on the run is an ordinary job: it waits in its queue for a worker, can be blocked by concurrency limits and fails like any other job. The scheduler only **enqueues**. If no worker serves the task's queue, the nightly digest is enqueued on time and then sits there. ## Running more than one scheduler With the Puma plugin on two web servers, two schedulers read the same file. Solid Queue prevents duplicate runs without electing a leader: each enqueue writes a row to `solid_queue_recurring_executions` **in the same transaction**, and a unique index on task key and run time lets only one scheduler win each tick. Two conditions apply: - It only works while finished jobs are kept, which is the default (`preserve_finished_jobs = true`). - All schedulers must use the **same** schedule file; Solid Queue supports one recurring configuration, not different ones per scheduler. ## The task the installer adds Because finished jobs are preserved, `solid_queue_jobs` would grow forever. The generated file therefore ships `clear_solid_queue_finished_jobs`, which deletes finished jobs older than `SolidQueue.clear_finished_jobs_after` (one day by default) in batches, every hour at minute 12. Removing it without a replacement lets the queue database grow without bound. ## Switching tasks off and checking them - `SOLID_QUEUE_SKIP_RECURRING=true` or `bin/jobs --skip-recurring` starts no scheduler, which is useful on staging or review apps. - `--only-recurring` (or `SOLID_QUEUE_ONLY_RECURRING`) runs only the scheduler. - `bin/jobs check` reports invalid tasks, such as an unparsable schedule or a class that does not exist, before a deploy. - With `scheduler: dynamic_tasks_enabled: true` in `queue.yml`, `SolidQueue.schedule_recurring_task` and `unschedule_recurring_task` manage tasks at runtime.

  • When should a recurring task use command: rather than class:?
    For a one-line maintenance call that does not deserve its own job class, such as clearing old rows. The string is evaluated inside `SolidQueue::RecurringJob` on the `solid_queue_recurring` queue, so it needs a worker that serves that queue. Anything with real logic, arguments or error handling is clearer as a job `class:` that can be tested and retried like any other job.
  • The nightly digest was enqueued at 6:00 but sent at 7:40; where do you look?
    The scheduler did its part on time; the delay is downstream. Check whether any worker polls the `digests` queue, whether that queue sits behind a busier one in a worker's ordered list, and whether all worker threads were busy, for example with a long import. The task's `queue:` and the workers' `queues` lists must line up.

The scheduler is a shop's alarm clock that only rings: at six it drops an order slip into the right tray, and it is still up to the staff on duty, the workers, to pick the slip up and do the work.

  • recurring.yml tasks run inside the scheduler process itself
  • A command: task runs a shell command on the server
  • Two schedulers reading the same file will enqueue every task twice
  • Tasks under production: also run in development
  • The generated clear_solid_queue_finished_jobs task retries failed jobs