42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
from uuid import UUID
|
|
|
|
from faststream.rabbit import RabbitBroker, RabbitExchange
|
|
from pamqp.commands import Basic
|
|
|
|
from messenger.attachments.domain.jobs import JOB_TYPE
|
|
from messenger.shared.application.errors import Unavailable
|
|
from messenger.shared.infrastructure.trace_context import restored_context
|
|
|
|
EXCHANGE = RabbitExchange("attachments", declare=False)
|
|
QUEUE = "attachments.process"
|
|
DEAD_LETTERS = "attachments.process.dlq"
|
|
|
|
|
|
class RabbitJobPublisher:
|
|
__slots__ = ("_broker", "_timeout")
|
|
|
|
def __init__(self, broker: RabbitBroker, timeout: float) -> None:
|
|
self._broker = broker
|
|
self._timeout = timeout
|
|
|
|
async def publish(
|
|
self, job_id: UUID, body: bytes, trace_context: dict[str, str] | None = None
|
|
) -> None:
|
|
try:
|
|
with restored_context(trace_context):
|
|
confirmation = await self._broker.publish(
|
|
body,
|
|
exchange=EXCHANGE,
|
|
routing_key=JOB_TYPE,
|
|
mandatory=True,
|
|
persist=True,
|
|
message_id=str(job_id),
|
|
message_type=JOB_TYPE,
|
|
content_type="application/json",
|
|
timeout=self._timeout,
|
|
)
|
|
except Exception as error:
|
|
raise Unavailable("брокер не принял задание") from error
|
|
if not isinstance(confirmation, Basic.Ack):
|
|
raise Unavailable("брокер не подтвердил задание")
|