atlan-collect / translator / consumer.py
consumer.py
Raw
import pika
import os
import sys
import json

from send import emailSender
from translate_logic import translate

MAX_RETRIES = 3

def process_message(ch, method, properties, body, retries=1):
    data = json.loads(body)
    if retries > MAX_RETRIES:
        print("Max retries exceeded. Message discarded.")
        emailSender.notification(f"Your translation has failed", reciever_address=data['email'])
        ch.basic_ack(delivery_tag=method.delivery_tag)
        return

    result, err = translate.translate_text(body, ch)
    if err:
        print(f"Translation failed: {err}")
        emailSender.notification(f"Translation failed: {err} (Retry: {retries})", reciever_address=data['email'])
        # Increment retries and call process_message with the incremented value
        return process_message(ch, method, properties, body, retries=retries+1)
    else:
        print(f"Translation successful: {result}")
        emailSender.notification(f"Translation successful: {result}", reciever_address=data['email'])
        ch.basic_ack(delivery_tag=method.delivery_tag)

def main():
    # RabbitMQ connection
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host="rabbitmq")
    )
    channel = connection.channel()

    # consume messages from translate queue
    channel.basic_consume(
        queue=os.environ.get("TRANSLATOR_QUEUE"),
        on_message_callback=process_message,
    )

    print("Waiting for messages. To exit press CTRL+C")

    try:
        channel.start_consuming()
    except KeyboardInterrupt:
        print("Interrupted")
        channel.stop_consuming()
        connection.close()

if __name__ == "__main__":
    main()