DocsEngineeringDurable job leases

Durable job leases

Execution ownership, renewal, and recovery.

Job leases provide exclusive, time-bounded execution ownership.

Claim

An execution worker presents the expected dispatch attempt. The jobs service checks current state and returns a lease token only when the job is eligible. For CONTAINER jobs, claim also selects a fresh READY runtime node and returns its Docker endpoint. HEARTBEAT jobs do not receive runtime ownership.

Protected mutations

The token is required to:

  • renew the lease;
  • attach a container ID with the expected runtime node;
  • complete or fail the job;
  • cancel a claimed job;
  • release it for retry.

Renewal

Workers prove authority with an immediate conditional renewal before starting work from a claim or recovery response, then renew while the workload runs. Renewal never revives an already-expired lease. EXECUTION_WORKER_LEASE_DURATION must remain comfortably longer than EXECUTION_WORKER_LEASE_RENEW_INTERVAL to tolerate transient latency.

Lease duration must be a whole number of seconds from 1s through 2147483647s, matching the jobs RPC representation. Renewal interval must be positive and leave a positive renewal RPC budget below the lease duration. Invalid explicit timing fails worker startup instead of being truncated or silently rewritten. Configure both values when using a lease shorter than the default renewal interval.

An unset environment renewal interval remains 10s, even if lease duration is changed. Only programmatic executor configuration derives an omitted interval when the default no longer fits. Validation checks wire precision and a positive budget, not sufficient latency headroom: near-equal values may pass but time out during initial proof or renewal. Leave room for observed jobs-service latency and scheduling delays; the 30s/10s defaults provide a five-second renewal RPC budget.

Recovery

Execution workers periodically request expired-lease recovery in bounded batches. A replay returns only leases whose token, worker, process owner, status, and database-evaluated expiry are still valid. Recovery includes the job's stored runtime owner and endpoint, so cleanup targets the Docker daemon that created the container. If that runtime is unavailable, recovery releases or fails eligible work through the normal retry policy instead of moving the container lifecycle to a different node.

If execution ends without a confirmed terminal or release response, the worker retains its bounded handoff permit and reconciles the original claim against PostgreSQL. This reconciliation is read-only: it never extends the lease. A live exact authority remains suppressed, while an expired or changed authority releases the permit and leaves the job eligible for normal recovery.

Container cleanup completion

Internal workers check a jobs-service completion receipt before contacting Docker and record it only after confirmed removal or absence. Receipts identify the exact job/container pair, survive workflow/job deletion and outbox pruning, and prevent repeated cleanup from depending on a retired runtime. Failed removal or receipt storage remains retryable. The receipt RPCs require the jobs-service admin role. Attachment and receipt APIs and storage share a 256-character container-ID limit.

Expired-container recovery checks receipts before resolving the runtime and records confirmed absence or successful removal before lease settlement. Lease-owned recovery has a three-minute attempt deadline and preserves caller/lease cancellation; receipt I/O and removal use ten-second budgets. Cleanup after an accepted terminal settlement retains its detached ten-second budget.

Receipts have no automatic expiry. Published cleanup events remain in the outbox until a matching receipt exists; ordinary published-event retention does not discard unfinished cleanup snapshots. Outbox publication alone is not evidence of removal, and outbox retention must not delete completion evidence. A crash after Docker removal but before receipt commit can still require another confirmation; cleanup is not an exactly-once external operation. Cleanup quarantine/redrive is a separate policy from completion evidence.

Split-brain prevention

After expiration and recovery, the old token is invalid. A delayed worker cannot report success for work that another worker now owns.

Lease state transitionsinternal/repository/jobs/lease.go