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
+13 -7
View File
@@ -31,6 +31,7 @@ from messenger.attachments.application.ports import (
ChatMembership,
Faults,
ImageInspector,
Jitter,
JobPublisher,
ObjectStorage,
VariantRenderer,
@@ -44,6 +45,7 @@ from messenger.attachments.infrastructure.images import (
ProcessVariantRenderer,
RenderLimits,
)
from messenger.attachments.infrastructure.jitter import RandomJitter
from messenger.attachments.infrastructure.postgres import (
PostgresAttachmentDirectory,
PostgresAttachmentsUnitOfWork,
@@ -166,6 +168,10 @@ class PipelineProvider(Provider):
settings.job_backoff_cap_seconds,
)
@provide(scope=Scope.APP)
def faults(self, settings: PipelineSettings, logger: Logger) -> Faults:
return EnvironmentFaults(settings.fault_point, logger, settings.fault_stall_seconds)
@provide(scope=Scope.REQUEST)
def unit_of_work(self, database: Database) -> AttachmentsUnitOfWork:
return PostgresAttachmentsUnitOfWork(database)
@@ -185,8 +191,8 @@ class WorkerProvider(Provider):
renderer.close()
@provide(scope=Scope.APP)
def faults(self, settings: PipelineSettings, logger: Logger) -> Faults:
return EnvironmentFaults(settings.fault_point, logger)
def jitter(self) -> Jitter:
return RandomJitter()
@provide(scope=Scope.REQUEST)
def command_bus(
@@ -196,8 +202,8 @@ class WorkerProvider(Provider):
renderer: VariantRenderer,
faults: Faults,
publisher: EventPublisher,
clock: Clock,
ids: IdGenerator,
jitter: Jitter,
policy: RetryPolicy,
settings: PipelineSettings,
) -> AttachmentsCommandBus:
@@ -206,8 +212,8 @@ class WorkerProvider(Provider):
storage,
renderer,
faults,
clock=clock,
ids=ids,
jitter=jitter,
policy=policy,
lease=timedelta(seconds=settings.job_timeout_seconds),
)
@@ -229,13 +235,13 @@ class RelayProvider(Provider):
uow: AttachmentsUnitOfWork,
storage: ObjectStorage,
jobs: JobPublisher,
faults: Faults,
publisher: EventPublisher,
clock: Clock,
policy: RetryPolicy,
) -> AttachmentsCommandBus:
handlers: dict[type[Command], CommandHandler] = {
PublishOutbox: PublishOutboxHandler(uow, jobs),
RecoverExpiredLeases: RecoverExpiredLeasesHandler(uow, clock, policy),
PublishOutbox: PublishOutboxHandler(uow, jobs, faults),
RecoverExpiredLeases: RecoverExpiredLeasesHandler(uow, policy),
CleanStaging: CleanStagingHandler(uow, storage),
}
return AttachmentsCommandBus(handlers, uow, publisher)