37 lines
861 B
Python
37 lines
861 B
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pika
|
|
|
|
|
|
QUEUE_NAME = "attachment.process.v1"
|
|
|
|
|
|
def connect_broker():
|
|
url = Path(os.environ["BROKER_URL_FILE"]).read_text().strip()
|
|
if not url:
|
|
raise ValueError("Empty broker URL file")
|
|
|
|
parameters = pika.URLParameters(url)
|
|
parameters.heartbeat = 15
|
|
parameters.blocked_connection_timeout = 5
|
|
parameters.socket_timeout = 5
|
|
parameters.stack_timeout = 10
|
|
parameters.connection_attempts = 1
|
|
|
|
connection = pika.BlockingConnection(parameters)
|
|
try:
|
|
channel = connection.channel()
|
|
channel.queue_declare(
|
|
queue=QUEUE_NAME,
|
|
durable=True,
|
|
exclusive=False,
|
|
auto_delete=False,
|
|
)
|
|
return connection, channel
|
|
except Exception:
|
|
connection.close()
|
|
raise
|