Files
video-render/main.py

104 lines
3.4 KiB
Python

import os
import pika
import json
import time
from components.video import process_full_video
RABBITMQ_HOST = os.environ.get('RABBITMQ_HOST', 'rabbitmq')
RABBITMQ_PORT = int(os.environ.get('RABBITMQ_PORT', 5672))
RABBITMQ_USER = os.environ.get('RABBITMQ_USER', 'admin')
RABBITMQ_PASS = os.environ.get('RABBITMQ_PASS')
RABBITMQ_QUEUE = os.environ.get('RABBITMQ_QUEUE', 'to-render')
RABBITMQ_UPLOAD_QUEUE = os.environ.get('RABBITMQ_UPLOAD_QUEUE', 'to-upload')
if not RABBITMQ_PASS:
raise RuntimeError("RABBITMQ_PASS não definido no ambiente")
def get_next_message():
credentials = pika.PlainCredentials(RABBITMQ_USER, RABBITMQ_PASS)
parameters = pika.ConnectionParameters(
host=RABBITMQ_HOST,
port=RABBITMQ_PORT,
credentials=credentials,
heartbeat=60,
blocked_connection_timeout=300
)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
method_frame, header_frame, body = channel.basic_get(RABBITMQ_QUEUE)
if method_frame:
channel.basic_ack(method_frame.delivery_tag)
connection.close()
return body
else:
connection.close()
return None
def publish_to_queue(payload):
credentials = pika.PlainCredentials(RABBITMQ_USER, RABBITMQ_PASS)
parameters = pika.ConnectionParameters(
host=RABBITMQ_HOST,
port=RABBITMQ_PORT,
credentials=credentials,
heartbeat=60,
blocked_connection_timeout=300
)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue=RABBITMQ_UPLOAD_QUEUE, durable=True)
channel.basic_publish(
exchange='',
routing_key=RABBITMQ_UPLOAD_QUEUE,
body=json.dumps(payload),
properties=pika.BasicProperties(
delivery_mode=2, # persistente
)
)
connection.close()
def main():
print(' [*] Esperando mensagens. Para sair: CTRL+C')
while True:
body = get_next_message()
if body is None:
time.sleep(5)
continue
try:
data = json.loads(body)
filename = data.get("filename")
times = data.get("times", [])
url = data.get("url")
video_id = data.get("videoId")
print(f"Processando vídeo: {filename}")
processed_files = process_full_video(filename, times)
payload = {
"videosProcessedQuantity": len(processed_files),
"filename": filename,
"processedFiles": processed_files,
"url": url,
"videoId": video_id,
"error": False,
}
except Exception as e:
payload = {
"videosProcessedQuantity": 0,
"filename": filename if 'filename' in locals() else None,
"processedFiles": [],
"url": url if 'url' in locals() else None,
"videoId": video_id if 'video_id' in locals() else None,
"error": str(e),
}
print(f"Erro no processamento: {e}")
try:
publish_to_queue(payload)
print(f"Mensagem publicada na fila '{RABBITMQ_UPLOAD_QUEUE}'.")
except Exception as publish_err:
print(f"Erro ao publicar na fila '{RABBITMQ_UPLOAD_QUEUE}': {publish_err}")
if __name__ == "__main__":
main()