Background jobs
Work that does not belong to a request runs as a job: a row in the jobs table that any API server replica may pick up. Periodic work, such as stopping idle workspaces, is a job put on the queue by a schedule. There is no leader: every replica runs jobs and keeps the schedules, and the database makes sure each job runs once at a time.
Lifecycle
queued ──claim──▶ running ──▶ succeeded
▲ │
└──── backoff ◀────┤ (attempts left)
├──▶ failed (attempts spent, or a permanent error)
└──▶ cancelled (by a person)
- Claim. A replica runs up to
jobs.concurrencyjobs at once and fills a slot as soon as it frees: a long job holds its own slot only. It takes due jobs withFOR UPDATE SKIP LOCKED, marks themrunningand holds a 30-second lease, which it renews every 10 seconds while the job runs. If it cannot renew a lease before it may have run out, it cancels the job rather than run on where another replica may have taken over. - Crash recovery. A job whose lease expired is claimed again by any replica, so a job interrupted by a crash, an eviction or a rolling upgrade resumes on its own. Jobs must therefore be idempotent.
- Fencing. Every claim increments
attempts, and an outcome is only recorded for the claim that still holds the job. A replica that stalled past its lease cannot overwrite the result of the replica that took over. - Retries. A failed attempt is retried after 10 s, 20 s, 40 s… up to an hour, until the kind's attempts are spent. An error that asking again cannot help fails the job at once: a client error (an
apierr4xx other than 408, 425 and 429), a broken invariant, or one wrapped withapierr.Permanent. A job whose every attempt died with its replica is failed as abandoned. - Cancellation. Cancelling a running job tells every replica to renew its leases at once; the one running the job finds it no longer held and cancels the job's context.
- Shutdown. A replica that stops claims no more jobs and gives those it runs 15 seconds to finish. It then cancels them and hands them back to the queue, due at once and with the attempt unspent, for another replica to run.
- Retention. The
jobs.prunejob deletes finished jobs older thanjobs.retention_days.
Schedules
A kind declared with Every gets a row in job_schedules. Advancing the schedule and enqueueing its run happen in one transaction on the locked row, and the run holds the key schedule:<kind> while it is unfinished, so a schedule never runs twice at once however many replicas there are. Operators can change the interval, pause a schedule or run it now on the Background jobs page or through /v1/job-kinds.
Writing a job
A module lists its kinds in module.Module.Jobs:
jobs.Kind{
Name: "storage.collect_orphans",
Every: 10 * time.Minute, // omit for jobs that are only enqueued
MaxAttempts: 5,
Timeout: 5 * time.Minute,
Run: func(ctx context.Context, job *jobs.Job) error {
in, err := jobs.Decode[Input](job)
...
job.Result = map[string]int{"deleted": n} // shown on the job
return nil
},
}
and enqueues in the request's transaction, so the job exists exactly when the change that asks for it does:
d.Jobs.Enqueue(ctx, tx, "notification.send", payload, jobs.Options{Key: "notify:" + id, Actor: actor})
A Key makes the enqueue add no job while another queued job holds it; the queued holder is brought forward to the earlier of the two run times instead. Running a schedule by hand therefore runs its pending run now. A job of the key that is running does not absorb the enqueue: a new job is queued behind it and waits until it has finished, so that a trigger which arrives while the work is under way, such as files changed again, is not lost. A key thus has at most one running and one queued job.