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>
79 lines
2.5 KiB
Python
79 lines
2.5 KiB
Python
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()
|