forked from mtusi2026/mtusi2026lab6
37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
"""RabbitMQ с подтверждаемой публикацией и ручным ack."""
|
||
|
||
import json
|
||
from contextlib import contextmanager
|
||
|
||
import pika
|
||
|
||
from src.config import setting
|
||
|
||
QUEUE = 'attachment.process.v1'
|
||
|
||
|
||
@contextmanager
|
||
def channel(heartbeat=90):
|
||
params = pika.URLParameters(setting('BROKER_URL'))
|
||
params.heartbeat = heartbeat
|
||
params.socket_timeout = 4
|
||
params.stack_timeout = 6
|
||
params.blocked_connection_timeout = 5
|
||
connection = pika.BlockingConnection(params)
|
||
try:
|
||
result = connection.channel()
|
||
result.queue_declare(queue=QUEUE, durable=True)
|
||
yield connection, result
|
||
finally:
|
||
if connection.is_open:
|
||
connection.close()
|
||
|
||
|
||
def publish(ch, message):
|
||
ch.basic_publish(
|
||
exchange='', routing_key=QUEUE, body=json.dumps(message, separators=(',', ':')).encode(),
|
||
properties=pika.BasicProperties(content_type='application/json', delivery_mode=2,
|
||
message_id=message['job_id']),
|
||
mandatory=True,
|
||
)
|