|
| 1 | +import datetime |
| 2 | +import aio_pika |
| 3 | +import asyncio |
| 4 | +from utils import SETTINGS, send_email |
| 5 | + |
| 6 | + |
| 7 | +async def connect_to_rabbitmq(rabbitmq_url: str = SETTINGS.RABBITMQ_URL) -> tuple: |
| 8 | + try: |
| 9 | + print("Connecting to RabbitMQ...") |
| 10 | + connection = await aio_pika.connect_robust(rabbitmq_url, timeout=5) |
| 11 | + |
| 12 | + channel = await connection.channel() |
| 13 | + exchange = await channel.declare_exchange( |
| 14 | + SETTINGS.RABBITMQ_EXCHANGE, aio_pika.ExchangeType.TOPIC |
| 15 | + ) |
| 16 | + queue = await channel.declare_queue(SETTINGS.RABBITMQ_QUEUE, durable=True) |
| 17 | + |
| 18 | + await queue.bind(exchange, routing_key=SETTINGS.RABBITMQ_ROUTING_KEY) |
| 19 | + |
| 20 | + print("Connected to RabbitMQ") |
| 21 | + return connection, exchange, queue |
| 22 | + except Exception as e: |
| 23 | + import traceback |
| 24 | + |
| 25 | + print(f"Error connecting to RabbitMQ: {e}") |
| 26 | + traceback.print_exc() |
| 27 | + return None, None, None |
| 28 | + |
| 29 | + |
| 30 | +async def consume_rabbitmq_messages_(): |
| 31 | + connection, exchange, queue = await connect_to_rabbitmq() |
| 32 | + if not connection: |
| 33 | + print("Failed to connect to RabbitMQ. Exiting consumer.") |
| 34 | + return |
| 35 | + |
| 36 | + async with connection: |
| 37 | + async with queue.iterator() as queue_iter: |
| 38 | + async for message in queue_iter: |
| 39 | + async with message.process(): |
| 40 | + try: |
| 41 | + payload = message.body.decode() |
| 42 | + print(f"Received message from RabbitMQ: {payload}") |
| 43 | + # Assuming payload is a JSON string with required fields |
| 44 | + import json |
| 45 | + |
| 46 | + data = json.loads(payload) |
| 47 | + receiver_email = data.get("receiver_email") |
| 48 | + message_text = data.get("message_text") |
| 49 | + email_object = data.get("email_object") |
| 50 | + |
| 51 | + if not all([receiver_email, message_text, email_object]): |
| 52 | + print( |
| 53 | + f"[{datetime.datetime.now()}] Incomplete email data received: {data}" |
| 54 | + ) |
| 55 | + print("The correct format is:") |
| 56 | + print( |
| 57 | + '{"receiver_email": "email@example.com", "email_object": "Subject", "message_text": "Body"} or {"receiver_email": ["email@example.com"], "email_object": "Subject", "message_text": "Body"}' |
| 58 | + ) |
| 59 | + print("Skipping this message.") |
| 60 | + continue |
| 61 | + |
| 62 | + send_email(receiver_email, message_text, email_object) |
| 63 | + except Exception as e: |
| 64 | + print(f"Error processing message: {e}") |
| 65 | + finally: |
| 66 | + await asyncio.sleep(3) # Prevent tight loop |
| 67 | + |
| 68 | + |
| 69 | +def consume_rabbitmq_messages(): |
| 70 | + if not SETTINGS.USE_RABBITMQ: |
| 71 | + print( |
| 72 | + "RabbitMQ integration is disabled. Setting USE_RABBITMQ to True to enable it and restart the service." |
| 73 | + ) |
| 74 | + return |
| 75 | + print("Starting RabbitMQ consumer...") |
| 76 | + asyncio.run(consume_rabbitmq_messages_()) |
0 commit comments