forked from mtusi2026/mtusi2026lab5
ЛР 5 — БВТ2301 — Курлова — на 5
RabbitMQ + transactional outbox, отдельный worker (thumbnail/crop/WebP), lease и heartbeat, retry с backoff+jitter, DLQ и replay, admission control; тесты отказов, трек 1 и отчёт.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
"""RabbitMQ topology and confirmed publishing (aio-pika).
|
||||
|
||||
Topology (declared idempotently by every publisher and consumer):
|
||||
exchange attachments direct, durable
|
||||
queue attachments.process quorum, durable, dead-letter -> attachments.dlx, x-delivery-limit
|
||||
exchange attachments.dlx fanout, durable
|
||||
queue attachments.dlq quorum, durable (poison / undeliverable messages for the operator)
|
||||
Messages are persistent; the channel uses publisher confirms, so `publish` returns only after the broker
|
||||
has taken responsibility for the message (written to the quorum log).
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
|
||||
import aio_pika
|
||||
from aio_pika import DeliveryMode, ExchangeType, Message
|
||||
|
||||
from app import config
|
||||
|
||||
log = logging.getLogger("messenger.broker")
|
||||
ROUTING_KEY = "attachment.process.v1"
|
||||
|
||||
|
||||
async def connect():
|
||||
return await aio_pika.connect_robust(config.BROKER_URL, timeout=5, client_properties={"connection_name": config.INSTANCE_ID})
|
||||
|
||||
|
||||
async def declare(channel):
|
||||
dlx = await channel.declare_exchange(config.BROKER_DLX, ExchangeType.FANOUT, durable=True)
|
||||
dlq = await channel.declare_queue(config.BROKER_DLQ, durable=True, arguments={"x-queue-type": "quorum"})
|
||||
await dlq.bind(dlx)
|
||||
exchange = await channel.declare_exchange(config.BROKER_EXCHANGE, ExchangeType.DIRECT, durable=True)
|
||||
queue = await channel.declare_queue(
|
||||
config.BROKER_QUEUE,
|
||||
durable=True,
|
||||
arguments={
|
||||
"x-queue-type": "quorum",
|
||||
"x-dead-letter-exchange": config.BROKER_DLX,
|
||||
"x-delivery-limit": config.BROKER_DELIVERY_LIMIT,
|
||||
},
|
||||
)
|
||||
await queue.bind(exchange, ROUTING_KEY)
|
||||
return exchange, queue
|
||||
|
||||
|
||||
class Publisher:
|
||||
def __init__(self) -> None:
|
||||
self.connection = None
|
||||
self.channel = None
|
||||
self.exchange = None
|
||||
|
||||
async def ensure(self) -> None:
|
||||
if self.channel is not None and not self.channel.is_closed:
|
||||
return
|
||||
if self.connection is None or self.connection.is_closed:
|
||||
self.connection = await connect()
|
||||
self.channel = await self.connection.channel(publisher_confirms=True)
|
||||
self.exchange, _ = await declare(self.channel)
|
||||
|
||||
async def publish(self, body: dict) -> None:
|
||||
await self.ensure()
|
||||
message = Message(
|
||||
json.dumps(body, separators=(",", ":")).encode(),
|
||||
content_type="application/json",
|
||||
delivery_mode=DeliveryMode.PERSISTENT,
|
||||
message_id=body["job_id"],
|
||||
type=body["type"],
|
||||
headers={"attempt": body["attempt"]},
|
||||
)
|
||||
# mandatory: an unroutable message is an error, not a silent drop; awaited confirm = broker ack.
|
||||
await self.exchange.publish(message, routing_key=ROUTING_KEY, mandatory=True, timeout=5)
|
||||
|
||||
async def close(self) -> None:
|
||||
try:
|
||||
if self.connection is not None:
|
||||
await self.connection.close()
|
||||
except Exception:
|
||||
pass
|
||||
self.connection = self.channel = self.exchange = None
|
||||
Reference in New Issue
Block a user