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
+16 -9
View File
@@ -8,6 +8,7 @@ import pytest
from messenger.attachments.domain.jobs import Job, RetryPolicy
from messenger.attachments.domain.variants import fit, variant_key
from messenger.attachments.entrypoints.schemas import JobMessage
from messenger.attachments.infrastructure.jitter import RandomJitter
SCHEMA = json.loads(
(Path(__file__).resolve().parents[2] / "contracts" / "jobs.schema.json").read_text()
@@ -34,16 +35,22 @@ def test_thumbnail_fits_the_box_rounding_down(
assert fit(*size, 256) == fitted
def test_retry_policy_doubles_up_to_the_cap_and_stops_at_the_limit() -> None:
@pytest.mark.parametrize(("spread", "share"), [(0.0, 0.5), (0.5, 0.75), (0.999, 0.9995)])
def test_retry_delay_has_equal_jitter_up_to_the_cap(spread: float, share: float) -> None:
policy = RetryPolicy(max_attempts=5, base_seconds=2, cap_seconds=10)
delays = [policy.delay(attempt) for attempt in range(1, 6)]
assert delays == [
timedelta(seconds=2),
timedelta(seconds=4),
timedelta(seconds=8),
timedelta(seconds=10),
None,
]
delays = [policy.delay(attempt, spread) for attempt in range(1, 6)]
ceilings = [2, 4, 8, 10]
assert delays[:4] == [timedelta(seconds=ceiling * share) for ceiling in ceilings]
assert delays[4] is None
assert policy.exhausted(5)
assert not policy.exhausted(4)
def test_jitter_spreads_retries_between_half_and_full_delay() -> None:
policy, jitter = RetryPolicy(5, 2, 60), RandomJitter()
delays = {policy.delay(3, jitter.spread()) for _ in range(200)}
assert all(timedelta(seconds=4) <= delay < timedelta(seconds=8) for delay in delays if delay)
assert len(delays) > 150
def test_job_message_follows_the_contract_schema() -> None: