Skip to content
Français

Operate and recover workers

After your first job, prepare handlers for cancellation, crashes and repeated execution.

After your first job, prepare handlers for cancellation, crashes and repeated execution.

Each claim increments the job’s fence, so a worker that lost its lease cannot overwrite its successor’s result.

EventWhat happens
Handler runsThe lease lasts leaseMs (default 30 s, from 30 ms to 5 min) and is renewed every third of it.
Worker crashesThe lease expires; another worker claims the job with a new fence.
Renewal fails or job cancelledThe handler’s signal aborts and this worker stores no result.
Handler failsThe job becomes failed. The queue does not retry it: enqueue a new ID.
deadline passesThe job becomes cancelled. deadline is a timestamp in epoch milliseconds.

Pass signal to every operation the handler starts, so cancellation and lost leases stop it.

A job can run twice: a crashed worker’s successor starts the handler again. Handlers with external effects deduplicate them with idempotencyKey.

import type { QueueHandler } from "@elie-laloum/outpost/queues";
import type { WorkflowJson } from "@elie-laloum/outpost/workflows";

function deliveryHandler(
  deliverOnce: (
    key: string,
    input: WorkflowJson,
    signal: AbortSignal,
  ) => Promise<WorkflowJson>,
): QueueHandler {
  return async (input, { idempotencyKey, signal }) => ({
    value: await deliverOnce(idempotencyKey, input, signal),
  });
}

API reference: QueueHandlerContext and TaskContext.

A remote API with persistent idempotency keys works too. A receipt kept in memory, or written apart from the effect, is lost in a crash. Derive one key per effect when a handler makes several, and keep receipts as long as a job can be replayed.

  • Deploy handlers firstStart workers that know a handler before producers enqueue jobs for it.
  • Scale outRun more workers on the same queue, one worker name per process.
  • Stop cleanlyStop producers, abort the worker’s signal, await runQueueWorker(), then close the queue.
  • Recover a crashConfirm the old process stopped, then start a replacement; it claims the job once the lease expires.
  • Recover a workflow jobRelease the crashed run’s checkpoint as in Durable runs, then enqueue a new job ID for a retry-incomplete handler.
  • MonitorWatch job age, failed jobs, lease renewal errors and storage space.

A handler aborted during a stop leaves its job active; another worker claims it once the lease expires. A reclaimed workflow job fails while the crashed run still owns its checkpoint.