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: