Files
mtusi2026lab5/src/messenger/attachments/application/ports.py
T
ArtyomandClaude Opus 5.5 ecb4827e75 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>
2026-10-29 03:08:31 +03:00

150 lines
3.9 KiB
Python

from collections.abc import AsyncIterator, Sequence
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import Protocol, runtime_checkable
from uuid import UUID
from messenger.attachments.domain.jobs import Claim, FailureCode, Job, Lease, Outcome
from messenger.attachments.domain.models import Attachment
from messenger.attachments.domain.values import ImageInfo, ImageKind
from messenger.attachments.domain.variants import Variant
from messenger.shared.application.ports import UnitOfWork
@dataclass(frozen=True, slots=True)
class ObjectContent:
size: int
chunks: AsyncIterator[bytes]
@dataclass(frozen=True, slots=True)
class Rendition:
name: str
content_type: str
extension: str
data: bytes
width: int
height: int
@dataclass(frozen=True, slots=True)
class OutboxEntry:
id: int
job_id: UUID
body: bytes
@dataclass(frozen=True, slots=True)
class StagedObject:
attachment_id: UUID
key: str
@runtime_checkable
class ObjectStorage(Protocol):
async def put(self, key: str, data: bytes, content_type: str) -> None: ...
async def open(self, key: str) -> ObjectContent: ...
async def read(self, key: str) -> bytes: ...
async def copy(self, source: str, target: str) -> None: ...
async def delete(self, key: str) -> None: ...
@runtime_checkable
class ImageInspector(Protocol):
async def inspect(self, data: bytes, declared: ImageKind) -> ImageInfo: ...
@runtime_checkable
class VariantRenderer(Protocol):
async def render(self, data: bytes, pipeline_version: int) -> list[Rendition]: ...
@runtime_checkable
class JobPublisher(Protocol):
async def publish(self, job_id: UUID, body: bytes) -> None: ...
@runtime_checkable
class Faults(Protocol):
async def reach(self, point: str) -> None: ...
@runtime_checkable
class Jitter(Protocol):
def spread(self) -> float: ...
@runtime_checkable
class ChatMembership(Protocol):
async def is_member(self, chat_id: UUID, user_id: UUID) -> bool: ...
@runtime_checkable
class AttachmentRepository(Protocol):
async def add(self, attachment: Attachment) -> None: ...
async def save(self, attachment: Attachment) -> None: ...
async def lock(self, attachment_id: UUID) -> Attachment | None: ...
async def next_abandoned(self, before: datetime) -> Attachment | None: ...
async def remove(self, attachment: Attachment) -> None: ...
async def staged(self, limit: int) -> list[StagedObject]: ...
async def unstage(self, attachment_ids: Sequence[UUID]) -> None: ...
@runtime_checkable
class JobRepository(Protocol):
async def add(self, job: Job) -> None: ...
async def claim(self, job: Job, token: UUID, lease: timedelta) -> Claim | None: ...
async def complete(
self, lease: Lease, variants: Sequence[Variant], original_key: str
) -> Outcome: ...
async def reschedule(
self, lease: Lease, delay: timedelta, kind: Outcome, error: str
) -> Job | None: ...
async def renew(self, lease: Lease, duration: timedelta) -> bool: ...
async def fail(self, lease: Lease, code: FailureCode, error: str) -> bool: ...
async def note(self, job: Job, kind: Outcome) -> None: ...
async def expired(self, limit: int) -> list[Lease]: ...
@runtime_checkable
class OutboxRepository(Protocol):
async def pending(self, limit: int) -> list[OutboxEntry]: ...
async def published(self, entry_ids: Sequence[int]) -> None: ...
@runtime_checkable
class AttachmentsUnitOfWork(UnitOfWork, Protocol):
@property
def attachments(self) -> AttachmentRepository: ...
@property
def jobs(self) -> JobRepository: ...
@property
def outbox(self) -> OutboxRepository: ...
@runtime_checkable
class AttachmentDirectory(Protocol):
async def visible(self, attachment_id: UUID) -> Attachment | None: ...
async def failed_among(self, chat_id: UUID, attachment_ids: Sequence[UUID]) -> bool: ...