Files
traduccion_bots/iniciar_todo.py
nickpons666 ae51b6333c fix: Añadir reintentos de conexión MySQL y limitar reinicios de servicios
- get_connection() reintenta hasta 30 veces con delays de 2s
- iniciar_todo.py limita reinicios a 3 por servicio con cooldown de 60s
- Evitar loops infinitos cuando MySQL no está disponible
2026-03-20 03:55:22 -06:00

107 lines
3.6 KiB
Python
Executable File

#!/usr/bin/env python3
import subprocess
import sys
import os
import signal
import time
from datetime import datetime
LOG_DIR = "/app/data/logs"
os.makedirs(LOG_DIR, exist_ok=True)
def get_timestamp():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_file = open(f"{LOG_DIR}/bots.log", "a")
def log(msg):
timestamp = get_timestamp()
log_line = f"[{timestamp}] {msg}\n"
log_file.write(log_line)
log_file.flush()
print(msg)
print("🤖 Iniciando Bots de Traducción...")
print("=" * 40)
processes = []
restart_counts = {}
MAX_RESTARTS = 3
RESTART_COOLDOWN = 60
def start_process(cmd, name, delay_before=0):
if delay_before > 0:
log(f"⏳ Esperando {delay_before}s antes de iniciar {name}...")
time.sleep(delay_before)
log(f"✅ Iniciando {name}...")
log_path = f"{LOG_DIR}/{name.lower().replace(' ', '_')}.log"
with open(log_path, "a") as f:
p = subprocess.Popen(
cmd,
stdout=f,
stderr=subprocess.STDOUT,
preexec_fn=os.setsid
)
processes.append((p, name))
return p
p1 = start_process(["python3", "-X", "faulthandler", "-X", "importsys", "botdiscord/bot.py"], "Discord Bot", delay_before=10)
p2 = start_process(["python3", "-X", "faulthandler", "-X", "importsys", "bottelegram/telegram_bot.py"], "Telegram Bot", delay_before=5)
p3 = start_process(["python3", "-X", "faulthandler", "-X", "importsys", "panel/main.py"], "Panel Web", delay_before=0)
print("=" * 40)
print("🎉 Todos los servicios funcionando!")
print(f" 📍 Panel: http://localhost:8000")
print(f" 📁 Logs: {LOG_DIR}")
print("=" * 40)
print("\nCtrl+C para detener")
def signal_handler(sig, frame):
log("🛑 Deteniendo...")
for p, name in processes:
try:
os.killpg(os.getpgid(p.pid), signal.SIGTERM)
log(f"{name} detenido")
except:
pass
log_file.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
while True:
for p, name in processes:
if p.poll() is not None:
if name not in restart_counts:
restart_counts[name] = {"count": 0, "last_restart": 0}
current_time = time.time()
if current_time - restart_counts[name]["last_restart"] > RESTART_COOLDOWN:
restart_counts[name]["count"] = 0
restart_counts[name]["count"] += 1
restart_counts[name]["last_restart"] = current_time
if restart_counts[name]["count"] > MAX_RESTARTS:
log(f"{name} se detuvo {restart_counts[name]['count']} veces. Demasiados reinicios. Esperando {RESTART_COOLDOWN}s...")
time.sleep(RESTART_COOLDOWN)
restart_counts[name]["count"] = 0
continue
log(f"⚠️ {name} se detuvo (intento {restart_counts[name]['count']}/{MAX_RESTARTS}), reiniciando...")
delay = 5 if name != "Discord Bot" else 10
if name == "Discord Bot":
p1 = start_process(["python3", "-X", "faulthandler", "-X", "importsys", "botdiscord/bot.py"], "Discord Bot", delay_before=delay)
elif name == "Telegram Bot":
p2 = start_process(["python3", "-X", "faulthandler", "-X", "importsys", "bottelegram/telegram_bot.py"], "Telegram Bot", delay_before=delay)
elif name == "Panel Web":
p3 = start_process(["python3", "-X", "faulthandler", "-X", "importsys", "panel/main.py"], "Panel Web", delay_before=0)
time.sleep(5)