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
+26 -5
View File
@@ -20,6 +20,7 @@ from messenger.attachments.application.pipeline import (
)
from messenger.attachments.application.ports import (
Faults,
Jitter,
ObjectStorage,
Rendition,
VariantRenderer,
@@ -30,15 +31,25 @@ from messenger.attachments.infrastructure.faults import EnvironmentFaults
from messenger.attachments.infrastructure.images import RenderLimits, render_variants
from messenger.attachments.infrastructure.postgres import PostgresAttachmentsUnitOfWork
from messenger.shared.application.errors import Unavailable
from messenger.shared.infrastructure.clock import MonotonicUtcClock
from messenger.shared.infrastructure.ids import Uuid7Generator
from messenger.shared.infrastructure.postgres import Database
IDLE_SECONDS = 0.1
HALF = 0.5
LIMITS = RenderLimits(10_485_760, 40_000_000)
FAST = RetryPolicy(max_attempts=5, base_seconds=0.001, cap_seconds=0.001)
class FixedJitter:
__slots__ = ("_spread",)
def __init__(self, spread: float) -> None:
self._spread = spread
def spread(self) -> float:
return self._spread
class InlineRenderer:
__slots__ = ()
@@ -62,7 +73,16 @@ class CapturingPublisher:
class InlinePipeline:
__slots__ = ("_database", "faults", "lease", "policy", "publisher", "renderer", "storage")
__slots__ = (
"_database",
"faults",
"jitter",
"lease",
"policy",
"publisher",
"renderer",
"storage",
)
def __init__(
self,
@@ -79,13 +99,14 @@ class InlinePipeline:
self.publisher = CapturingPublisher()
self.renderer: VariantRenderer = InlineRenderer()
self.faults: Faults = EnvironmentFaults("", NullLogger())
self.jitter: Jitter = FixedJitter(HALF)
def uow(self) -> PostgresAttachmentsUnitOfWork:
return PostgresAttachmentsUnitOfWork(self._database)
async def publish(self) -> list[Job]:
start = len(self.publisher.bodies)
await PublishOutboxHandler(self.uow(), self.publisher)(PublishOutbox(100))
await PublishOutboxHandler(self.uow(), self.publisher, self.faults)(PublishOutbox(100))
return [
JobMessage.model_validate_json(body).job() for body in self.publisher.bodies[start:]
]
@@ -96,8 +117,8 @@ class InlinePipeline:
self.storage,
self.renderer,
self.faults,
clock=MonotonicUtcClock(),
ids=Uuid7Generator(),
jitter=self.jitter,
policy=self.policy,
lease=self.lease,
)
@@ -117,7 +138,7 @@ class InlinePipeline:
return outcomes
async def recover(self) -> int:
handler = RecoverExpiredLeasesHandler(self.uow(), MonotonicUtcClock(), self.policy)
handler = RecoverExpiredLeasesHandler(self.uow(), self.policy)
return await handler(RecoverExpiredLeases(100))
async def clean(self) -> int: