skip to content

In Rails 8.1, how does ActiveJob::Continuable let a long-running job resume after a deploy stops its worker, and what must the job's code do for it to work?

level: seniorimportance: nice to knowfreq 22%

answer

  1. steps plus an optional cursor
  2. checkpoints ask the adapter stopping?
  3. Continuation::Interrupt, then retry_job
  4. completed steps skipped on resume
  5. no checkpoint inside a transaction

basics

~20 s

Including ActiveJob::Continuable lets perform declare steps with an optional cursor; at each checkpoint the job asks the adapter whether it is stopping and, if so, saves its progress into the job data and re-enqueues itself to resume from that point.

solid answer

~50 s

Rails 8.1's `ActiveJob::Continuable` adds `step :name do |step| ... end` (or `step :name` calling a method). Inside a step you record progress with `step.advance! from: record.id`, `step.set!(cursor)` or `step.checkpoint!`; each is a **checkpoint**, and there is one before every step except the first in a run. At a checkpoint the job asks `queue_adapter.stopping?`; if the worker is shutting down it raises `ActiveJob::Continuation::Interrupt` and re-enqueues itself with `resume_options` (`{ wait: 5.seconds }` by default), storing completed steps and the current cursor under `"continuation"`. On resume, completed steps are skipped and the current one restarts from its cursor, while code outside steps runs again. For it to work, checkpoint more often than the shutdown grace period, make each item safe to redo, never checkpoint inside an open transaction (`CheckpointError`) and keep step names and order stable across deploys.

go deeper

for a junior

Know that Rails 8.1 jobs can include ActiveJob::Continuable to split work into steps that resume after an interruption.

for a middle

Explain steps, cursors and checkpoints, what gets stored under continuation, and that code outside steps re-runs.

for a senior

Size checkpoint frequency to the shutdown grace period, keep units idempotent, avoid transactions across checkpoints and keep steps stable across deploys.

for a principal

Judge when a long job should become a continuation and when it should be split into many small jobs that the queue can parallelise.

## The problem continuations solve Some jobs run for minutes or hours: regenerating 40,000 certificate PDFs after a course's template changes, for example. Deploys stop worker processes and give running jobs only a short grace period. Before Rails 8.1, such a job was killed and later retried **from the start**, or hand-rolled its own progress tracking. **Continuations**, new in Rails 8.1, let a job split its work into **steps**, record progress inside a step with a **cursor**, and resume where it stopped. ## Steps and cursors ```ruby class RegenerateCourseCertificatesJob < ApplicationJob include ActiveJob::Continuable def perform(course) @course = course # runs on every execution @template = course.certificate_template step :render_pdfs do |step| course.enrollments.completed.find_each(start: step.cursor) do |enrollment| CertificatePdf.new(enrollment, @template).attach! step.advance! from: enrollment.id end end step :mark_done end private def mark_done = @course.update!(certificates_regenerated_at: Time.current) end ``` - `step :name do |step| ... end` runs a block; `step :name` calls a method of that name, which may take the step object or nothing. - A step's **cursor** starts as `nil` or the `start:` value. `step.set!(value)` sets it; `step.advance!` calls `succ` on it, and `advance! from: record.id` sets it to the next value after that id, the natural fit for `find_each(start:)`. - Steps run in order as `perform` reaches them. Code **outside** steps runs on every execution, so loading shared state there is fine and side effects there are not. ## Checkpoints and interruption 1. A **checkpoint** happens at every `set!`, `advance!` and `checkpoint!`, and automatically before each step except the first one of the current execution. 2. At a checkpoint the job calls `queue_adapter.stopping?`. Solid Queue's and Sidekiq's adapters report when their worker is shutting down. 3. If the adapter is stopping, the job raises **`ActiveJob::Continuation::Interrupt`**. It inherits from `Exception`, not `StandardError`, so a plain `rescue => e` in your code does not swallow it. 4. Continuable's `around_perform` catches it and calls `retry_job` with `resume_options`, `{ wait: 5.seconds }` by default. The new payload holds the **completed step names** and the **current step with its cursor** under the `"continuation"` key. 5. On the next run, completed steps are skipped, the interrupted step receives its saved cursor (`step.resumed?` is true), and `resumptions` counts how often this has happened. A job is **not** interrupted between checkpoints; it keeps running until the next one or until the process is killed. ## Configuration | Class attribute | Default | Effect | |---|---|---| | `max_resumptions` | `nil` (unlimited) | beyond it, `Continuation::ResumeLimitError` is raised | | `resume_options` | `{ wait: 5.seconds }` | options passed to `retry_job` when resuming | | `resume_errors_after_advancing` | `true` | an error after progress was made also resumes instead of failing | `step :name, isolated: true` forces a step to start in a fresh execution, so progress is saved before a step that cannot checkpoint often enough. ## Watching it happen Active Job logs each transition at info level, which is usually enough to confirm the behaviour after the first deploy: - `Step 'render_pdfs' started for RegenerateCourseCertificatesJob` when a step begins fresh. - `Interrupted RegenerateCourseCertificatesJob ... (stopping)` when a checkpoint sees the worker stopping. - `Resuming RegenerateCourseCertificatesJob ...` and `Step 'render_pdfs' resumed from cursor '...'` on the next run. - `Step '...' skipped` for steps already completed. ## What your code must guarantee - **Checkpoint often.** The interval between checkpoints must be shorter than the worker's shutdown grace period, or the process is killed mid-step. - **Make each unit idempotent.** Progress is saved only when the job re-enqueues itself. When a backend re-runs a job whose process was killed or crashed between checkpoints, it starts from the progress stored in the payload, so some items are processed twice: attaching a regenerated PDF twice must be harmless. - **Never checkpoint inside an open transaction.** With Active Record loaded, a checkpoint while any transaction is open raises `ActiveJob::Continuation::CheckpointError`. - **Keep steps stable.** Names must be symbols, unique, not nested, and in the same order on every run; a deploy that renames or reorders steps while jobs are mid-flight raises `Continuation::InvalidStepError` on resume. - **Use a serializable cursor** that implements `succ` if you call `advance!` without `from:`, or `Continuation::UnadvanceableCursorError` is raised.

  • Why does a continuable job sometimes process the same record twice?
    Progress is persisted only when the job re-enqueues itself, at an interrupting checkpoint or after an error. When a backend re-runs a job whose process was killed between checkpoints, it starts from the cursor already in the payload, so records handled after that point are processed again. Each unit of work must be safe to repeat.
  • What does isolated: true on a step buy you?
    It makes the step start in its own execution: if earlier steps made progress in this run, the job interrupts itself first so that progress is saved. A slow step that cannot checkpoint within the grace period then at least does not take the earlier steps' work down with it.

  • A continuable job is paused mid-statement the moment the worker gets SIGTERM
  • Completed steps re-run from scratch on every resume
  • Code outside step blocks runs only on the first execution
  • You can checkpoint inside a transaction; Rails commits it first
  • Continuations make each step's work exactly-once, so idempotency is unnecessary

Assumes Rails 8.1, the release that added ActiveJob::Continuable; Rails 8.0 and earlier have no continuations.