Deployment and recovery¶
Process roles¶
workhold defines roles independently of image count:
- API: producer/worker protocol;
- Admin: private control plane;
- Relay: Delivery Outbox publication;
- Maintainer: partition premake, retention, and registry purge;
- Migrator: one-shot Alembic upgrade.
One versioned image exposes the commands api, migrate, maintain, relay,
and apply (ADR 015;
the fifth role is ADR 028).
Runtime engine — PostgreSQL 18.6.x
(ADR 024).
API/Admin share the api binary but use separate listeners, credentials, and
network policy. Relay is introduced with the Delivery Outbox. Maintainer is single-winner through
an advisory lock. apply is a one-shot ensure-exists for named queues from a mounted
JSON (QUEUE_CATALOG_PATH); the catalog is not baked into the image, it is not GitOps
reconcile of live state, and it is not part of api startup / /readyz.
All five CLI commands (api, migrate, maintain, relay, apply) honor the same
opt-in error reporting: a non-empty SENTRY_DSN (or SENTRY_DSN_FILE in the
image) initializes sentry_sdk when the role starts; unset/empty leaves the SDK off
(ADR 026).
Remote MCP resource server (not an image role)¶
The optional workhold-mcp adapter is a separate process, installed from its own
package (ADR 035;
guide).
workhold-mcp is not one of api, migrate, maintain, relay, or apply,
the runtime image does not contain it, and PROCESS_ROLES does not list it.
- Run one process per trust boundary behind a reverse proxy that terminates TLS
and forwards the public
Host. The process serves only/mcp; bind127.0.0.1unless the proxy is on another host. - Set the resource URL (for example
https://mcp.example.invalid/mcp), the issuer (https://idp.example.invalid) and its JWKS URL. The issuer is external; there is no identity provider in Workhold. - Set the Host allowlist to the public host name. Set an Origin allowlist for
browser clients, or choose no-browser mode with
--no-browser-origins. Add--forwarded-allow-ipfor the proxy address when it sets forwarded headers. - Grant callers
workhold-mcp:read,workhold-mcp:produce, orworkhold-mcp:operateat the issuer. These are the MCP caller scope. They do not replace the startup tool policy or the Workhold role and queue scope of the role tokens in the process environment. - Issue those role tokens as Workhold principals with exact queue scopes (01-security.md). Keep them in the process environment, never in arguments or logs.
- MCP does not add Workhold authority. MCP does not add exactly-once delivery.
- To repeat the setup locally, run
uv run pytest tests/sdk/mcp/test_remote_smoke.py -q. It binds only127.0.0.1and signs tokens with a test-only key fixture that is not a product identity provider.
File secrets and image ENTRYPOINT¶
The runtime image uses exec-form ENTRYPOINT ["/app/entrypoint.sh"] and
CMD ["--help"] (ADR 025).
The entrypoint materializes allowlisted secrets from NAME_FILE
paths (see 01-security.md — File secrets),
then exec workhold with the remaining arguments. In Compose/Kubernetes keep
command: ["api"] (or migrate / maintain / relay / apply) as the role name —
do not use docker run … workhold api as the image command; the entrypoint already
invokes queue.
NAME_FILE values must be absolute paths. The process runs as uid
10001; that user must be able to read the mounted files. The default Compose secret mode
(0444) and Kubernetes defaultMode 0644 are acceptable. Mode 0400 is operator
guidance and requires matching ownership / fsGroup. The entrypoint follows symlinks
(Kubernetes Secret volumes use ..data).
Production Compose example (docs only — do not attach Compose secrets: to the
local development stack; that stack keeps inline env):
services:
api:
image: workhold:<version>
command: ["api"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
QUEUE_API_PRINCIPALS_MANIFEST_FILE: /run/secrets/api_principals.json
secrets:
- database_url
- api_principals
secrets:
database_url:
file: ./secrets/database_url
api_principals:
file: ./secrets/api_principals.json
Kubernetes Secret volume example (docs only):
env:
- name: DATABASE_URL_FILE
value: /run/secrets/queue/database_url
- name: QUEUE_API_PRINCIPALS_MANIFEST_FILE
value: /run/secrets/queue/api_principals.json
volumeMounts:
- name: queue-secrets
mountPath: /run/secrets/queue
readOnly: true
volumes:
- name: queue-secrets
secret:
secretName: queue-secrets
defaultMode: 0440
Do not add a secrets: section to docker-compose.dev.yml; development
stays inline-env only.
The manifest is one bounded secret JSON for PRODUCER, WORKER, OBSERVER, and
ADMIN; schema and placeholders:
service-principal-manifest.schema.json,
service-principal-manifest.example.json.
The mount must be a regular file, readable by uid 10001, non-empty, and reachable at an
absolute path. QUEUE_API_PRINCIPALS_MANIFEST_FILE and the unsuffixed env are exclusive;
the entrypoint materializes the JSON without printing its contents and removes _FILE before exec.
A non-empty manifest has exclusive precedence: legacy
QUEUE_API_BEARER_TOKEN(_PREVIOUS) is not merged and is not accepted. An invalid
manifest stops the API before listeners/engines. Only an absent or blank
manifest enables the legacy ADMIN fallback; it does not grant enqueue/claim/lease grants.
Exact queue scopes do not support wildcard/prefix. Rotation is performed by
overlapping generations of one stable principal, then removing the old generation.
Named queue catalog: apply (ensure-exists)¶
Optional one-shot workhold apply creates missing named queues from a
JSON file at the absolute QUEUE_CATALOG_PATH (apply role env, not
DeploymentSettings, not QUEUE_CATALOG_PATH_FILE). The operator mounts the file
(Compose volume / ConfigMap). The Dockerfile does not COPY catalogs into the image —
the catalog is not baked into the image. The process under uid 10001 must be able to read the path.
This is ensure-exists, not GitOps reconcile: existing queues (any state /
policy) are skipped; names removed from the file are not deleted and are not
drained. Pause / drain / activate policy is admin HTTP only. api does not
depends_on apply; startup and /readyz do not read the catalog.
Compose (docs/dev stack):
Kubernetes (docs-only; there is no chart in the repo): a Job with a ConfigMap volume, readable by
uid 10001, command: ["apply"], after the migrate Job; the api Deployment does not wait for
apply.
Pool budget: docker-compose.dev.yml records the comment
10 reserved + 14 committed (six PROCESS_ROLES × replica × pool, including
QUEUE_APPLY_*). Catalog example:
named-queue-catalog.example.json;
schema:
named-queue-catalog.schema.json.
Partial apply: after validation, a mid-catalog create failure can leave earlier
creates committed; recovery is to run workhold apply again.
Migrations and rolling upgrades¶
- a single migrator runs before new replicas become ready;
- API pods do not run migrations on startup;
- use expand → migrate/backfill → contract;
- schema and protocol versions are independent;
- N and N+1 binaries must tolerate the rollout schema;
- avoid long
ACCESS EXCLUSIVEoperations on active tables; - future daily partitions receive the same indexes/constraints as the parent.
Readiness checks PostgreSQL, a compatible schema, and the partition premake horizon. Liveness checks only that the process/event loop is alive. Statistics, relay destination, and retention lag do not make the Work Queue API unready while the correctness path is working.
Graceful shutdown¶
Process shutdown is not a persisted queue drain:
- fail readiness and stop accepting new requests;
- finish bounded in-flight API transactions;
- workers stop claiming and finish within grace or let leases expire;
- relay stops claiming and finishes/abandons current publications;
- do not mutate named queue state only because one replica terminates.
PostgreSQL connection budget¶
usable = max_connections - admin/monitoring/migration reserve
sum(role replicas × pool ceiling) <= usable
API, admin, relay, maintainer, migrator, and apply have separate bounded pools. Pool acquisition and statements fail fast. Broker/network publication never holds a DB transaction. PgBouncer is optional; any use of advisory locks or LISTEN must match the pooling mode.
Backup and restore¶
Production uses a PostgreSQL base backup + WAL/PITR according to the application RPO/RTO. Logical dumps are suitable only for small/dev instances.
Restore procedure (detail and verification steps: 05-runbooks.md — PITR):
- stop workers/relay and block intake;
- restore one consistent workhold database including active state,
correctness registries (
enqueue_dedup,complete_replay, admin replay), terminal/attempt history, andadmin_audit_log; - verify the schema and the partition horizon;
- reconcile only non-authoritative counters when needed;
- resume while expecting at-least-once task execution and duplicate-aware recovery (event republish when relay deployed).
A partial registry-only or table-level restore is forbidden. Retention is not a backup. Restoring to an earlier point can repeat work or delivery; consumer idempotency remains required. Do not treat PITR as exactly-once recovery.
Claim long polling (WORK-17)¶
Live capability: long_polling=true, max_wait_seconds=20, batch_claim=false,
max_claim_tasks=1. This is not push delivery: PostgreSQL LISTEN/NOTIFY is only a
wake hint; eligibility remains on each claim attempt. Fallback reconciliation is
1 s.
Operator bounds (see .env.example):
| Env | Default | Meaning |
|---|---|---|
QUEUE_CLAIM_MAX_WAIT_SECONDS |
20 | Server hard ceiling for wait_seconds |
QUEUE_CLAIM_WAIT_FALLBACK_SECONDS |
1.0 | Reconciliation on a missed wake |
QUEUE_CLAIM_CANCELLATION_PROBE_SECONDS |
0.25 | Disconnect / shutdown probe |
QUEUE_CLAIM_MAX_OUTSTANDING_WAITS |
64 | Per-replica waiter semaphore; the 65th → retryable resource_exhausted |
Each API replica holds one dedicated autocommit LISTEN connection outside the SQLAlchemy pool (see the connection budget). An outstanding wait does not hold a pooled checkout between claim attempts.
Production preflight: reverse-proxy upstream idle/response timeout ≥ 30 s
(server max 20 + 10 s). Client budgets: read = wait+5, total = wait+10.
ConsumerSupervisor default wait = 15 s after the capability preflight.
If the proxy floor is not proven, do not enable the capability in the deployment (fail-closed).
A real disconnect and API shutdown cancel the wait without handing out work and without exactly-once / distributed-transaction promises. Fencing and at-least-once claim semantics do not change.
Required runbooks¶
Executable procedures: 05-runbooks.md.
Kernel (Phase 4):
- API not ready / schema mismatch;
- connection exhaustion and slow claim;
- lease/reclaim storm;
- poison tasks and dead-letter growth;
- partition premake/retention failure;
- disk/WAL/autovacuum pressure;
- PITR restore and duplicate-aware recovery;
- credential rotation.
Deferred (Phase 5 Delivery Outbox):
- Delivery Outbox lag (not implemented in Phase 4 kernel runbooks).