Process attachment jobs through an outbox, a relay and a worker

Upload now stages the checked original under staging/, and one
transaction moves the intent to queued, inserts the job (pipeline
version 1, attempt 1), its outbox row and an audit event; the 202 is
the in-memory snapshot, never a reread. Content answers 409 until the
attachment is ready, the thumbnail variant is served from the
published version and absent variants are 404.

The relay publishes outbox rows through FastStream with publisher
confirms, mandatory routing and persistent delivery, marks a row only
after the broker acked it, returns jobs whose lease expired and deletes
the staging object of ready attachments. The worker consumes the
quorum queue through a FastStream subscriber that declares nothing,
takes prefetch from WORKER_CONCURRENCY and acks manually after the
result commit, rejects messages off jobs.schema.json to the dead letter
exchange and requeues after an infrastructure error. A job is claimed
under a lease token; the original is copied to originals/ and a
256 px JPEG thumbnail is written under a content-addressed key; the
commit is fenced by the token, inserts variants with ON CONFLICT DO
NOTHING and publishes only a newer pipeline version. A transient error
reschedules the same job with an exponential delay and a new outbox
row, the last attempt fails with retry_exhausted, an undecodable image
fails at once with invalid_image, and a redelivered finished job is a
recorded no-op.

The worker and the relay are assembled in worker.py and relay.py with
the dishka FastStream integration and the platform providers of the
API; the shared core imports neither FastStream nor aio-pika. Tests
cover the thumbnail geometry and rendering, the job message against
the contract, the pipeline against PostgreSQL (duplicates, parallel
copies, retries, the attempt bound, leases and fencing) and the broker
adapters against a throwaway RabbitMQ with the stand's topology and
permissions.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-29 01:08:06 +03:00
co-authored by Claude Opus 5.5
parent 64fdaa03f2
commit b152b6e07e
41 changed files with 2506 additions and 115 deletions
@@ -3,6 +3,7 @@ from dataclasses import dataclass
from datetime import timedelta
from uuid import UUID
from messenger.attachments.domain.jobs import Job
from messenger.shared.application.commands import Command
@@ -19,3 +20,23 @@ class UploadAttachment(Command):
class SweepAbandonedUploads(Command):
grace: timedelta
limit: int
@dataclass(frozen=True, slots=True)
class ProcessJob(Command):
job: Job
@dataclass(frozen=True, slots=True)
class PublishOutbox(Command):
limit: int
@dataclass(frozen=True, slots=True)
class RecoverExpiredLeases(Command):
limit: int
@dataclass(frozen=True, slots=True)
class CleanStaging(Command):
limit: int
@@ -10,6 +10,7 @@ from messenger.attachments.application.ports import (
ObjectStorage,
)
from messenger.attachments.domain.errors import ChatNotFound, PayloadTooLarge
from messenger.attachments.domain.jobs import Job
from messenger.attachments.domain.models import Attachment, AttachmentStatus
from messenger.attachments.domain.values import Filename, ImageKind
from messenger.shared.application.ports import Clock, IdGenerator, Logger
@@ -35,7 +36,8 @@ async def read_limited(body: AsyncIterator[bytes], limit: int) -> tuple[bytes, s
async def drop(
uow: AttachmentsUnitOfWork, storage: ObjectStorage, attachment: Attachment, reason: str
) -> None:
await storage.delete(attachment.object_key)
if attachment.staging_key is not None:
await storage.delete(attachment.staging_key)
attachment.discard(reason)
await uow.attachments.remove(attachment)
await uow.commit()
@@ -49,6 +51,7 @@ class UploadAttachmentHandler:
"_limit",
"_logger",
"_membership",
"_pipeline_version",
"_slots",
"_storage",
"_uow",
@@ -66,6 +69,7 @@ class UploadAttachmentHandler:
limit: int,
slots: UploadSlots,
logger: Logger,
pipeline_version: int = 1,
) -> None:
self._uow = uow
self._membership = membership
@@ -76,6 +80,7 @@ class UploadAttachmentHandler:
self._limit = limit
self._slots = slots
self._logger = logger
self._pipeline_version = pipeline_version
async def __call__(self, command: UploadAttachment) -> Attachment:
if not await self._membership.is_member(command.chat_id, command.owner_id):
@@ -84,10 +89,13 @@ class UploadAttachmentHandler:
declared = ImageKind.declared(command.content_type)
async with self._slots:
attachment = await self._write(command, filename, declared)
attachment.store(self._clock.now())
now = self._clock.now()
attachment.enqueue(now)
job = Job(self._ids.new_id(), attachment.id, self._pipeline_version, 1, now)
try:
async with self._uow.transaction():
await self._uow.attachments.save(attachment)
await self._uow.jobs.add(job)
await self._uow.commit()
except Exception:
await self._compensate(attachment)
@@ -112,7 +120,7 @@ class UploadAttachmentHandler:
async with self._uow.transaction():
await self._uow.attachments.add(attachment)
await self._uow.commit()
await self._storage.put(attachment.object_key, data, attachment.content_type)
await self._storage.put(str(attachment.staging_key), data, attachment.content_type)
return attachment
async def _compensate(self, staged: Attachment) -> None:
@@ -120,7 +128,7 @@ class UploadAttachmentHandler:
async with self._uow.transaction():
current = await self._uow.attachments.lock(staged.id)
if current is None:
await self._storage.delete(staged.object_key)
await self._storage.delete(str(staged.staging_key))
elif current.status is AttachmentStatus.PENDING:
await drop(self._uow, self._storage, current, "compensation")
except Exception as error:
@@ -0,0 +1,219 @@
import asyncio
from datetime import timedelta
from hashlib import sha256
from messenger.attachments.application.commands import (
CleanStaging,
ProcessJob,
PublishOutbox,
RecoverExpiredLeases,
)
from messenger.attachments.application.ports import (
AttachmentsUnitOfWork,
Faults,
JobPublisher,
ObjectStorage,
VariantRenderer,
)
from messenger.attachments.domain.errors import UnreadableImage
from messenger.attachments.domain.events import JobSettled
from messenger.attachments.domain.jobs import Claim, FailureCode, Job, Lease, Outcome, RetryPolicy
from messenger.attachments.domain.models import original_key
from messenger.attachments.domain.variants import Variant, variant_key
from messenger.shared.application.errors import Unavailable
from messenger.shared.application.ports import Clock, IdGenerator
TRANSIENT_FAULT = "transient_error"
def settled(
job: Job,
outcome: Outcome,
*,
error_code: str | None = None,
delay_seconds: float | None = None,
) -> JobSettled:
return JobSettled(
job_id=job.id,
attachment_id=job.attachment_id,
pipeline_version=job.pipeline_version,
attempt=job.attempt,
outcome=outcome.value,
error_code=error_code,
delay_seconds=delay_seconds,
)
class ProcessJobHandler:
__slots__ = ("_clock", "_faults", "_ids", "_lease", "_policy", "_renderer", "_storage", "_uow")
def __init__(
self,
uow: AttachmentsUnitOfWork,
storage: ObjectStorage,
renderer: VariantRenderer,
faults: Faults,
*,
clock: Clock,
ids: IdGenerator,
policy: RetryPolicy,
lease: timedelta,
) -> None:
self._uow = uow
self._storage = storage
self._renderer = renderer
self._faults = faults
self._clock = clock
self._ids = ids
self._policy = policy
self._lease = lease
async def __call__(self, command: ProcessJob) -> Outcome:
async with self._uow.transaction():
claim = await self._uow.jobs.claim(command.job, self._ids.new_id(), self._lease)
if claim is None:
await self._uow.jobs.note(command.job, Outcome.DUPLICATE)
self._uow.record(settled(command.job, Outcome.DUPLICATE))
await self._uow.commit()
if claim is None:
return Outcome.DUPLICATE
try:
variants = await self._produce(claim)
except UnreadableImage as error:
return await self._fail(claim.lease, FailureCode.INVALID_IMAGE, error)
except Unavailable as error:
return await self._retry(claim.lease, error)
except Exception as error:
return await self._fail(claim.lease, FailureCode.PROCESSING_FAILED, error)
return await self._complete(claim.lease, variants)
async def _produce(self, claim: Claim) -> list[Variant]:
job = claim.lease.job
self._faults.reach(TRANSIENT_FAULT)
data = await self._storage.read(claim.source_key)
renditions = await self._renderer.render(data, job.pipeline_version)
if claim.staged:
await self._storage.copy(claim.source_key, original_key(job.attachment_id))
variants: list[Variant] = []
for rendition in renditions:
digest = sha256(rendition.data).hexdigest()
key = variant_key(
job.attachment_id, job.pipeline_version, rendition.name, digest, rendition.extension
)
await self._storage.put(key, rendition.data, rendition.content_type)
variants.append(
Variant(
name=rendition.name,
content_type=rendition.content_type,
size_bytes=len(rendition.data),
width=rendition.width,
height=rendition.height,
sha256=digest,
object_key=key,
)
)
return variants
async def _complete(self, lease: Lease, variants: list[Variant]) -> Outcome:
async with self._uow.transaction():
outcome = await self._uow.jobs.complete(
lease, variants, original_key(lease.job.attachment_id)
)
self._uow.record(settled(lease.job, outcome))
await self._uow.commit()
return outcome
async def _retry(self, lease: Lease, error: Exception) -> Outcome:
delay = self._policy.delay(lease.job.attempt)
if delay is None:
return await self._fail(lease, FailureCode.RETRY_EXHAUSTED, error)
async with self._uow.transaction():
at = self._clock.now() + delay
job = await self._uow.jobs.reschedule(
lease, at, Outcome.RESCHEDULED, type(error).__name__
)
outcome = Outcome.FENCED if job is None else Outcome.RESCHEDULED
if job is None:
await self._uow.jobs.note(lease.job, outcome)
self._uow.record(settled(lease.job, outcome, delay_seconds=delay.total_seconds()))
await self._uow.commit()
return outcome
async def _fail(self, lease: Lease, code: FailureCode, error: Exception) -> Outcome:
async with self._uow.transaction():
done = await self._uow.jobs.fail(lease, code, type(error).__name__)
outcome = Outcome.FAILED if done else Outcome.FENCED
if not done:
await self._uow.jobs.note(lease.job, outcome)
self._uow.record(settled(lease.job, outcome, error_code=code.value))
await self._uow.commit()
return outcome
class PublishOutboxHandler:
__slots__ = ("_publisher", "_uow")
def __init__(self, uow: AttachmentsUnitOfWork, publisher: JobPublisher) -> None:
self._uow = uow
self._publisher = publisher
async def __call__(self, command: PublishOutbox) -> int:
failure: Unavailable | None = None
published: list[int] = []
async with self._uow.transaction():
for entry in await self._uow.outbox.pending(command.limit):
try:
await self._publisher.publish(entry.job_id, entry.body)
except Unavailable as error:
failure = error
break
published.append(entry.id)
if published:
await self._uow.outbox.published(published)
await self._uow.commit()
if failure is not None:
raise failure
return len(published)
class RecoverExpiredLeasesHandler:
__slots__ = ("_clock", "_policy", "_uow")
def __init__(self, uow: AttachmentsUnitOfWork, clock: Clock, policy: RetryPolicy) -> None:
self._uow = uow
self._clock = clock
self._policy = policy
async def __call__(self, command: RecoverExpiredLeases) -> int:
async with self._uow.transaction():
leases = await self._uow.jobs.expired(command.limit)
for lease in leases:
if self._policy.delay(lease.job.attempt) is None:
await self._uow.jobs.fail(
lease, FailureCode.RETRY_EXHAUSTED, Outcome.LEASE_EXPIRED.value
)
outcome = Outcome.FAILED
else:
await self._uow.jobs.reschedule(
lease, self._clock.now(), Outcome.LEASE_EXPIRED, Outcome.LEASE_EXPIRED.value
)
outcome = Outcome.LEASE_EXPIRED
self._uow.record(settled(lease.job, outcome))
await self._uow.commit()
return len(leases)
class CleanStagingHandler:
__slots__ = ("_storage", "_uow")
def __init__(self, uow: AttachmentsUnitOfWork, storage: ObjectStorage) -> None:
self._uow = uow
self._storage = storage
async def __call__(self, command: CleanStaging) -> int:
async with self._uow.transaction():
staged = await self._uow.attachments.staged(command.limit)
await asyncio.gather(*(self._storage.delete(item.key) for item in staged))
await self._uow.attachments.unstage([item.attachment_id for item in staged])
await self._uow.commit()
return len(staged)
+87 -3
View File
@@ -1,11 +1,13 @@
from collections.abc import AsyncIterator
from collections.abc import AsyncIterator, Sequence
from dataclasses import dataclass
from datetime import datetime
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
@@ -15,12 +17,39 @@ class ObjectContent:
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: ...
@@ -29,6 +58,21 @@ 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):
def reach(self, point: str) -> None: ...
@runtime_checkable
class ChatMembership(Protocol):
async def is_member(self, chat_id: UUID, user_id: UUID) -> bool: ...
@@ -46,13 +90,53 @@ class AttachmentRepository(Protocol):
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, at: datetime, kind: Outcome, error: str
) -> Job | None: ...
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 ready(self, attachment_id: UUID) -> Attachment | None: ...
async def visible(self, attachment_id: UUID) -> Attachment | None: ...
async def failed_among(self, chat_id: UUID, attachment_ids: Sequence[UUID]) -> bool: ...
@@ -7,10 +7,12 @@ from messenger.attachments.application.ports import (
ObjectContent,
ObjectStorage,
)
from messenger.attachments.domain.errors import AttachmentNotFound
from messenger.attachments.domain.errors import AttachmentNotFound, AttachmentNotReady
from messenger.attachments.domain.models import Attachment
from messenger.shared.application.ports import Logger
ORIGINAL = "original"
class GetAttachment:
__slots__ = ("_attachments", "_membership")
@@ -20,7 +22,7 @@ class GetAttachment:
self._membership = membership
async def __call__(self, attachment_id: UUID, user_id: UUID) -> Attachment:
attachment = await self._attachments.ready(attachment_id)
attachment = await self._attachments.visible(attachment_id)
if attachment is None or not await self._membership.is_member(attachment.chat_id, user_id):
raise AttachmentNotFound("вложение не найдено")
return attachment
@@ -35,12 +37,21 @@ class OpenAttachment:
self._logger = logger
async def __call__(
self, attachment_id: UUID, user_id: UUID
) -> tuple[Attachment, ObjectContent]:
self, attachment_id: UUID, user_id: UUID, variant: str = ORIGINAL
) -> tuple[str, ObjectContent]:
attachment = await self._get(attachment_id, user_id)
if not attachment.ready:
raise AttachmentNotReady("вложение ещё не обработано или обработка не удалась")
if variant == ORIGINAL:
key, content_type = attachment.original_key, attachment.content_type
else:
found = attachment.variant(variant)
if found is None:
raise AttachmentNotFound("у вложения нет такого варианта")
key, content_type = found.object_key, found.content_type
try:
content = await self._storage.open(attachment.object_key)
content = await self._storage.open(str(key))
except ObjectMissingError as error:
self._logger.warning("attachment.object_missing", attachment_id=str(attachment.id))
raise AttachmentNotFound("вложение не найдено") from error
return attachment, content
return content_type, content
+21
View File
@@ -1,3 +1,5 @@
from pathlib import Path
from pydantic import Field, SecretStr
from messenger.shared.config import Settings
@@ -22,3 +24,22 @@ class AttachmentsSettings(Settings):
upload_grace_seconds: int = Field(default=900, ge=60)
upload_sweep_interval_seconds: float = Field(default=30.0, gt=0)
upload_sweep_batch: int = Field(default=100, ge=1, le=10_000)
class PipelineSettings(AttachmentsSettings):
broker_url: SecretStr = Field(min_length=8)
worker_concurrency: int = Field(default=2, ge=1, le=32)
job_max_attempts: int = Field(default=5, ge=1, le=20)
job_timeout_seconds: float = Field(default=60.0, ge=1, le=290)
job_backoff_base_seconds: float = Field(default=2.0, gt=0, le=60)
job_backoff_cap_seconds: float = Field(default=60.0, gt=0, le=3600)
staging_retention_seconds: int = Field(default=86_400, ge=60)
outbox_poll_seconds: float = Field(default=1.0, gt=0, le=10)
outbox_batch: int = Field(default=50, ge=1, le=1000)
sweep_interval_seconds: float = Field(default=5.0, gt=0, le=300)
sweep_batch: int = Field(default=100, ge=1, le=10_000)
publish_timeout_seconds: float = Field(default=5.0, gt=0, le=60)
shutdown_grace_seconds: float = Field(default=5.0, gt=0, le=9)
heartbeat_file: Path | None = None
heartbeat_interval_seconds: float = Field(default=5.0, gt=0, le=10)
fault_point: str = ""
@@ -25,5 +25,13 @@ class AttachmentNotFound(AttachmentError):
pass
class AttachmentNotReady(AttachmentError):
pass
class UploadAborted(AttachmentError):
pass
class UnreadableImage(AttachmentError):
pass
+14 -2
View File
@@ -5,12 +5,12 @@ from messenger.shared.domain.events import DomainEvent
@dataclass(frozen=True, slots=True)
class AttachmentStored(DomainEvent):
class AttachmentQueued(DomainEvent):
attachment_id: UUID
chat_id: UUID
owner_id: UUID
size_bytes: int
name: str = field(init=False, default="attachment.stored")
name: str = field(init=False, default="attachment.queued")
@dataclass(frozen=True, slots=True)
@@ -19,3 +19,15 @@ class AttachmentDiscarded(DomainEvent):
chat_id: UUID
reason: str
name: str = field(init=False, default="attachment.discarded")
@dataclass(frozen=True, slots=True)
class JobSettled(DomainEvent):
job_id: UUID
attachment_id: UUID
pipeline_version: int
attempt: int
outcome: str
error_code: str | None = None
delay_seconds: float | None = None
name: str = field(init=False, default="job.settled")
+73
View File
@@ -0,0 +1,73 @@
from dataclasses import dataclass, replace
from datetime import UTC, datetime, timedelta
from enum import StrEnum
from typing import Any
from uuid import UUID
JOB_TYPE = "attachment.process.v1"
SCHEMA_VERSION = 1
class FailureCode(StrEnum):
INVALID_IMAGE = "invalid_image"
PROCESSING_FAILED = "processing_failed"
RETRY_EXHAUSTED = "retry_exhausted"
class Outcome(StrEnum):
DONE = "done"
DUPLICATE = "duplicate_noop"
FENCED = "fenced"
SUPERSEDED = "superseded"
RESCHEDULED = "retry_scheduled"
LEASE_EXPIRED = "lease_expired"
FAILED = "failed"
@dataclass(frozen=True, slots=True)
class Job:
id: UUID
attachment_id: UUID
pipeline_version: int
attempt: int
created_at: datetime
def payload(self) -> dict[str, Any]:
stamp = self.created_at.astimezone(UTC).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
return {
"schema_version": SCHEMA_VERSION,
"job_id": str(self.id),
"type": JOB_TYPE,
"attachment_id": str(self.attachment_id),
"pipeline_version": self.pipeline_version,
"attempt": self.attempt,
"created_at": stamp,
}
def retried(self, attempt: int) -> Job:
return replace(self, attempt=attempt)
@dataclass(frozen=True, slots=True)
class Lease:
job: Job
token: UUID
@dataclass(frozen=True, slots=True)
class Claim:
lease: Lease
source_key: str
staged: bool
@dataclass(frozen=True, slots=True)
class RetryPolicy:
max_attempts: int
base_seconds: float
cap_seconds: float
def delay(self, attempt: int) -> timedelta | None:
if attempt >= self.max_attempts:
return None
return timedelta(seconds=min(self.cap_seconds, self.base_seconds * 2 ** (attempt - 1)))
+27 -7
View File
@@ -3,16 +3,25 @@ from datetime import datetime
from enum import StrEnum
from uuid import UUID
from messenger.attachments.domain.events import AttachmentDiscarded, AttachmentStored
from messenger.attachments.domain.events import AttachmentDiscarded, AttachmentQueued
from messenger.attachments.domain.values import Filename, ImageInfo
from messenger.attachments.domain.variants import Variant
from messenger.shared.domain.events import DomainEvent
OBJECT_PREFIX = "attachments/"
STAGING_PREFIX = "staging/"
ORIGINALS_PREFIX = "originals/"
class AttachmentStatus(StrEnum):
PENDING = "pending"
QUEUED = "queued"
PROCESSING = "processing"
READY = "ready"
FAILED = "failed"
def original_key(attachment_id: UUID) -> str:
return f"{ORIGINALS_PREFIX}{attachment_id}"
@dataclass(slots=True)
@@ -24,10 +33,14 @@ class Attachment:
image: ImageInfo
size_bytes: int
sha256: str
object_key: str
status: AttachmentStatus
created_at: datetime
updated_at: datetime
staging_key: str | None = None
original_key: str | None = None
published_version: int = 0
error_code: str | None = None
variants: tuple[Variant, ...] = ()
_events: list[DomainEvent] = field(init=False, default_factory=list, repr=False, compare=False)
@classmethod
@@ -51,21 +64,28 @@ class Attachment:
image=image,
size_bytes=size_bytes,
sha256=sha256,
object_key=f"{OBJECT_PREFIX}{attachment_id}",
status=AttachmentStatus.PENDING,
created_at=now,
updated_at=now,
staging_key=f"{STAGING_PREFIX}{attachment_id}",
)
@property
def content_type(self) -> str:
return self.image.kind.value
def store(self, now: datetime) -> None:
self.status = AttachmentStatus.READY
@property
def ready(self) -> bool:
return self.status is AttachmentStatus.READY
def variant(self, name: str) -> Variant | None:
return next((variant for variant in self.variants if variant.name == name), None)
def enqueue(self, now: datetime) -> None:
self.status = AttachmentStatus.QUEUED
self.updated_at = max(now, self.created_at)
self._events.append(
AttachmentStored(
AttachmentQueued(
attachment_id=self.id,
chat_id=self.chat_id,
owner_id=self.owner_id,
@@ -0,0 +1,31 @@
from dataclasses import dataclass
from uuid import UUID
THUMBNAIL = "thumbnail"
THUMBNAIL_BOX = 256
VARIANTS_PREFIX = "variants/"
@dataclass(frozen=True, slots=True)
class Variant:
name: str
content_type: str
size_bytes: int
width: int
height: int
sha256: str
object_key: str
def fit(width: int, height: int, box: int) -> tuple[int, int]:
if max(width, height) <= box:
return width, height
if width >= height:
return box, max(1, height * box // width)
return max(1, width * box // height), box
def variant_key(
attachment_id: UUID, pipeline_version: int, name: str, sha256: str, extension: str
) -> str:
return f"{VARIANTS_PREFIX}{attachment_id}/{pipeline_version}/{name}-{sha256[:16]}.{extension}"
@@ -1,5 +1,6 @@
from messenger.attachments.domain.errors import (
AttachmentNotFound,
AttachmentNotReady,
ChatNotFound,
InvalidFilename,
PayloadTooLarge,
@@ -13,6 +14,7 @@ ERROR_CODES: dict[type[DomainError], tuple[str, int]] = {
UploadAborted: ("invalid_request", 400),
ChatNotFound: ("not_found", 404),
AttachmentNotFound: ("not_found", 404),
AttachmentNotReady: ("conflict", 409),
PayloadTooLarge: ("payload_too_large", 413),
UnsupportedImage: ("unsupported_media_type", 415),
}
@@ -0,0 +1,44 @@
import asyncio
from collections.abc import Awaitable, Callable
from contextlib import suppress
from pathlib import Path
from messenger.shared.application.ports import Logger
class Heartbeat:
__slots__ = ("_interval", "_logger", "_path", "_probe", "_task")
def __init__(
self,
path: Path | None,
probe: Callable[[], Awaitable[bool]],
interval: float,
logger: Logger,
) -> None:
self._path = path
self._probe = probe
self._interval = interval
self._logger = logger
self._task: asyncio.Task[None] | None = None
def start(self) -> None:
if self._path is not None:
self._task = asyncio.create_task(self._run(self._path))
async def stop(self) -> None:
if self._task is None:
return
self._task.cancel()
with suppress(asyncio.CancelledError):
await self._task
self._task = None
async def _run(self, path: Path) -> None:
while True:
try:
if await self._probe():
await asyncio.to_thread(path.touch)
except Exception as error:
self._logger.warning("heartbeat.failed", error=type(error).__name__)
await asyncio.sleep(self._interval)
@@ -1,6 +1,6 @@
import re
from collections.abc import AsyncIterator
from typing import Annotated, Literal
from typing import Annotated
from uuid import UUID
from dishka.integrations.fastapi import FromDishka, inject
@@ -12,7 +12,7 @@ from messenger.attachments.application.bus import AttachmentsCommandBus
from messenger.attachments.application.commands import UploadAttachment
from messenger.attachments.application.queries import GetAttachment, OpenAttachment
from messenger.attachments.domain.errors import UploadAborted
from messenger.attachments.entrypoints.schemas import AttachmentOut
from messenger.attachments.entrypoints.schemas import AttachmentOut, VariantName
from messenger.shared.entrypoints.actor import Actor
UPLOAD_PATH = re.compile(r"/api/v1/chats/[^/]+/attachments/?")
@@ -30,7 +30,7 @@ async def body_of(request: Request) -> AsyncIterator[bytes]:
raise UploadAborted("клиент оборвал загрузку до конца тела") from error
@uploads_router.post("", status_code=status.HTTP_201_CREATED)
@uploads_router.post("", status_code=status.HTTP_202_ACCEPTED)
@inject
async def upload_attachment(
chat_id: UUID,
@@ -67,11 +67,11 @@ async def download_attachment(
attachment_id: UUID,
actor: Actor,
query: FromDishka[OpenAttachment],
_variant: Annotated[Literal["original"], Query(alias="variant")] = "original",
variant: Annotated[VariantName, Query()] = "original",
) -> StreamingResponse:
attachment, content = await query(attachment_id, actor)
content_type, content = await query(attachment_id, actor, variant)
return StreamingResponse(
content.chunks,
media_type=attachment.content_type,
media_type=content_type,
headers={**CONTENT_HEADERS, "Content-Length": str(content.size)},
)
+130 -13
View File
@@ -1,28 +1,50 @@
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from datetime import timedelta
from dishka import Provider, Scope, provide
from faststream.rabbit import RabbitBroker
from messenger.attachments.application.bus import AttachmentsCommandBus
from messenger.attachments.application.commands import SweepAbandonedUploads, UploadAttachment
from messenger.attachments.application.commands import (
CleanStaging,
ProcessJob,
PublishOutbox,
RecoverExpiredLeases,
SweepAbandonedUploads,
UploadAttachment,
)
from messenger.attachments.application.handlers import (
SweepAbandonedUploadsHandler,
UploadAttachmentHandler,
UploadSlots,
)
from messenger.attachments.application.pipeline import (
CleanStagingHandler,
ProcessJobHandler,
PublishOutboxHandler,
RecoverExpiredLeasesHandler,
)
from messenger.attachments.application.ports import (
AttachmentDirectory,
AttachmentsUnitOfWork,
ChatMembership,
Faults,
ImageInspector,
JobPublisher,
ObjectStorage,
VariantRenderer,
)
from messenger.attachments.application.queries import GetAttachment, OpenAttachment
from messenger.attachments.config import AttachmentsSettings
from messenger.attachments.infrastructure.images import PillowImageInspector
from messenger.attachments.config import AttachmentsSettings, PipelineSettings
from messenger.attachments.domain.jobs import RetryPolicy
from messenger.attachments.infrastructure.faults import EnvironmentFaults
from messenger.attachments.infrastructure.images import PillowImageInspector, PillowVariantRenderer
from messenger.attachments.infrastructure.postgres import (
PostgresAttachmentDirectory,
PostgresAttachmentsUnitOfWork,
)
from messenger.attachments.infrastructure.rabbitmq import RabbitJobPublisher
from messenger.attachments.infrastructure.s3 import S3ObjectStorage, s3_client
from messenger.shared.application.commands import Command
from messenger.shared.application.messagebus import CommandHandler
@@ -30,6 +52,20 @@ from messenger.shared.application.ports import Clock, EventPublisher, IdGenerato
from messenger.shared.infrastructure.postgres import Database
@asynccontextmanager
async def object_storage(settings: AttachmentsSettings) -> AsyncIterator[ObjectStorage]:
async with s3_client(
endpoint=settings.s3_endpoint,
region=settings.s3_region,
access_key_id=settings.s3_access_key_id.get_secret_value(),
secret_access_key=settings.s3_secret_access_key.get_secret_value(),
connect_timeout=settings.s3_connect_timeout_seconds,
read_timeout=settings.s3_read_timeout_seconds,
max_attempts=settings.s3_max_attempts,
) as client:
yield S3ObjectStorage(client, settings.s3_bucket)
class AttachmentsProvider(Provider):
def __init__(self, settings: AttachmentsSettings) -> None:
super().__init__()
@@ -41,16 +77,8 @@ class AttachmentsProvider(Provider):
@provide(scope=Scope.APP)
async def storage(self, settings: AttachmentsSettings) -> AsyncIterator[ObjectStorage]:
async with s3_client(
endpoint=settings.s3_endpoint,
region=settings.s3_region,
access_key_id=settings.s3_access_key_id.get_secret_value(),
secret_access_key=settings.s3_secret_access_key.get_secret_value(),
connect_timeout=settings.s3_connect_timeout_seconds,
read_timeout=settings.s3_read_timeout_seconds,
max_attempts=settings.s3_max_attempts,
) as client:
yield S3ObjectStorage(client, settings.s3_bucket)
async with object_storage(settings) as storage:
yield storage
@provide(scope=Scope.APP)
def images(self, settings: AttachmentsSettings) -> ImageInspector:
@@ -110,3 +138,92 @@ class AttachmentsProvider(Provider):
SweepAbandonedUploads: SweepAbandonedUploadsHandler(uow, storage, clock),
}
return AttachmentsCommandBus(handlers, uow, publisher)
class PipelineProvider(Provider):
def __init__(self, settings: PipelineSettings) -> None:
super().__init__()
self._settings = settings
@provide(scope=Scope.APP)
def settings(self) -> PipelineSettings:
return self._settings
@provide(scope=Scope.APP)
async def storage(self, settings: PipelineSettings) -> AsyncIterator[ObjectStorage]:
async with object_storage(settings) as storage:
yield storage
@provide(scope=Scope.APP)
def policy(self, settings: PipelineSettings) -> RetryPolicy:
return RetryPolicy(
settings.job_max_attempts,
settings.job_backoff_base_seconds,
settings.job_backoff_cap_seconds,
)
@provide(scope=Scope.REQUEST)
def unit_of_work(self, database: Database) -> AttachmentsUnitOfWork:
return PostgresAttachmentsUnitOfWork(database)
class WorkerProvider(Provider):
@provide(scope=Scope.APP)
def renderer(self, settings: PipelineSettings) -> VariantRenderer:
return PillowVariantRenderer(settings.max_image_pixels, settings.worker_concurrency)
@provide(scope=Scope.APP)
def faults(self, settings: PipelineSettings, logger: Logger) -> Faults:
return EnvironmentFaults(settings.fault_point, logger)
@provide(scope=Scope.REQUEST)
def command_bus(
self,
uow: AttachmentsUnitOfWork,
storage: ObjectStorage,
renderer: VariantRenderer,
faults: Faults,
publisher: EventPublisher,
clock: Clock,
ids: IdGenerator,
policy: RetryPolicy,
settings: PipelineSettings,
) -> AttachmentsCommandBus:
process = ProcessJobHandler(
uow,
storage,
renderer,
faults,
clock=clock,
ids=ids,
policy=policy,
lease=timedelta(seconds=settings.job_timeout_seconds),
)
return AttachmentsCommandBus({ProcessJob: process}, uow, publisher)
class RelayProvider(Provider):
def __init__(self, broker: RabbitBroker) -> None:
super().__init__()
self._broker = broker
@provide(scope=Scope.APP)
def publisher(self, settings: PipelineSettings) -> JobPublisher:
return RabbitJobPublisher(self._broker, settings.publish_timeout_seconds)
@provide(scope=Scope.REQUEST)
def command_bus(
self,
uow: AttachmentsUnitOfWork,
storage: ObjectStorage,
jobs: JobPublisher,
publisher: EventPublisher,
clock: Clock,
policy: RetryPolicy,
) -> AttachmentsCommandBus:
handlers: dict[type[Command], CommandHandler] = {
PublishOutbox: PublishOutboxHandler(uow, jobs),
RecoverExpiredLeases: RecoverExpiredLeasesHandler(uow, clock, policy),
CleanStaging: CleanStagingHandler(uow, storage),
}
return AttachmentsCommandBus(handlers, uow, publisher)
@@ -1,7 +1,22 @@
from messenger.attachments.domain.events import AttachmentDiscarded, AttachmentStored
from messenger.attachments.domain.events import AttachmentDiscarded, AttachmentQueued, JobSettled
from messenger.shared.application.messagebus import EventBus
from messenger.shared.application.ports import Logger
LOG_FIELDS = frozenset({"attachment_id", "chat_id", "actor_id", "size_bytes", "count"})
LOG_FIELDS = frozenset(
{
"attachment_id",
"chat_id",
"actor_id",
"size_bytes",
"count",
"job_id",
"attempt",
"pipeline_version",
"outcome",
"error_code",
"delay_seconds",
}
)
class AttachmentsJournal:
@@ -10,9 +25,9 @@ class AttachmentsJournal:
def __init__(self, logger: Logger) -> None:
self._logger = logger
async def on_attachment_stored(self, event: AttachmentStored) -> None:
async def on_attachment_queued(self, event: AttachmentQueued) -> None:
self._logger.info(
"audit.attachment_stored",
"audit.attachment_queued",
attachment_id=str(event.attachment_id),
chat_id=str(event.chat_id),
actor_id=str(event.owner_id),
@@ -26,3 +41,22 @@ class AttachmentsJournal:
chat_id=str(event.chat_id),
reason=event.reason,
)
async def on_job_settled(self, event: JobSettled) -> None:
self._logger.info(
"audit.job_settled",
job_id=str(event.job_id),
attachment_id=str(event.attachment_id),
pipeline_version=event.pipeline_version,
attempt=event.attempt,
outcome=event.outcome,
error_code=event.error_code,
delay_seconds=event.delay_seconds,
)
def subscribe_journal(bus: EventBus, logger: Logger) -> None:
journal = AttachmentsJournal(logger)
bus.subscribe(AttachmentQueued, journal.on_attachment_queued)
bus.subscribe(AttachmentDiscarded, journal.on_attachment_discarded)
bus.subscribe(JobSettled, journal.on_job_settled)
@@ -0,0 +1,67 @@
import asyncio
from collections.abc import Sequence
from contextlib import suppress
from dishka import AsyncContainer
from messenger.attachments.application.bus import AttachmentsCommandBus
from messenger.attachments.application.commands import (
CleanStaging,
PublishOutbox,
RecoverExpiredLeases,
)
from messenger.attachments.config import PipelineSettings
from messenger.shared.application.commands import Command
from messenger.shared.application.ports import Logger
class Relay:
__slots__ = ("_container", "_logger", "_settings", "_tasks")
def __init__(
self, container: AsyncContainer, settings: PipelineSettings, logger: Logger
) -> None:
self._container = container
self._settings = settings
self._logger = logger
self._tasks: list[asyncio.Task[None]] = []
def start(self) -> None:
settings = self._settings
loops: tuple[tuple[float, Sequence[Command]], ...] = (
(settings.outbox_poll_seconds, (PublishOutbox(settings.outbox_batch),)),
(
settings.sweep_interval_seconds,
(RecoverExpiredLeases(settings.sweep_batch), CleanStaging(settings.sweep_batch)),
),
)
self._tasks = [asyncio.create_task(self._every(*loop)) for loop in loops]
async def stop(self) -> None:
for task in self._tasks:
task.cancel()
for task in self._tasks:
with suppress(asyncio.CancelledError):
await task
self._tasks = []
async def run(self, command: Command) -> int:
async with self._container() as scope:
bus = await scope.get(AttachmentsCommandBus)
return await bus.execute(command)
async def _every(self, interval: float, commands: Sequence[Command]) -> None:
while True:
for command in commands:
try:
done = await self.run(command)
except Exception as error:
self._logger.warning(
"relay.pass_failed",
reason=type(command).__name__,
error=type(error).__name__,
)
continue
if done:
self._logger.info("relay.pass", reason=type(command).__name__, count=done)
await asyncio.sleep(interval)
@@ -1,10 +1,34 @@
from datetime import datetime
from typing import Literal
from typing import Annotated, Literal
from uuid import UUID
from pydantic import BaseModel
from pydantic import AwareDatetime, BaseModel, ConfigDict, Field
from messenger.attachments.domain.jobs import Job
from messenger.attachments.domain.models import Attachment
from messenger.attachments.domain.variants import Variant
type VariantName = Literal["original", "thumbnail", "crop", "optimized"]
class VariantOut(BaseModel):
name: str
content_type: str
size_bytes: int
width: int
height: int
sha256: str
@classmethod
def of(cls, variant: Variant) -> VariantOut:
return cls(
name=variant.name,
content_type=variant.content_type,
size_bytes=variant.size_bytes,
width=variant.width,
height=variant.height,
sha256=variant.sha256,
)
class AttachmentOut(BaseModel):
@@ -15,9 +39,9 @@ class AttachmentOut(BaseModel):
content_type: str
size_bytes: int
sha256: str
status: Literal["ready"] = "ready"
variants: tuple[()] = ()
error_code: None = None
status: str
variants: list[VariantOut]
error_code: str | None
created_at: datetime
updated_at: datetime
@@ -31,6 +55,30 @@ class AttachmentOut(BaseModel):
content_type=attachment.content_type,
size_bytes=attachment.size_bytes,
sha256=attachment.sha256,
status=attachment.status.value,
variants=[VariantOut.of(variant) for variant in attachment.variants],
error_code=attachment.error_code,
created_at=attachment.created_at,
updated_at=attachment.updated_at,
)
class JobMessage(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True)
schema_version: Literal[1]
job_id: UUID
type: Literal["attachment.process.v1"]
attachment_id: UUID
pipeline_version: Annotated[int, Field(ge=1)]
attempt: Annotated[int, Field(ge=1)]
created_at: AwareDatetime
traceparent: Annotated[
str | None, Field(pattern=r"^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$")
] = None
tracestate: Annotated[str | None, Field(max_length=512)] = None
def job(self) -> Job:
return Job(
self.job_id, self.attachment_id, self.pipeline_version, self.attempt, self.created_at
)
@@ -0,0 +1,50 @@
import asyncio
from dishka import FromDishka
from faststream import AckPolicy
from faststream.rabbit import Channel, QueueType, RabbitBroker, RabbitQueue
from faststream.rabbit.annotations import RabbitMessage
from pydantic import ValidationError
from messenger.attachments.application.bus import AttachmentsCommandBus
from messenger.attachments.application.commands import ProcessJob
from messenger.attachments.entrypoints.schemas import JobMessage
from messenger.attachments.infrastructure.rabbitmq import QUEUE
from messenger.shared.application.ports import Logger
REQUEUE_DELAY_SECONDS = 1.0
JOBS = RabbitQueue(QUEUE, queue_type=QueueType.QUORUM, declare=False)
async def raw(message: RabbitMessage) -> bytes:
return message.body
async def handle_job(
message: RabbitMessage,
bus: FromDishka[AttachmentsCommandBus],
logger: FromDishka[Logger],
) -> None:
try:
job = JobMessage.model_validate_json(message.body).job()
except ValidationError:
logger.warning("job.rejected", reason="message does not match jobs.schema.json")
await message.reject(requeue=False)
return
try:
await bus.execute(ProcessJob(job))
except Exception as error:
logger.warning("job.requeued", job_id=str(job.id), error=type(error).__name__)
await asyncio.sleep(REQUEUE_DELAY_SECONDS)
await message.nack(requeue=True)
return
await message.ack()
def subscribe(broker: RabbitBroker, concurrency: int) -> None:
broker.subscriber(
JOBS,
channel=Channel(prefetch_count=concurrency),
ack_policy=AckPolicy.MANUAL,
decoder=raw,
)(handle_job)
@@ -0,0 +1,26 @@
import os
import sys
from messenger.shared.application.errors import Unavailable
from messenger.shared.application.ports import Logger
TRANSIENT = frozenset({"transient_error"})
CRASH_EXIT_CODE = 137
class EnvironmentFaults:
__slots__ = ("_logger", "_point")
def __init__(self, point: str, logger: Logger) -> None:
self._point = point
self._logger = logger
def reach(self, point: str) -> None:
if not self._point or point != self._point:
return
self._logger.warning("fault.injected", reason=point)
if point in TRANSIENT:
raise Unavailable("внедрённая временная ошибка")
sys.stdout.flush()
sys.stderr.flush()
os._exit(CRASH_EXIT_CODE)
@@ -2,14 +2,19 @@ import asyncio
import warnings
from io import BytesIO
from PIL import Image
from PIL import Image, ImageOps
from messenger.attachments.domain.errors import PayloadTooLarge, UnsupportedImage
from messenger.attachments.application.ports import Rendition
from messenger.attachments.domain.errors import PayloadTooLarge, UnreadableImage, UnsupportedImage
from messenger.attachments.domain.values import ImageInfo, ImageKind
from messenger.attachments.domain.variants import THUMBNAIL, THUMBNAIL_BOX, fit
KINDS = {"PNG": ImageKind.PNG, "JPEG": ImageKind.JPEG}
BOMBS = (Image.DecompressionBombError, Image.DecompressionBombWarning)
JPEG_SCALE = 8
WIDE_MODES = frozenset({"I", "I;16", "I;16B", "I;16L", "I;16N"})
ALPHA_MODES = frozenset({"LA", "La", "PA", "RGBa"})
JPEG_QUALITY = 85
class PillowImageInspector:
@@ -47,3 +52,51 @@ class PillowImageInspector:
except Exception as error:
raise UnsupportedImage("изображение повреждено или обрезано") from error
return ImageInfo(kind, width, height)
def flatten(image: Image.Image) -> Image.Image:
if image.mode in WIDE_MODES:
image = image.convert("I").point(lambda value: value / 256, "L")
if image.mode == "P":
image = image.convert("RGBA" if "transparency" in image.info else "RGB")
if image.mode in ALPHA_MODES:
image = image.convert("RGBA")
if image.mode == "RGBA":
background = Image.new("RGB", image.size, "white")
background.paste(image, mask=image.getchannel("A"))
return background
return image if image.mode == "RGB" else image.convert("RGB")
def jpeg(name: str, image: Image.Image, box: int) -> Rendition:
size = fit(image.width, image.height, box)
scaled = image if size == image.size else image.resize(size, Image.Resampling.LANCZOS)
buffer = BytesIO()
scaled.save(buffer, "JPEG", quality=JPEG_QUALITY, optimize=True)
return Rendition(name, ImageKind.JPEG.value, "jpg", buffer.getvalue(), *size)
def render_variants(data: bytes, max_pixels: int) -> list[Rendition]:
try:
with Image.open(BytesIO(data), formats=tuple(KINDS)) as image:
if image.width * image.height > max_pixels:
raise UnreadableImage("изображение больше допустимого числа пикселей")
image.load()
upright = flatten(ImageOps.exif_transpose(image))
except UnreadableImage:
raise
except Exception as error:
raise UnreadableImage("изображение не декодируется") from error
return [jpeg(THUMBNAIL, upright, THUMBNAIL_BOX)]
class PillowVariantRenderer:
__slots__ = ("_max_pixels", "_slots")
def __init__(self, max_pixels: int, concurrency: int) -> None:
self._max_pixels = max_pixels
self._slots = asyncio.Semaphore(concurrency)
async def render(self, data: bytes, pipeline_version: int) -> list[Rendition]:
async with self._slots:
return await asyncio.to_thread(render_variants, data, self._max_pixels)
@@ -1,11 +1,16 @@
from datetime import datetime
import json
from collections.abc import Sequence
from datetime import datetime, timedelta
from uuid import UUID
from asyncpg import ForeignKeyViolationError, Record
from messenger.attachments.application.ports import OutboxEntry, StagedObject
from messenger.attachments.domain.errors import ChatNotFound
from messenger.attachments.domain.jobs import Claim, FailureCode, Job, Lease, Outcome
from messenger.attachments.domain.models import Attachment, AttachmentStatus
from messenger.attachments.domain.values import Filename, ImageInfo, ImageKind
from messenger.attachments.domain.variants import Variant
from messenger.shared.application.errors import Unavailable
from messenger.shared.infrastructure.postgres import (
Connection,
@@ -16,26 +21,151 @@ from messenger.shared.infrastructure.postgres import (
INSERT = """
INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
width, height, object_key, status, created_at, updated_at)
width, height, staging_key, status, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
"""
MARK = "UPDATE attachments SET status = $2, updated_at = $3 WHERE id = $1 AND status = 'pending'"
COLUMNS = """
SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, width, height,
object_key, status, created_at, updated_at
FROM attachments
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type, a.size_bytes, a.sha256, a.width,
a.height, a.status, a.staging_key, a.original_key, a.published_version, a.error_code,
a.created_at, a.updated_at
FROM attachments a
"""
READY = COLUMNS + "WHERE id = $1 AND status = 'ready'"
LOCK = COLUMNS + "WHERE id = $1 FOR UPDATE"
VISIBLE = """
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type, a.size_bytes, a.sha256, a.width,
a.height, a.status, a.staging_key, a.original_key, a.published_version, a.error_code,
a.created_at, a.updated_at,
(SELECT coalesce(jsonb_agg(jsonb_build_array(v.name, v.content_type, v.size_bytes, v.width,
v.height, v.sha256, v.object_key)
ORDER BY v.name), '[]')
FROM attachment_variants v
WHERE v.attachment_id = a.id AND v.pipeline_version = a.published_version) AS variants
FROM attachments a
WHERE a.id = $1 AND a.status <> 'pending'
"""
FAILED_AMONG = """
SELECT EXISTS (SELECT 1 FROM attachments
WHERE id = ANY($1::uuid[]) AND chat_id = $2 AND status = 'failed')
"""
LOCK = COLUMNS + "WHERE a.id = $1 FOR UPDATE"
ABANDONED = (
COLUMNS
+ """WHERE status = 'pending' AND created_at < $1
ORDER BY created_at LIMIT 1 FOR UPDATE SKIP LOCKED"""
+ """WHERE a.status = 'pending' AND a.created_at < $1
ORDER BY a.created_at LIMIT 1 FOR UPDATE SKIP LOCKED"""
)
REMOVE = "DELETE FROM attachments WHERE id = $1 AND status = 'pending'"
STAGED = """
SELECT id, staging_key FROM attachments
WHERE staging_key IS NOT NULL AND status = 'ready'
ORDER BY updated_at LIMIT $1 FOR UPDATE SKIP LOCKED
"""
UNSTAGE = "UPDATE attachments SET staging_key = NULL WHERE id = ANY($1::uuid[])"
ADD_JOB = """
WITH job AS (
INSERT INTO jobs (job_id, attachment_id, pipeline_version, status, attempt,
created_at, updated_at)
VALUES ($1, $2, $3, 'queued', $4, $5, $5)
RETURNING job_id
), box AS (
INSERT INTO outbox (job_id, payload, available_at)
SELECT job_id, $6::jsonb, now() FROM job
)
INSERT INTO job_events (job_id, attachment_id, kind, attempt)
SELECT job_id, $2, 'enqueued', $4 FROM job
"""
CLAIM = """
WITH claimed AS (
UPDATE jobs
SET status = 'processing', lease_token = $4, claims = claims + 1, updated_at = now(),
lease_until = now() + make_interval(secs => $5)
WHERE job_id = $1 AND attachment_id = $2 AND pipeline_version = $3
AND ((status = 'queued' AND coalesce(next_attempt_at, '-infinity') <= now())
OR (status = 'processing' AND lease_until < now()))
RETURNING job_id, attachment_id, attempt, created_at
), moved AS (
UPDATE attachments a SET status = 'processing', updated_at = greatest(now(), a.created_at)
FROM claimed c WHERE a.id = c.attachment_id AND a.status = 'queued'
), audit AS (
INSERT INTO job_events (job_id, attachment_id, kind, attempt)
SELECT job_id, attachment_id, 'claimed', attempt FROM claimed
)
SELECT c.attempt, c.created_at, a.staging_key, a.original_key
FROM claimed c JOIN attachments a ON a.id = c.attachment_id
"""
FINISH = """
UPDATE jobs SET status = 'done', lease_token = NULL, lease_until = NULL, next_attempt_at = NULL,
last_error = NULL, updated_at = now()
WHERE job_id = $1 AND status = 'processing' AND lease_token = $2
"""
ADD_VARIANTS = """
INSERT INTO attachment_variants (attachment_id, pipeline_version, name, object_key, content_type,
size_bytes, width, height, sha256, created_at)
SELECT $1, $2, v.name, v.object_key, v.content_type, v.size_bytes, v.width, v.height, v.sha256,
now()
FROM unnest($3::text[], $4::text[], $5::text[], $6::bigint[], $7::int[], $8::int[], $9::text[])
AS v (name, object_key, content_type, size_bytes, width, height, sha256)
ON CONFLICT DO NOTHING
"""
PUBLISH = """
UPDATE attachments
SET status = 'ready', published_version = $2, error_code = NULL, original_key = $3,
updated_at = greatest(now(), created_at)
WHERE id = $1 AND published_version < $2
"""
SUPERSEDE = "UPDATE jobs SET status = 'superseded', updated_at = now() WHERE job_id = $1"
RESCHEDULE = """
WITH job AS (
UPDATE jobs
SET status = 'queued', attempt = attempt + 1, lease_token = NULL, lease_until = NULL,
next_attempt_at = $3, last_error = $4, updated_at = now()
WHERE job_id = $1 AND status = 'processing' AND lease_token = $2
RETURNING job_id, attachment_id, attempt
), moved AS (
UPDATE attachments a SET status = 'queued', updated_at = greatest(now(), a.created_at)
FROM job WHERE a.id = job.attachment_id AND a.status = 'processing'
), audit AS (
INSERT INTO job_events (job_id, attachment_id, kind, attempt, detail)
SELECT job_id, attachment_id, $5, attempt,
jsonb_build_object('error', $4::text, 'next_attempt_at', $3::timestamptz)
FROM job
)
SELECT attempt FROM job
"""
ENQUEUE = "INSERT INTO outbox (job_id, payload, available_at) VALUES ($1, $2::jsonb, $3)"
FAIL = """
WITH job AS (
UPDATE jobs SET status = 'failed', lease_token = NULL, lease_until = NULL, last_error = $4,
updated_at = now()
WHERE job_id = $1 AND status = 'processing' AND lease_token = $2
RETURNING job_id, attachment_id, attempt
), moved AS (
UPDATE attachments a SET status = 'failed', error_code = $3,
updated_at = greatest(now(), a.created_at)
FROM job WHERE a.id = job.attachment_id AND a.status IN ('queued', 'processing')
), audit AS (
INSERT INTO job_events (job_id, attachment_id, kind, attempt, detail)
SELECT job_id, attachment_id, 'failed', attempt,
jsonb_build_object('error_code', $3::text, 'error', $4::text)
FROM job
)
SELECT attempt FROM job
"""
NOTE = "INSERT INTO job_events (job_id, attachment_id, kind, attempt) VALUES ($1, $2, $3, $4)"
EXPIRED = """
SELECT job_id, attachment_id, pipeline_version, attempt, created_at, lease_token FROM jobs
WHERE status = 'processing' AND lease_until < now()
ORDER BY lease_until LIMIT $1 FOR UPDATE SKIP LOCKED
"""
PENDING = """
SELECT id, job_id, payload::text AS body FROM outbox
WHERE published_at IS NULL AND available_at <= now()
ORDER BY available_at, id LIMIT $1 FOR UPDATE SKIP LOCKED
"""
PUBLISHED = "UPDATE outbox SET published_at = now() WHERE id = ANY($1::bigint[])"
def attachment_of(row: Record) -> Attachment:
def attachment_of(row: Record, variants: tuple[Variant, ...] = ()) -> Attachment:
return Attachment(
id=row["id"],
chat_id=row["chat_id"],
@@ -44,13 +174,21 @@ def attachment_of(row: Record) -> Attachment:
image=ImageInfo(ImageKind(row["content_type"]), row["width"], row["height"]),
size_bytes=row["size_bytes"],
sha256=row["sha256"],
object_key=row["object_key"],
status=AttachmentStatus(row["status"]),
created_at=row["created_at"],
updated_at=row["updated_at"],
staging_key=row["staging_key"],
original_key=row["original_key"],
published_version=row["published_version"],
error_code=row["error_code"],
variants=variants,
)
def payload(job: Job) -> str:
return json.dumps(job.payload(), separators=(",", ":"))
class PostgresAttachmentRepository:
__slots__ = ("_connection", "_seen")
@@ -71,7 +209,7 @@ class PostgresAttachmentRepository:
attachment.sha256,
attachment.image.width,
attachment.image.height,
attachment.object_key,
attachment.staging_key,
attachment.status.value,
attachment.created_at,
attachment.updated_at,
@@ -100,6 +238,127 @@ class PostgresAttachmentRepository:
await self._connection.execute(REMOVE, attachment.id)
self._seen.append(attachment)
async def staged(self, limit: int) -> list[StagedObject]:
rows = await self._connection.fetch(STAGED, limit)
return [StagedObject(row["id"], row["staging_key"]) for row in rows]
async def unstage(self, attachment_ids: Sequence[UUID]) -> None:
if attachment_ids:
await self._connection.execute(UNSTAGE, list(attachment_ids))
class PostgresJobRepository:
__slots__ = ("_connection",)
def __init__(self, connection: Connection) -> None:
self._connection = connection
async def add(self, job: Job) -> None:
await self._connection.execute(
ADD_JOB,
job.id,
job.attachment_id,
job.pipeline_version,
job.attempt,
job.created_at,
payload(job),
)
async def claim(self, job: Job, token: UUID, lease: timedelta) -> Claim | None:
row = await self._connection.fetchrow(
CLAIM,
job.id,
job.attachment_id,
job.pipeline_version,
token,
lease.total_seconds(),
)
if row is None:
return None
claimed = Job(
job.id, job.attachment_id, job.pipeline_version, row["attempt"], row["created_at"]
)
staged = row["staging_key"] is not None
source = row["staging_key"] if staged else row["original_key"]
return Claim(Lease(claimed, token), source, staged)
async def complete(
self, lease: Lease, variants: Sequence[Variant], original_key: str
) -> Outcome:
job = lease.job
if await self._connection.execute(FINISH, job.id, lease.token) == "UPDATE 0":
await self.note(job, Outcome.FENCED)
return Outcome.FENCED
await self._connection.execute(
ADD_VARIANTS,
job.attachment_id,
job.pipeline_version,
[v.name for v in variants],
[v.object_key for v in variants],
[v.content_type for v in variants],
[v.size_bytes for v in variants],
[v.width for v in variants],
[v.height for v in variants],
[v.sha256 for v in variants],
)
published = await self._connection.execute(
PUBLISH, job.attachment_id, job.pipeline_version, original_key
)
outcome = Outcome.SUPERSEDED if published == "UPDATE 0" else Outcome.DONE
if outcome is Outcome.SUPERSEDED:
await self._connection.execute(SUPERSEDE, job.id)
await self.note(job, outcome)
return outcome
async def reschedule(self, lease: Lease, at: datetime, kind: Outcome, error: str) -> Job | None:
attempt = await self._connection.fetchval(
RESCHEDULE, lease.job.id, lease.token, at, error, kind.value
)
if attempt is None:
return None
job = lease.job.retried(attempt)
await self._connection.execute(ENQUEUE, job.id, payload(job), at)
return job
async def fail(self, lease: Lease, code: FailureCode, error: str) -> bool:
attempt = await self._connection.fetchval(
FAIL, lease.job.id, lease.token, code.value, error
)
return attempt is not None
async def note(self, job: Job, kind: Outcome) -> None:
await self._connection.execute(NOTE, job.id, job.attachment_id, kind.value, job.attempt)
async def expired(self, limit: int) -> list[Lease]:
rows = await self._connection.fetch(EXPIRED, limit)
return [
Lease(
Job(
row["job_id"],
row["attachment_id"],
row["pipeline_version"],
row["attempt"],
row["created_at"],
),
row["lease_token"],
)
for row in rows
]
class PostgresOutboxRepository:
__slots__ = ("_connection",)
def __init__(self, connection: Connection) -> None:
self._connection = connection
async def pending(self, limit: int) -> list[OutboxEntry]:
rows = await self._connection.fetch(PENDING, limit)
return [OutboxEntry(row["id"], row["job_id"], row["body"].encode()) for row in rows]
async def published(self, entry_ids: Sequence[int]) -> None:
await self._connection.execute(PUBLISHED, list(entry_ids))
class PostgresAttachmentsUnitOfWork(PostgresUnitOfWork):
__slots__ = ()
@@ -108,6 +367,18 @@ class PostgresAttachmentsUnitOfWork(PostgresUnitOfWork):
def attachments(self) -> PostgresAttachmentRepository:
return PostgresAttachmentRepository(self.connection, self.seen)
@property
def jobs(self) -> PostgresJobRepository:
return PostgresJobRepository(self.connection)
@property
def outbox(self) -> PostgresOutboxRepository:
return PostgresOutboxRepository(self.connection)
def variants_of(raw: str) -> tuple[Variant, ...]:
return tuple(Variant(*item) for item in json.loads(raw))
class PostgresAttachmentDirectory:
__slots__ = ("_database",)
@@ -115,7 +386,11 @@ class PostgresAttachmentDirectory:
def __init__(self, database: Database) -> None:
self._database = database
async def ready(self, attachment_id: UUID) -> Attachment | None:
async def visible(self, attachment_id: UUID) -> Attachment | None:
async with self._database.connection() as connection:
row = await connection.fetchrow(READY, attachment_id)
return None if row is None else attachment_of(row)
row = await connection.fetchrow(VISIBLE, attachment_id)
return None if row is None else attachment_of(row, variants_of(row["variants"]))
async def failed_among(self, chat_id: UUID, attachment_ids: Sequence[UUID]) -> bool:
async with self._database.connection() as connection:
return bool(await connection.fetchval(FAILED_AMONG, list(attachment_ids), chat_id))
@@ -0,0 +1,37 @@
from uuid import UUID
from faststream.rabbit import RabbitBroker, RabbitExchange
from pamqp.commands import Basic
from messenger.attachments.domain.jobs import JOB_TYPE
from messenger.shared.application.errors import Unavailable
EXCHANGE = RabbitExchange("attachments", declare=False)
QUEUE = "attachments.process"
DEAD_LETTERS = "attachments.process.dlq"
class RabbitJobPublisher:
__slots__ = ("_broker", "_timeout")
def __init__(self, broker: RabbitBroker, timeout: float) -> None:
self._broker = broker
self._timeout = timeout
async def publish(self, job_id: UUID, body: bytes) -> None:
try:
confirmation = await self._broker.publish(
body,
exchange=EXCHANGE,
routing_key=JOB_TYPE,
mandatory=True,
persist=True,
message_id=str(job_id),
message_type=JOB_TYPE,
content_type="application/json",
timeout=self._timeout,
)
except Exception as error:
raise Unavailable("брокер не принял задание") from error
if not isinstance(confirmation, Basic.Ack):
raise Unavailable("брокер не подтвердил задание")
@@ -77,6 +77,21 @@ class S3ObjectStorage:
raise Unavailable("S3 недоступно") from error
return ObjectContent(size=response["ContentLength"], chunks=self._chunks(response["Body"]))
async def read(self, key: str) -> bytes:
content = await self.open(key)
try:
return b"".join([chunk async for chunk in content.chunks])
except Exception as error:
raise Unavailable("S3 оборвал чтение объекта") from error
async def copy(self, source: str, target: str) -> None:
try:
await self._client.copy_object(
Bucket=self._bucket, Key=target, CopySource={"Bucket": self._bucket, "Key": source}
)
except FAILURES as error:
raise Unavailable("S3 недоступно") from error
async def delete(self, key: str) -> None:
try:
await self._client.delete_object(Bucket=self._bucket, Key=key)
+2 -5
View File
@@ -8,12 +8,11 @@ from fastapi import FastAPI
from messenger.attachments.application.ports import ChatMembership
from messenger.attachments.config import AttachmentsSettings
from messenger.attachments.domain.events import AttachmentDiscarded, AttachmentStored
from messenger.attachments.entrypoints.errors import ERROR_CODES as ATTACHMENT_ERRORS
from messenger.attachments.entrypoints.http import UPLOAD_PATH, attachments_router, uploads_router
from messenger.attachments.entrypoints.ioc import AttachmentsProvider
from messenger.attachments.entrypoints.journal import LOG_FIELDS as ATTACHMENT_FIELDS
from messenger.attachments.entrypoints.journal import AttachmentsJournal
from messenger.attachments.entrypoints.journal import subscribe_journal
from messenger.attachments.entrypoints.sweeper import UploadSweeper
from messenger.identity.config import IdentitySettings
from messenger.identity.domain.events import SessionClosed, SessionOpened, UserRegistered
@@ -75,9 +74,7 @@ def wire_events(bus: EventBus, logger: Logger) -> None:
bus.subscribe(ChatCreated, messaging.on_chat_created)
bus.subscribe(MessagePosted, messaging.on_message_posted)
attachments = AttachmentsJournal(logger)
bus.subscribe(AttachmentStored, attachments.on_attachment_stored)
bus.subscribe(AttachmentDiscarded, attachments.on_attachment_discarded)
subscribe_journal(bus, logger)
def create_app(
+66
View File
@@ -0,0 +1,66 @@
import asyncio
import signal
from dishka import make_async_container
from faststream.rabbit import Channel, RabbitBroker
from messenger.attachments.config import PipelineSettings
from messenger.attachments.entrypoints.heartbeat import Heartbeat
from messenger.attachments.entrypoints.ioc import PipelineProvider, RelayProvider
from messenger.attachments.entrypoints.journal import LOG_FIELDS, subscribe_journal
from messenger.attachments.entrypoints.relay import Relay
from messenger.shared.application.messagebus import EventBus
from messenger.shared.config import PlatformSettings
from messenger.shared.entrypoints.ioc import PlatformProvider
from messenger.shared.infrastructure.logging import PLATFORM_FIELDS, StructlogLogger
PING_TIMEOUT_SECONDS = 2.0
async def serve() -> None:
platform = PlatformSettings()
pipeline = PipelineSettings()
logger = StructlogLogger(level=platform.log_level, allowed_fields=PLATFORM_FIELDS | LOG_FIELDS)
broker = RabbitBroker(
pipeline.broker_url.get_secret_value(),
default_channel=Channel(publisher_confirms=True, on_return_raises=True),
logger=None,
)
container = make_async_container(
PlatformProvider(platform, logger), PipelineProvider(pipeline), RelayProvider(broker)
)
stop = asyncio.Event()
loop = asyncio.get_running_loop()
for signum in (signal.SIGTERM, signal.SIGINT):
loop.add_signal_handler(signum, stop.set)
await broker.connect()
bus = await container.get(EventBus)
subscribe_journal(bus, logger)
bus.start()
relay = Relay(container, pipeline, logger)
heartbeat = Heartbeat(
pipeline.heartbeat_file,
lambda: broker.ping(PING_TIMEOUT_SECONDS),
pipeline.heartbeat_interval_seconds,
logger,
)
relay.start()
heartbeat.start()
logger.info("relay.started")
try:
await stop.wait()
finally:
await heartbeat.stop()
await relay.stop()
await bus.stop()
await broker.stop()
await container.close()
logger.info("relay.stopped")
def main() -> None:
asyncio.run(serve())
if __name__ == "__main__":
main()
+78
View File
@@ -0,0 +1,78 @@
import asyncio
from collections.abc import Sequence
from dishka import Provider, make_async_container
from dishka_faststream import setup_dishka
from faststream import FastStream
from faststream.rabbit import RabbitBroker
from messenger.attachments.config import PipelineSettings
from messenger.attachments.entrypoints.heartbeat import Heartbeat
from messenger.attachments.entrypoints.ioc import PipelineProvider, WorkerProvider
from messenger.attachments.entrypoints.journal import LOG_FIELDS, subscribe_journal
from messenger.attachments.entrypoints.worker import subscribe
from messenger.shared.application.messagebus import EventBus
from messenger.shared.config import PlatformSettings
from messenger.shared.entrypoints.ioc import PlatformProvider
from messenger.shared.infrastructure.logging import PLATFORM_FIELDS, StructlogLogger
PING_TIMEOUT_SECONDS = 2.0
def create_worker(
*,
platform: PlatformSettings | None = None,
pipeline: PipelineSettings | None = None,
overrides: Sequence[Provider] = (),
) -> FastStream:
platform = platform or PlatformSettings()
pipeline = pipeline or PipelineSettings()
logger = StructlogLogger(level=platform.log_level, allowed_fields=PLATFORM_FIELDS | LOG_FIELDS)
broker = RabbitBroker(
pipeline.broker_url.get_secret_value(),
graceful_timeout=pipeline.shutdown_grace_seconds,
logger=None,
)
subscribe(broker, pipeline.worker_concurrency)
container = make_async_container(
PlatformProvider(platform, logger),
PipelineProvider(pipeline),
WorkerProvider(),
*overrides,
)
heartbeat = Heartbeat(
pipeline.heartbeat_file,
lambda: broker.ping(PING_TIMEOUT_SECONDS),
pipeline.heartbeat_interval_seconds,
logger,
)
app = FastStream(broker, logger=None)
@app.on_startup
async def start_bus() -> None:
bus = await container.get(EventBus)
subscribe_journal(bus, logger)
bus.start()
@app.after_startup
async def started() -> None:
heartbeat.start()
logger.info("worker.started", limit=pipeline.worker_concurrency)
@app.after_shutdown
async def stopped() -> None:
await heartbeat.stop()
await (await container.get(EventBus)).stop()
await container.close()
logger.info("worker.stopped")
setup_dishka(container, app, auto_inject=True)
return app
def main() -> None:
asyncio.run(create_worker().run())
if __name__ == "__main__":
main()