From 7d4fc17567ec90101c39ca9ceec5c20712aaa613 Mon Sep 17 00:00:00 2001 From: nickpons666 Date: Fri, 20 Mar 2026 18:24:00 -0600 Subject: [PATCH] =?UTF-8?q?Fix:=20Health=20Check=20tolerante=20con=20timeo?= =?UTF-8?q?ut=2010s=20y=20m=C3=A1ximo=202=20fallos=20antes=20de=20bloquear?= =?UTF-8?q?=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- botdiscord/translate.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/botdiscord/translate.py b/botdiscord/translate.py index 1ecd79e..dde6df9 100644 --- a/botdiscord/translate.py +++ b/botdiscord/translate.py @@ -68,10 +68,12 @@ async def _translate_segment(session, url, segment, target_code): _libretranslate_healthy: bool = True _last_health_check: float = 0 +_consecutive_failures: int = 0 +_MAX_FAILURES = 2 # Tolerar hasta 2 fallos antes de marcar como caído async def check_libretranslate_health(url: str) -> bool: """Verifica si LibreTranslate está disponible. Cachea el resultado 30 segundos.""" - global _libretranslate_healthy, _last_health_check + global _libretranslate_healthy, _last_health_check, _consecutive_failures import time now = time.monotonic() if now - _last_health_check < 30: @@ -79,14 +81,27 @@ async def check_libretranslate_health(url: str) -> bool: _last_health_check = now try: + check_url = url.replace("/translate", "/languages") + if "/languages" not in check_url: + check_url = url.rstrip("/") + "/../languages" async with aiohttp.ClientSession() as session: - async with session.get(url.replace("/translate", "/languages"), timeout=3) as resp: - _libretranslate_healthy = resp.status == 200 - except Exception: - _libretranslate_healthy = False + async with session.get(check_url, timeout=10) as resp: + if resp.status == 200: + _consecutive_failures = 0 + _libretranslate_healthy = True + else: + _consecutive_failures += 1 + except Exception as e: + _consecutive_failures += 1 + log.warning(f"⚠️ LibreTranslate health check falló (intento {_consecutive_failures}/{_MAX_FAILURES}): {e}") + + if _consecutive_failures >= _MAX_FAILURES: + _libretranslate_healthy = False + log.warning("⚠️ LibreTranslate no está disponible tras varios intentos.") + elif _consecutive_failures > 0: + # Un solo fallo no bloquea el servicio + _libretranslate_healthy = True - if not _libretranslate_healthy: - log.warning("⚠️ LibreTranslate no está disponible o tardó demasiado en responder.") return _libretranslate_healthy async def translate_text(text: str, target_lang: str) -> str: