Files
mtusi2026lab2/src/messenger/attachments/application/ports.py
T

193 lines
5.2 KiB
Python

from collections.abc import AsyncIterator, Sequence
from contextlib import AbstractAsyncContextManager
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from typing import Protocol, runtime_checkable
from uuid import UUID
from messenger.attachments.domain.jobs import Claim, FailedJob, 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
trace_context: dict[str, str] = field(default_factory=dict)
@dataclass(frozen=True, slots=True)
class StagedObject:
attachment_id: UUID
key: str
@dataclass(frozen=True, slots=True)
class StoredObject:
key: str
size: int
modified: datetime
@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: ...
def listing(self, prefix: str, page_size: int) -> AsyncIterator[Sequence[StoredObject]]: ...
async def stat(self, key: str) -> StoredObject | 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, trace_context: dict[str, str] | None = None
) -> 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 page_after(self, key: str, limit: int) -> Sequence[Attachment]: ...
async def by_key(self, key: str) -> Attachment | None: ...
async def claim(self, attachment_id: UUID) -> Attachment | None: ...
async def mark(self, attachment: Attachment) -> None: ...
async def staged(self, limit: int, retention: timedelta) -> 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 reprocess(self, job: Job, operator: str) -> bool: ...
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]: ...
async def replay(self, attachment: Attachment, operator: str) -> Job | None: ...
async def note_replay(self, attachment_id: UUID, operator: str, result: str) -> None: ...
@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: ...
async def failed_jobs(self, limit: int) -> list[FailedJob]: ...
@runtime_checkable
class JobObserver(Protocol):
def active(self, delta: int) -> None: ...
def observe(self, job: Job, outcome: str, seconds: float) -> None: ...
@runtime_checkable
class SingleRun(Protocol):
def hold(self) -> AbstractAsyncContextManager[bool]: ...