Files
mtusi2026lab5/app/broker.py
T

92 lines
3.0 KiB
Python

"""RabbitMQ publisher confirms. Payloads are job ids, never file bytes or credentials."""
from __future__ import annotations
import json
import logging
import threading
import pika
from pika.exceptions import AMQPError, NackError, UnroutableError
log = logging.getLogger("messenger.broker")
EXCHANGE = "attachment.jobs"
QUEUE = "attachment.process"
DLQ = "attachment.dlq"
ROUTING_PROCESS = "process"
ROUTING_DLQ = "dlq"
def _declare(channel) -> None:
channel.exchange_declare(exchange=EXCHANGE, exchange_type="direct", durable=True)
channel.queue_declare(queue=DLQ, durable=True)
channel.queue_bind(queue=DLQ, exchange=EXCHANGE, routing_key=ROUTING_DLQ)
channel.queue_declare(queue=QUEUE, durable=True)
channel.queue_bind(queue=QUEUE, exchange=EXCHANGE, routing_key=ROUTING_PROCESS)
class Publisher:
def __init__(self, url: str):
self._url = url
self._lock = threading.Lock()
self._connection = None
self._channel = None
def _reset(self) -> None:
try:
if self._connection is not None and self._connection.is_open:
self._connection.close()
except Exception:
pass
self._connection = None
self._channel = None
def _channel_locked(self):
if self._channel is not None and self._channel.is_open:
return self._channel
self._reset()
params = pika.URLParameters(self._url)
params.heartbeat = 30
params.blocked_connection_timeout = 30
params.socket_timeout = 10
self._connection = pika.BlockingConnection(params)
channel = self._connection.channel()
channel.confirm_delivery()
_declare(channel)
self._channel = channel
return channel
def publish(self, payload: dict, routing_key: str = ROUTING_PROCESS) -> bool:
body = json.dumps(payload, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
with self._lock:
try:
channel = self._channel_locked()
channel.basic_publish(
exchange=EXCHANGE,
routing_key=routing_key,
body=body,
properties=pika.BasicProperties(
content_type="application/json",
delivery_mode=2,
type="attachment.process.v1",
),
mandatory=True,
)
return True
except (AMQPError, NackError, UnroutableError, OSError):
log.warning("broker publish was not confirmed", exc_info=True)
self._reset()
return False
def open_consumer(url: str):
params = pika.URLParameters(url)
params.heartbeat = 30
params.blocked_connection_timeout = 30
params.socket_timeout = 10
connection = pika.BlockingConnection(params)
channel = connection.channel()
_declare(channel)
channel.basic_qos(prefetch_count=1)
return connection, channel