forked from mtusi2026/mtusi2026lab6
add level 3 and 4
This commit is contained in:
+7
-4
@@ -7,6 +7,7 @@ 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"
|
||||
@@ -28,7 +29,7 @@ def _connection() -> pika.BlockingConnection:
|
||||
return connection
|
||||
|
||||
|
||||
def _publish(payload: dict) -> None:
|
||||
def _publish(payload: dict, headers: dict | None = None) -> None:
|
||||
connection = _connection()
|
||||
try:
|
||||
channel = connection.channel()
|
||||
@@ -36,16 +37,18 @@ def _publish(payload: dict) -> None:
|
||||
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"),
|
||||
properties=pika.BasicProperties(delivery_mode=pika.DeliveryMode.Persistent, content_type="application/json", headers=headers),
|
||||
mandatory=True,
|
||||
)
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
|
||||
async def publish(payload: dict) -> None:
|
||||
async def publish(payload: dict, context=None) -> None:
|
||||
try:
|
||||
await run_in_threadpool(_publish, payload)
|
||||
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)))
|
||||
await run_in_threadpool(_publish, payload, tel.carrier())
|
||||
except (pika.exceptions.AMQPError, OSError, ValueError) as error:
|
||||
raise ApiError(503, "unavailable", "Брокер недоступен") from error
|
||||
|
||||
|
||||
Reference in New Issue
Block a user