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
@@ -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
)