Fix: Health Check tolerante con timeout 10s y máximo 2 fallos antes de bloquear (#9)
This commit is contained in:
@@ -68,10 +68,12 @@ async def _translate_segment(session, url, segment, target_code):
|
|||||||
|
|
||||||
_libretranslate_healthy: bool = True
|
_libretranslate_healthy: bool = True
|
||||||
_last_health_check: float = 0
|
_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:
|
async def check_libretranslate_health(url: str) -> bool:
|
||||||
"""Verifica si LibreTranslate está disponible. Cachea el resultado 30 segundos."""
|
"""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
|
import time
|
||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
if now - _last_health_check < 30:
|
if now - _last_health_check < 30:
|
||||||
@@ -79,14 +81,27 @@ async def check_libretranslate_health(url: str) -> bool:
|
|||||||
|
|
||||||
_last_health_check = now
|
_last_health_check = now
|
||||||
try:
|
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 aiohttp.ClientSession() as session:
|
||||||
async with session.get(url.replace("/translate", "/languages"), timeout=3) as resp:
|
async with session.get(check_url, timeout=10) as resp:
|
||||||
_libretranslate_healthy = resp.status == 200
|
if resp.status == 200:
|
||||||
except Exception:
|
_consecutive_failures = 0
|
||||||
_libretranslate_healthy = False
|
_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
|
return _libretranslate_healthy
|
||||||
|
|
||||||
async def translate_text(text: str, target_lang: str) -> str:
|
async def translate_text(text: str, target_lang: str) -> str:
|
||||||
|
|||||||
Reference in New Issue
Block a user