Guide

Deploy tasks

One-time work owed by a release — a backfill, a data fix, a cache warm — run once, on the new code, after the deploy has rolled over.

What a deploy task is#

A deploy task runs once per (key, version), and only after every live pgbus process reports the new app revision and at least one worker on that revision is alive. So it:

  • never runs on old code during the deploy overlap window;
  • runs once per rollover in normal operation, however many hosts you have — two schedulers never both fire it;
  • is not forgotten — a task that is owed waits until the fleet has rolled over.

It is not an exactly-once guarantee: a failure between the enqueue and recording the run can repeat it after a scheduler restart (see "What ran means" below), so make deploy tasks idempotent.

Declare tasks in deploy_tasks.yml#

Same shape as recurring.yml, minus schedule:.

Each task is a key with either a class (an ActiveJob class) or a command (Const.method, run by Pgbus::Recurring::CommandJob, with the same rules as a recurring command:). queue, args, priority and description are optional. A class: that is not an ActiveJob is skipped with a warning when the scheduler boots.

config/deploy_tasks.yml
production:
  backfill_order_totals:
    class: BackfillOrderTotalsJob      # or command: "Orders.backfill_totals!"
    version: 2                          # bump to run again after the next rollover (default 1)
    queue: maintenance
    args: [{ batch_size: 500 }]
    description: Recompute totals after the currency migration
  warm_fragment_caches:
    command: "FragmentCacheWarmer.run"
    every_deploy: true                  # runs after every rollover, dedup'd on the app revision
The file is ERB-evaluated and can be environment-scoped or flat. List several files with config.deploy_tasks_files; explicit files replace the default one. config.deploy_tasks takes a hash of the same shape.

version: or every_deploy:#

SettingRunsUse for
version: 1Once per version, after the next rollover. Compared as a string; default "1".A data fix. Bump the version in the same commit as a new fix to run it again.
every_deploy: trueAfter every rollover, dedup'd on the app revision.A cache warm. A rollback deploy re-fires it; a scheduler crash-restart does not.

version is ignored for an every_deploy task (a warning is logged).

Wait for another task with after:#

Ordering by completion, not by enqueue.

after: names the tasks a task waits for: one key, or a list of keys declared in the loaded configuration (every file in config.deploy_tasks_files counts). The task stays armed, and is not enqueued, until every task it names has a completed run at that task's current version; for an every_deploy dependency, a completed run on this revision. A rollover run and a Run now run both count, so a dependency that failed and that you fixed and ran again with Run now unblocks its dependents without a new deploy. A completed run from an earlier release counts too, as long as the version is the same.

A waiting task is not a failure: no retry backoff, no error report. It fires on the first scheduler tick after its last dependency completes. The dashboard says what it waits on and each dependency's state: not run, running (the newest run at that version has no outcome yet) or failed.

config/deploy_tasks.yml
production:
  widgets_backfill:
    class: BackfillWidgetsJob
    version: 1
  widget_totals_backfill:
    class: BackfillWidgetTotalsJob
    version: 1
    after: [widgets_backfill]          # or after: widgets_backfill

A task is skipped at scheduler boot, with a warning naming the problem, when its after: names an unknown key, when it is part of a cycle (a task naming itself included), or when a task it names is itself skipped. Removing a task from the file therefore means removing it from its dependents' after: in the same change.

Each run records its outcome. The worker stamps completed_at when perform returned having done its work, without re-enqueuing the job. A retry_on attempt whose retry is queued leaves the outcome to the retry, and so does a discard_on block that queues one. It stamps failed_at when ActiveJob discarded the job without queuing a retry (a discard_on, with or without a block, or a retry_on that gave up through its block; one without a block re-raises, so the job fails and is retried until it is dead-lettered), when the job is dead-lettered or discarded as a :while_executing duplicate, or when its retry never left: dropped by the job's own concurrency limit (on_conflict: :discard) or aborted by a before_enqueue callback. A discarded dependency reads failed, so its dependents keep waiting until a later run of it completes. A job that rescues an error inside perform and returns has completed. The task's page shows each run's outcome.

A dependency whose run never gets an outcome (its worker was killed between finishing and recording it, or its message was discarded from the dashboard) keeps reading running: Run now the dependency, or Run now the waiting task with Ignore dependencies ticked.

Runs recorded before the outcome columns existed have no outcome, so they satisfy nothing. When an after: names a task whose only runs predate the upgrade, Run now that task once (for a versioned task, bumping its version works too; an every_deploy task needs a completed run on the current revision, so only Run now helps).

The app revision#

How pgbus knows which release a process runs.

