Files
mtusi2026lab5/app/broker.py
T
RainakandClaude Opus 5.5 0a221b0675 ЛР5 БВТ2304 Запорожец на 5
RabbitMQ (quorum queue, publisher confirms, manual ack, DLX) + отдельный worker:
staging в S3 + вложение/job/outbox одной транзакцией -> 202 queued; relay outbox,
lease с heartbeat и fencing (worker_id, attempt), retry с backoff+jitter, failed-реестр,
безопасный replay с audit; thumbnail/crop/WebP в изолированном процессе с лимитами.
Тесты окон отказа (fault injection), трек 1 «Очередь под перегрузкой» с backpressure.
Перенесено исправление замечания lab3 PR #99 (U+0000 в тексте).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 18:33:52 +03:00

40 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""RabbitMQ: топология очередей заданий. Объявление идемпотентно — его выполняют и API (relay), и worker.
attachments (direct, durable) --attachment.process.v1--> quorum queue attachment.process.v1
отклонённые/непарсящиеся и превысившие x-delivery-limit сообщения
--> attachments.dlx --> quorum queue attachment.process.v1.dlq (просмотр оператором)
Quorum queue: сообщения реплицируются в Raft-журнал на диске (volume rabbitdata) и переживают рестарт
брокера; persistent delivery_mode + publisher confirm у publisher; manual ack у consumer.
"""
import aio_pika
from aio_pika import ExchangeType
EXCHANGE = 'attachments'
DLX = 'attachments.dlx'
QUEUE = 'attachment.process.v1'
DLQ = 'attachment.process.v1.dlq'
ROUTING_KEY = 'attachment.process.v1'
# Защита от poison message, который валит worker до ack: после N доставок — в DLQ.
# Состояние в БД при этом не теряется: reaper/sweeper вернёт работу по lease (см. app/outbox.py).
DELIVERY_LIMIT = 20
async def connect(url: str) -> aio_pika.abc.AbstractRobustConnection:
return await aio_pika.connect_robust(url, timeout=5, client_properties={'connection_name': 'messenger'})
async def declare(channel: aio_pika.abc.AbstractChannel):
dlx = await channel.declare_exchange(DLX, ExchangeType.DIRECT, durable=True)
dlq = await channel.declare_queue(DLQ, durable=True, arguments={'x-queue-type': 'quorum'})
await dlq.bind(dlx, ROUTING_KEY)
exchange = await channel.declare_exchange(EXCHANGE, ExchangeType.DIRECT, durable=True)
queue = await channel.declare_queue(QUEUE, durable=True, arguments={
'x-queue-type': 'quorum',
'x-dead-letter-exchange': DLX,
'x-dead-letter-routing-key': ROUTING_KEY,
'x-delivery-limit': DELIVERY_LIMIT,
})
await queue.bind(exchange, ROUTING_KEY)
return exchange, queue