jean-router: WoL plus robuste + retry sur echec upstream

- Retry avec WoL quand la requete upstream echoue (connexion refusee)
- Logs plus verbeux dans wait_for_jean
- Cache jean_alive reduit a 2s
This commit is contained in:
Folzo
2026-08-01 08:51:30 +02:00
parent 4f8a5288ff
commit f2bcfb98e3
+17 -2
View File
@@ -108,7 +108,7 @@ def send_wol():
def jean_alive() -> bool: def jean_alive() -> bool:
"""Verifie si Jean est accessible""" """Verifie si Jean est accessible"""
now = time.time() now = time.time()
if now - jean_alive_cache["timestamp"] < 5: if now - jean_alive_cache["timestamp"] < 2:
return jean_alive_cache["alive"] return jean_alive_cache["alive"]
try: try:
@@ -125,7 +125,8 @@ def jean_alive() -> bool:
def wait_for_jean(trigger_wol=True): def wait_for_jean(trigger_wol=True):
"""Attend que Jean soit accessible. Si ENABLE_WOL et trigger_wol, envoie WoL d'abord.""" """Attend que Jean soit accessible. Si ENABLE_WOL et trigger_wol, envoie WoL d'abord."""
if jean_alive(): alive = jean_alive()
if alive:
return return
if not ENABLE_WOL or not trigger_wol: if not ENABLE_WOL or not trigger_wol:
@@ -140,10 +141,12 @@ def wait_for_jean(trigger_wol=True):
deadline = time.time() + WAKE_TIMEOUT deadline = time.time() + WAKE_TIMEOUT
while time.time() < deadline: while time.time() < deadline:
remaining = int(deadline - time.time())
if jean_alive(): if jean_alive():
jean_alive_cache["timestamp"] = 0 jean_alive_cache["timestamp"] = 0
log.info(f"[WoL] Jean UP apres {int(time.time() - deadline + WAKE_TIMEOUT)}s") log.info(f"[WoL] Jean UP apres {int(time.time() - deadline + WAKE_TIMEOUT)}s")
return return
log.info(f"[WoL] En attente... ({remaining}s restantes)")
time.sleep(CHECK_INTERVAL) time.sleep(CHECK_INTERVAL)
log.error(f"[WoL] Jean ne repond pas apres {WAKE_TIMEOUT}s") log.error(f"[WoL] Jean ne repond pas apres {WAKE_TIMEOUT}s")
@@ -819,6 +822,18 @@ def chat_completions():
) )
except Exception as e: except Exception as e:
log.error(f"[Router] Erreur upstream: {e}") log.error(f"[Router] Erreur upstream: {e}")
if ENABLE_WOL:
log.info("[Router] Tentative WoL + retry apres echec upstream...")
wait_for_jean(trigger_wol=True)
try:
resp = requests.request(
method=request.method, url=upstream_url,
headers=headers, json=data, stream=True, timeout=None,
)
except Exception as e2:
log.error(f"[Router] Echec apres WoL: {e2}")
return jsonify({"error": str(e2)}), 502
else:
return jsonify({"error": str(e)}), 502 return jsonify({"error": str(e)}), 502
excluded = {"content-encoding", "content-length", "transfer-encoding", "connection"} excluded = {"content-encoding", "content-length", "transfer-encoding", "connection"}