Files
mtusi2026lab5/src/app/storage/rabbit.py
T
2026-10-23 00:07:41 +03:00

156 lines
4.9 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: topology, publisher with confirms, consumer с manual ack.
- Основная очередь: durable quorum queue.
- Retry: TTL-очереди с dead-letter обратно в основную; x-retry-count.
- DLQ: финальные ошибки и исчерпанные retry.
- Publisher confirms: publish() ждёт подтверждения брокера.
ВАЖНО: setup_topology вызывается в worker при старте и в outbox
однократно при первом publish. Не декларируем топологию на каждый
publish — иначе гонка с параллельными декларациями.
"""
import json
import logging
import os
import threading
from typing import Optional
import pika
log = logging.getLogger("rabbit")
EXCHANGE = "messenger.attachments"
QUEUE = "attachment.process.v1"
RETRY_EXCHANGE = "messenger.attachments.retry"
DLX = "messenger.attachments.dlx"
DLQ = "attachment.process.v1.dlq"
RETRY_DELAYS = [5, 15, 60]
MAX_ATTEMPTS = len(RETRY_DELAYS) + 1
_topology_ready = False
_topology_lock = threading.Lock()
def _url() -> str:
return os.environ.get("RABBITMQ_URL", "amqp://guest:guest@rabbitmq:5672/%2F")
def connection_params() -> pika.ConnectionParameters:
return pika.URLParameters(_url())
def connect() -> pika.BlockingConnection:
return pika.BlockingConnection(connection_params())
def setup_topology(channel: pika.adapters.blocking_connection.BlockingChannel) -> None:
channel.exchange_declare(EXCHANGE, exchange_type="direct", durable=True)
channel.exchange_declare(RETRY_EXCHANGE, exchange_type="direct", durable=True)
channel.exchange_declare(DLX, exchange_type="direct", durable=True)
channel.queue_declare(DLQ, durable=True, arguments={"x-queue-type": "quorum"})
channel.queue_bind(DLQ, DLX, routing_key=DLQ)
channel.queue_declare(
QUEUE,
durable=True,
arguments={
"x-queue-type": "quorum",
"x-dead-letter-exchange": DLX,
"x-dead-letter-routing-key": DLQ,
},
)
channel.queue_bind(QUEUE, EXCHANGE, routing_key=QUEUE)
for delay in RETRY_DELAYS:
qname = f"attachment.retry.{delay}s"
channel.queue_declare(
qname,
durable=True,
arguments={
"x-message-ttl": delay * 1000,
"x-dead-letter-exchange": EXCHANGE,
"x-dead-letter-routing-key": QUEUE,
},
)
channel.queue_bind(qname, RETRY_EXCHANGE, routing_key=qname)
def _ensure_topology(ch: pika.adapters.blocking_connection.BlockingChannel) -> None:
global _topology_ready
if _topology_ready:
return
with _topology_lock:
if _topology_ready:
return
setup_topology(ch)
_topology_ready = True
def publish_job(payload: dict) -> None:
"""Публикация job с publisher confirm. Бросает при неудаче."""
conn = connect()
try:
ch = conn.channel()
ch.confirm_delivery()
_ensure_topology(ch)
body = json.dumps(payload, ensure_ascii=False, separators=(",", ":"))
ch.basic_publish(
exchange=EXCHANGE,
routing_key=QUEUE,
body=body.encode("utf-8"),
properties=pika.BasicProperties(
delivery_mode=2,
content_type="application/json",
),
mandatory=True,
)
finally:
conn.close()
def publish_retry(payload: dict, retry_count: int) -> None:
idx = min(retry_count - 1, len(RETRY_DELAYS) - 1)
delay = RETRY_DELAYS[idx]
qname = f"attachment.retry.{delay}s"
conn = connect()
try:
ch = conn.channel()
ch.confirm_delivery()
_ensure_topology(ch)
headers = dict(payload.get("_headers") or {})
headers["x-retry-count"] = retry_count
clean = {k: v for k, v in payload.items() if k != "_headers"}
body = json.dumps(clean, ensure_ascii=False, separators=(",", ":"))
ch.basic_publish(
exchange=RETRY_EXCHANGE,
routing_key=qname,
body=body.encode("utf-8"),
properties=pika.BasicProperties(
delivery_mode=2,
content_type="application/json",
headers=headers,
),
mandatory=True,
)
finally:
conn.close()
def publish_dlq(payload: dict) -> None:
conn = connect()
try:
ch = conn.channel()
ch.confirm_delivery()
_ensure_topology(ch)
body = json.dumps(payload, ensure_ascii=False, separators=(",", ":"))
ch.basic_publish(
exchange=DLX,
routing_key=DLQ,
body=body.encode("utf-8"),
properties=pika.BasicProperties(delivery_mode=2, content_type="application/json"),
mandatory=True,
)
finally:
conn.close()