Ajusta rabbitmq

This commit is contained in:
2025-08-05 03:59:08 +02:00
parent 6288d77d46
commit dd4f9fc51c

41
main.py
View File

@@ -1,6 +1,7 @@
import os
import pika
import json
import time
from components.video import process_full_video
RABBITMQ_HOST = os.environ.get('RABBITMQ_HOST', 'rabbitmq')
@@ -19,7 +20,7 @@ def publish_to_queue(payload):
host=RABBITMQ_HOST,
port=RABBITMQ_PORT,
credentials=credentials,
heartbeat=600,
heartbeat=60, # Usando heartbeat menor
blocked_connection_timeout=300
)
connection = pika.BlockingConnection(parameters)
@@ -75,21 +76,29 @@ def callback(ch, method, properties, body):
ch.basic_ack(delivery_tag=method.delivery_tag)
def main():
credentials = pika.PlainCredentials(RABBITMQ_USER, RABBITMQ_PASS)
parameters = pika.ConnectionParameters(
host=RABBITMQ_HOST,
port=RABBITMQ_PORT,
credentials=credentials,
heartbeat=600,
blocked_connection_timeout=300
)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue=RABBITMQ_QUEUE, durable=True)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue=RABBITMQ_QUEUE, on_message_callback=callback)
print(' [*] Esperando mensagens. Para sair: CTRL+C')
channel.start_consuming()
while True:
try:
credentials = pika.PlainCredentials(RABBITMQ_USER, RABBITMQ_PASS)
parameters = pika.ConnectionParameters(
host=RABBITMQ_HOST,
port=RABBITMQ_PORT,
credentials=credentials,
heartbeat=60, # Usando heartbeat menor
blocked_connection_timeout=300
)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue=RABBITMQ_QUEUE, durable=True)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue=RABBITMQ_QUEUE, on_message_callback=callback)
print(' [*] Esperando mensagens. Para sair: CTRL+C')
channel.start_consuming()
except pika.exceptions.StreamLostError as e:
print(f"Conexão perdida: {e}. Reconectando em 5s...")
time.sleep(5)
except Exception as e:
print(f"Erro inesperado: {e}. Reconectando em 5s...")
time.sleep(5)
if __name__ == "__main__":
main()