forked from mtusi2026/mtusi2026lab7
add level 3
This commit is contained in:
+10
-3
@@ -1,6 +1,7 @@
|
||||
"""Small durable RabbitMQ adapter; broker messages contain no attachment bytes."""
|
||||
import json
|
||||
import os
|
||||
import ssl
|
||||
from pathlib import Path
|
||||
|
||||
import pika
|
||||
@@ -16,13 +17,19 @@ 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")
|
||||
if not value.startswith("amqps://"):
|
||||
raise ValueError("BROKER_URL или BROKER_URL_FILE должен содержать AMQPS URL")
|
||||
return value
|
||||
|
||||
|
||||
def _connection() -> pika.BlockingConnection:
|
||||
connection = pika.BlockingConnection(pika.URLParameters(broker_url()))
|
||||
url = broker_url()
|
||||
parameters = pika.URLParameters(url)
|
||||
ca_file = os.environ.get("BROKER_CA_FILE")
|
||||
if not ca_file:
|
||||
raise ValueError("BROKER_CA_FILE is required for verified broker TLS")
|
||||
parameters.ssl_options = pika.SSLOptions(ssl.create_default_context(cafile=ca_file), parameters.host)
|
||||
connection = pika.BlockingConnection(parameters)
|
||||
channel = connection.channel()
|
||||
channel.queue_declare(queue=QUEUE, durable=True)
|
||||
channel.queue_declare(queue=DLQ, durable=True)
|
||||
|
||||
Reference in New Issue
Block a user