|
| 1 | +from kafka import KafkaConsumer |
| 2 | +from utils import SETTINGS, send_email |
| 3 | +from datetime import datetime |
| 4 | +import json |
| 5 | +import time |
| 6 | + |
| 7 | + |
| 8 | +def make_consumer(topic, bootstrap_servers, group_id="mail_service-group"): |
| 9 | + """Create and return a Kafka consumer with exponential backoff on connection failure.""" |
| 10 | + backoff = 1 |
| 11 | + while True: |
| 12 | + try: |
| 13 | + c = KafkaConsumer( |
| 14 | + topic, |
| 15 | + bootstrap_servers=bootstrap_servers, |
| 16 | + auto_offset_reset="earliest", |
| 17 | + enable_auto_commit=True, |
| 18 | + group_id=group_id, |
| 19 | + ) |
| 20 | + print("Connected to Kafka") |
| 21 | + return c |
| 22 | + except Exception as e: |
| 23 | + print(f"Kafka connection failed: {e}. retrying in {backoff}s") |
| 24 | + time.sleep(backoff) |
| 25 | + backoff = min(backoff * 2, 30) |
| 26 | + |
| 27 | + |
| 28 | +def consume_messages(): |
| 29 | + """Consume messages from a specified Kafka topic.""" |
| 30 | + if not SETTINGS.USE_KAFKA: |
| 31 | + print( |
| 32 | + "Kafka integration is disabled. Setting USE_KAFKA to True to enable it and restart the service." |
| 33 | + ) |
| 34 | + return |
| 35 | + |
| 36 | + print("Waiting for the producer to start...") |
| 37 | + |
| 38 | + consumer = make_consumer( |
| 39 | + topic=SETTINGS.KAFKA_CONSUMER_TOPIC, |
| 40 | + bootstrap_servers=SETTINGS.KAFKA_BOOTSTRAP_SERVERS.split(","), |
| 41 | + ) |
| 42 | + |
| 43 | + print("Consumer is ready and listening for messages...") |
| 44 | + |
| 45 | + try: |
| 46 | + while True: |
| 47 | + for message in consumer: |
| 48 | + ts = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") |
| 49 | + key = message.key.decode("utf-8") if message.key else "None" |
| 50 | + data = message.value.decode("utf-8") |
| 51 | + |
| 52 | + if key == "email_topic": |
| 53 | + # Get email details from the message |
| 54 | + email_data = json.loads(data) |
| 55 | + receiver_email = email_data.get("receiver_email") |
| 56 | + email_object = email_data.get("email_object") |
| 57 | + message_text = email_data.get("message_text") |
| 58 | + |
| 59 | + if not all([receiver_email, email_object, message_text]): |
| 60 | + print(f"[{ts}] Incomplete email data received: {email_data}") |
| 61 | + print("The correct format is:") |
| 62 | + print( |
| 63 | + '{"receiver_email": "email@example.com", "email_object": "Subject", "message_text": "Body"} or {"receiver_email": ["email@example.com"], "email_object": "Subject", "message_text": "Body"}' |
| 64 | + ) |
| 65 | + print("Skipping this message.") |
| 66 | + continue |
| 67 | + |
| 68 | + print( |
| 69 | + f"[{ts}] Sending email to {receiver_email} with subject '{email_object}'" |
| 70 | + ) |
| 71 | + # Send the email |
| 72 | + send_email( |
| 73 | + receiver_email=receiver_email, |
| 74 | + message_text=message_text, |
| 75 | + email_object=email_object, |
| 76 | + ) |
| 77 | + |
| 78 | + except KeyboardInterrupt: |
| 79 | + pass |
| 80 | + finally: |
| 81 | + consumer.close() |
| 82 | + time.sleep(3) |
0 commit comments