Renew job leases, spread retries with jitter and add crash points

Leases: the worker renews its lease before the first side effect and
every JOB_TIMEOUT_SECONDS/3 while the variants are produced; a renewal
that finds the lease taken cancels the work (and its render process)
and settles the job as fenced without writing anything.

Retries: the delay before attempt n+1 is drawn from [d/2, d] with
d = min(cap, base * 2^(n-1)) (equal jitter), the next attempt time is
computed by PostgreSQL and the delay is kept in the job event.

Faults: the Faults port is async; relay reaches after_commit_before_publish
and after_publish_before_confirm, worker reaches after_claim_before_side_effect,
stall_after_claim (once, FAULT_STALL_SECONDS), after_s3_write_before_commit
and after_commit_before_ack. Crashes are os._exit(137).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-29 03:08:31 +03:00
co-authored by Claude Opus 5.5
parent f2242eb39d
commit ecb4827e75
14 changed files with 432 additions and 70 deletions
@@ -118,19 +118,24 @@ RESCHEDULE = """
WITH job AS (
UPDATE jobs
SET status = 'queued', attempt = attempt + 1, lease_token = NULL, lease_until = NULL,
next_attempt_at = $3, last_error = $4, updated_at = now()
next_attempt_at = now() + make_interval(secs => $3), last_error = $4, updated_at = now()
WHERE job_id = $1 AND status = 'processing' AND lease_token = $2
RETURNING job_id, attachment_id, attempt
RETURNING job_id, attachment_id, attempt, next_attempt_at
), moved AS (
UPDATE attachments a SET status = 'queued', updated_at = greatest(now(), a.created_at)
FROM job WHERE a.id = job.attachment_id AND a.status = 'processing'
), audit AS (
INSERT INTO job_events (job_id, attachment_id, kind, attempt, detail)
SELECT job_id, attachment_id, $5, attempt,
jsonb_build_object('error', $4::text, 'next_attempt_at', $3::timestamptz)
jsonb_build_object('error', $4::text, 'next_attempt_at', next_attempt_at,
'delay_seconds', $3::float8)
FROM job
)
SELECT attempt FROM job
SELECT attempt, next_attempt_at FROM job
"""
RENEW = """
UPDATE jobs SET lease_until = now() + make_interval(secs => $3), updated_at = now()
WHERE job_id = $1 AND lease_token = $2 AND status = 'processing'
"""
ENQUEUE = "INSERT INTO outbox (job_id, payload, available_at) VALUES ($1, $2::jsonb, $3)"
FAIL = """
@@ -310,16 +315,24 @@ class PostgresJobRepository:
await self.note(job, outcome)
return outcome
async def reschedule(self, lease: Lease, at: datetime, kind: Outcome, error: str) -> Job | None:
attempt = await self._connection.fetchval(
RESCHEDULE, lease.job.id, lease.token, at, error, kind.value
async def reschedule(
self, lease: Lease, delay: timedelta, kind: Outcome, error: str
) -> Job | None:
row = await self._connection.fetchrow(
RESCHEDULE, lease.job.id, lease.token, delay.total_seconds(), error, kind.value
)
if attempt is None:
if row is None:
return None
job = lease.job.retried(attempt)
await self._connection.execute(ENQUEUE, job.id, payload(job), at)
job = lease.job.retried(row["attempt"])
await self._connection.execute(ENQUEUE, job.id, payload(job), row["next_attempt_at"])
return job
async def renew(self, lease: Lease, duration: timedelta) -> bool:
done = await self._connection.execute(
RENEW, lease.job.id, lease.token, duration.total_seconds()
)
return done == "UPDATE 1"
async def fail(self, lease: Lease, code: FailureCode, error: str) -> bool:
attempt = await self._connection.fetchval(
FAIL, lease.job.id, lease.token, code.value, error