forked from mtusi2026/mtusi2026lab6
91 lines
3.4 KiB
Python
91 lines
3.4 KiB
Python
"""Small durable RabbitMQ adapter; broker messages contain no attachment bytes."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pika
|
|
from starlette.concurrency import run_in_threadpool
|
|
|
|
from .errors import ApiError
|
|
from . import telemetry as tel
|
|
|
|
QUEUE = "attachment.process.v1"
|
|
DLQ = "attachment.process.v1.dlq"
|
|
|
|
|
|
def broker_url() -> str:
|
|
path = os.environ.get("BROKER_URL_FILE")
|
|
value = Path(path).read_text(encoding="utf-8").strip() if path else os.environ.get("BROKER_URL", "")
|
|
if not value.startswith(("amqp://", "amqps://")):
|
|
raise ValueError("BROKER_URL или BROKER_URL_FILE должен содержать AMQP URL")
|
|
return value
|
|
|
|
|
|
def _connection() -> pika.BlockingConnection:
|
|
connection = pika.BlockingConnection(pika.URLParameters(broker_url()))
|
|
channel = connection.channel()
|
|
channel.queue_declare(queue=QUEUE, durable=True)
|
|
channel.queue_declare(queue=DLQ, durable=True)
|
|
return connection
|
|
|
|
|
|
def _publish(payload: dict, headers: dict | None = None) -> None:
|
|
connection = _connection()
|
|
try:
|
|
channel = connection.channel()
|
|
channel.confirm_delivery()
|
|
channel.basic_publish(
|
|
exchange="", routing_key=QUEUE,
|
|
body=json.dumps(payload, separators=(",", ":")).encode("utf-8"),
|
|
properties=pika.BasicProperties(delivery_mode=pika.DeliveryMode.Persistent, content_type="application/json", headers=headers),
|
|
mandatory=True,
|
|
)
|
|
finally:
|
|
connection.close()
|
|
|
|
|
|
async def publish(payload: dict, context=None) -> None:
|
|
try:
|
|
with tel.tracer.start_as_current_span("attachment.publish", context=tel.extract(context), kind=tel.SpanKind.PRODUCER, record_exception=False, set_status_on_exception=False) as span:
|
|
span.set_attribute("messaging.retry.attempt", int(payload.get("attempt", 1)))
|
|
try:
|
|
await run_in_threadpool(_publish, payload, tel.carrier())
|
|
except Exception:
|
|
span.set_status(tel.Status(tel.StatusCode.ERROR))
|
|
raise
|
|
except (pika.exceptions.AMQPError, OSError, ValueError) as error:
|
|
raise ApiError(503, "unavailable", "Брокер недоступен") from error
|
|
|
|
|
|
async def publish_dlq(payload: dict) -> None:
|
|
payload = {**payload, "failure": payload.get('failure', 'retry_exhausted')}
|
|
try:
|
|
await run_in_threadpool(_publish_to, DLQ, payload)
|
|
except (pika.exceptions.AMQPError, OSError, ValueError) as error:
|
|
raise RuntimeError("DLQ is unavailable") from error
|
|
|
|
|
|
def _publish_to(queue: str, payload: dict) -> None:
|
|
connection = _connection()
|
|
try:
|
|
channel = connection.channel()
|
|
channel.confirm_delivery()
|
|
channel.basic_publish(
|
|
exchange="", routing_key=queue, body=json.dumps(payload, separators=(",", ":")).encode("utf-8"),
|
|
properties=pika.BasicProperties(delivery_mode=pika.DeliveryMode.Persistent, content_type="application/json"),
|
|
mandatory=True,
|
|
)
|
|
finally:
|
|
connection.close()
|
|
|
|
|
|
async def ready() -> None:
|
|
connection = None
|
|
try:
|
|
connection = await run_in_threadpool(_connection)
|
|
except (pika.exceptions.AMQPError, OSError, ValueError) as error:
|
|
raise ApiError(503, "unavailable", "Брокер недоступен") from error
|
|
finally:
|
|
if connection is not None:
|
|
await run_in_threadpool(connection.close)
|