diff --git a/jean-router/app.py b/jean-router/app.py index c12ff2d..52919e7 100644 --- a/jean-router/app.py +++ b/jean-router/app.py @@ -108,7 +108,7 @@ def send_wol(): def jean_alive() -> bool: """Verifie si Jean est accessible""" now = time.time() - if now - jean_alive_cache["timestamp"] < 5: + if now - jean_alive_cache["timestamp"] < 2: return jean_alive_cache["alive"] try: @@ -125,7 +125,8 @@ def jean_alive() -> bool: def wait_for_jean(trigger_wol=True): """Attend que Jean soit accessible. Si ENABLE_WOL et trigger_wol, envoie WoL d'abord.""" - if jean_alive(): + alive = jean_alive() + if alive: return if not ENABLE_WOL or not trigger_wol: @@ -140,10 +141,12 @@ def wait_for_jean(trigger_wol=True): deadline = time.time() + WAKE_TIMEOUT while time.time() < deadline: + remaining = int(deadline - time.time()) if jean_alive(): jean_alive_cache["timestamp"] = 0 log.info(f"[WoL] Jean UP apres {int(time.time() - deadline + WAKE_TIMEOUT)}s") return + log.info(f"[WoL] En attente... ({remaining}s restantes)") time.sleep(CHECK_INTERVAL) log.error(f"[WoL] Jean ne repond pas apres {WAKE_TIMEOUT}s") @@ -819,7 +822,19 @@ def chat_completions(): ) except Exception as e: log.error(f"[Router] Erreur upstream: {e}") - return jsonify({"error": str(e)}), 502 + 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 excluded = {"content-encoding", "content-length", "transfer-encoding", "connection"} response_headers = [