Files
mtusi2026lab2/src/messenger/attachments/application/ports.py
T
ArtyomandClaude Opus 5.5 5ae3d77152 Carry over the lab 5 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.

This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 03:51:58 +03:00

156 lines
4.2 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, 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
@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, 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 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]: ...