96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
import os
|
|
import pika
|
|
import json
|
|
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', 'video_tasks')
|
|
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 publish_to_queue(payload):
|
|
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_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 callback(ch, method, properties, body):
|
|
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}")
|
|
|
|
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()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|