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
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import sqlite3
|
||||
import mysql.connector
|
||||
import json
|
||||
import time
|
||||
from mysql.connector import Error as MySQLError
|
||||
from botdiscord.config import get_db_config, get_db_type
|
||||
|
||||
_connection = None
|
||||
MAX_CONNECTION_RETRIES = 30
|
||||
CONNECTION_RETRY_DELAY = 2
|
||||
|
||||
def get_connection():
|
||||
global _connection
|
||||
@@ -13,39 +16,53 @@ def get_connection():
|
||||
if db_type == "mysql":
|
||||
db_config = get_db_config()
|
||||
|
||||
# Si no hay conexión o no está activa, intentamos conectar/reconectar
|
||||
if _connection is None:
|
||||
try:
|
||||
_connection = mysql.connector.connect(
|
||||
host=db_config.get("host"),
|
||||
port=db_config.get("port"),
|
||||
user=db_config.get("user"),
|
||||
password=db_config.get("password"),
|
||||
database=db_config.get("name"),
|
||||
consume_results=True,
|
||||
connection_timeout=5
|
||||
)
|
||||
except MySQLError as e:
|
||||
print(f"CRITICAL: Could not establish initial MySQL connection: {e}")
|
||||
raise
|
||||
for attempt in range(1, MAX_CONNECTION_RETRIES + 1):
|
||||
try:
|
||||
_connection = mysql.connector.connect(
|
||||
host=db_config.get("host"),
|
||||
port=db_config.get("port"),
|
||||
user=db_config.get("user"),
|
||||
password=db_config.get("password"),
|
||||
database=db_config.get("name"),
|
||||
consume_results=True,
|
||||
connection_timeout=10
|
||||
)
|
||||
print(f"[DB] MySQL conectado exitosamente")
|
||||
break
|
||||
except MySQLError as e:
|
||||
if attempt < MAX_CONNECTION_RETRIES:
|
||||
print(f"[DB] Intento {attempt}/{MAX_CONNECTION_RETRIES}: MySQL no disponible, reintentando en {CONNECTION_RETRY_DELAY}s...")
|
||||
time.sleep(CONNECTION_RETRY_DELAY)
|
||||
_connection = None
|
||||
else:
|
||||
print(f"[DB] CRITICAL: No se pudo conectar a MySQL después de {MAX_CONNECTION_RETRIES} intentos")
|
||||
raise
|
||||
|
||||
# Verificar salud de la conexión existente
|
||||
try:
|
||||
_connection.ping(reconnect=True, attempts=3, delay=1)
|
||||
except MySQLError:
|
||||
try:
|
||||
_connection = mysql.connector.connect(
|
||||
host=db_config.get("host"),
|
||||
port=db_config.get("port"),
|
||||
user=db_config.get("user"),
|
||||
password=db_config.get("password"),
|
||||
database=db_config.get("name"),
|
||||
consume_results=True,
|
||||
connection_timeout=5
|
||||
)
|
||||
except MySQLError as e:
|
||||
print(f"CRITICAL: MySQL reconnection failed: {e}")
|
||||
raise
|
||||
for attempt in range(1, 6):
|
||||
try:
|
||||
print(f"[DB] Reconectando a MySQL (intento {attempt}/5)...")
|
||||
_connection = mysql.connector.connect(
|
||||
host=db_config.get("host"),
|
||||
port=db_config.get("port"),
|
||||
user=db_config.get("user"),
|
||||
password=db_config.get("password"),
|
||||
database=db_config.get("name"),
|
||||
consume_results=True,
|
||||
connection_timeout=10
|
||||
)
|
||||
print(f"[DB] MySQL reconectado exitosamente")
|
||||
break
|
||||
except MySQLError as e:
|
||||
if attempt < 5:
|
||||
time.sleep(2)
|
||||
_connection = None
|
||||
else:
|
||||
print(f"[DB] CRITICAL: MySQL reconnection failed: {e}")
|
||||
raise
|
||||
|
||||
return _connection
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user