The revision is the first non-blank value of:

  1. config.app_revision (a String, or a callable returning one)
  2. ENV["PGBUS_REVISION"]
  3. ENV["DASH_VERSION"]
  4. ENV["KAMAL_VERSION"]
  5. the REVISION file in Rails.root (Capistrano)

dash injects KAMAL_VERSION into every app container, and Kamal sets it, so neither needs configuring. Every pgbus process (supervisor, worker, dispatcher, scheduler, consumer, outbox poller) stamps the revision into its heartbeat row (metadata["revision"]); the Processes page shows it in the expanded row.

With no revision the gate stays closed: tasks wait, the scheduler warns once at boot, and the dashboard says "app revision not configured". Only Run now enqueues.

The rollover gate#

Checked by the scheduler at most every 5 seconds.

A live process is one with a heartbeat in the last 5 minutes. The gate is closed while any live process reports another revision, or while no live worker runs the new revision. A process with no revision at all (a pre-upgrade pgbus) counts as lagging.

A gracefully stopped process deletes its heartbeat row at once, so a clean dash or Kamal deploy opens the gate within a scheduler tick after the last old container drains. A hard-killed old container keeps the gate closed until its row goes stale, up to 5 minutes.

Every scheduler that boots syncs its own declarations. If an old-revision scheduler crash-restarts inside the overlap window, it removes the rows of tasks only the new release declares; the new scheduler restores them on its next boot.

Who runs it#

The scheduler, the process that already runs recurring tasks. The supervisor forks it when recurring tasks or deploy tasks are configured (config.deploy_tasks_enabled, default true); --scheduler-only works as before. At boot the scheduler syncs the declared tasks into pgbus_deploy_tasks and arms the owed ones. A compare-and-set claim on the row means two schedulers (the overlap window, several hosts) never both fire, and an arm can only be fired by a scheduler declaring that version or revision.

Tasks that are due together are enqueued in key order, with no completion ordering between them. If one depends on another, declare it with after: (above).

Run now and Disable#

On the Deploy tasks page, /pgbus/deploy_tasks.

The page shows a banner with the rollover status (ready, waiting for N processes on another revision, waiting for a worker, app revision not configured, or no scheduler running) and live process counts per revision. Each task says in words whether it ran (when, on which revision), is waiting (and why), is due ("runs on the scheduler's next tick"), is disabled, or never ran. A task's page shows its configuration and its last 25 runs.

  • Run now enqueues immediately, with no gate and no claim, and records a manual run. It does not cancel a pending rollover run: Disable the task for that. A task still waiting on an after: dependency is refused, with a message naming the dependencies, unless you tick Ignore dependencies on its page.
  • Enable / Disable is honoured by the scheduler on every tick.

What "ran" means#

Enqueued, like recurring tasks.

The task is enqueued through ActiveJob perform_later, so uniqueness, batches, concurrency, priority, retries and the dead-letter queue behave as for any job. The job's outcome is on the Jobs page.

If the enqueue fails (perform_later raises, returns false, or does not enqueue the job), the task is re-armed, the failure is logged and reported to your error reporters, and the scheduler retries it after 5 seconds, then 10, 20, and so on up to 5 minutes. Disable the task to stop the retries. Each enqueue emits pgbus.deploy_task.enqueue (task, version, revision, triggered_by).

The run row is what marks a version as done. It is written before the job is enqueued (the job carries its id, so the worker can record the outcome) and deleted if the enqueue fails, including when the job's own uniqueness or concurrency on_conflict: :discard / :log drops it. It counts once the enqueue has been recorded on it; if that write fails (logged and reported), the job still runs, but the next scheduler boot sees the version as owed and runs it again: make deploy tasks idempotent.

The scheduler clears a task's arm before it writes the run row. A scheduler that dies between the two leaves the task unarmed with no run; the next scheduler boot sees the version as owed and arms it again.

Run history#

Runs are kept in pgbus_deploy_task_runs for config.deploy_task_run_retention (default 90 days; nil keeps them forever). The dispatcher prunes hourly. The read-only MCP tool pgbus_deploy_tasks lists tasks, their state and the rollover status.

Install the tables#

rails generate pgbus:add_deploy_tasks                  # migration + config/deploy_tasks.yml
rails generate pgbus:add_deploy_tasks --database=pgbus # for a separate database
rails db:migrate

rails generate pgbus:update detects the missing tables and queues the generator for you. Fresh installs (pgbus:install) already have them.

Installs whose deploy task tables predate run outcomes and after: add them with rails generate pgbus:add_deploy_task_run_outcomes (pgbus:update queues it too). Until it is migrated, a task with after: is skipped with a warning naming that generator, and no outcome is recorded.