fix(notify): increase gateway timeouts + retry OpenClaw sends

This commit is contained in:
Abhimanyu Saharan
2026-02-02 21:03:39 +05:30
parent 8cd32124cd
commit 5877b4a17b
2 changed files with 51 additions and 12 deletions

View File

@@ -185,7 +185,7 @@ def notify_openclaw(session: Session, ctx: NotifyContext) -> None:
client.tools_invoke( client.tools_invoke(
"sessions_send", "sessions_send",
{"sessionKey": sk, "message": message}, {"sessionKey": sk, "message": message},
timeout_s=3.0, timeout_s=15.0,
) )
except Exception: except Exception:
logger.exception("notify_openclaw: sessions_send failed") logger.exception("notify_openclaw: sessions_send failed")

View File

@@ -2,9 +2,11 @@ from __future__ import annotations
import logging import logging
import os import os
import time
from typing import Any from typing import Any
import requests import requests
from requests.exceptions import ReadTimeout, RequestException
logger = logging.getLogger("app.openclaw") logger = logging.getLogger("app.openclaw")
@@ -16,6 +18,15 @@ class OpenClawClient:
@classmethod @classmethod
def from_env(cls) -> "OpenClawClient | None": def from_env(cls) -> "OpenClawClient | None":
# Ensure .env is loaded into os.environ (pydantic Settings reads env_file but
# does not automatically populate os.environ).
try:
from dotenv import load_dotenv
load_dotenv(override=False)
except Exception:
pass
url = os.environ.get("OPENCLAW_GATEWAY_URL") url = os.environ.get("OPENCLAW_GATEWAY_URL")
token = os.environ.get("OPENCLAW_GATEWAY_TOKEN") token = os.environ.get("OPENCLAW_GATEWAY_TOKEN")
if not url or not token: if not url or not token:
@@ -28,21 +39,49 @@ class OpenClawClient:
args: dict[str, Any], args: dict[str, Any],
*, *,
session_key: str | None = None, session_key: str | None = None,
timeout_s: float = 5.0, timeout_s: float = 10.0,
) -> dict[str, Any]: ) -> dict[str, Any]:
payload: dict[str, Any] = {"tool": tool, "args": args} payload: dict[str, Any] = {"tool": tool, "args": args}
logger.info( logger.info(
"openclaw.tools_invoke", extra={"tool": tool, "has_session_key": bool(session_key)} "openclaw.tools_invoke",
extra={"tool": tool, "has_session_key": bool(session_key), "timeout_s": timeout_s},
) )
if session_key is not None: if session_key is not None:
payload["sessionKey"] = session_key payload["sessionKey"] = session_key
r = requests.post( last_err: Exception | None = None
f"{self.base_url}/tools/invoke", for attempt in range(2):
headers={"Authorization": f"Bearer {self.token}", "Content-Type": "application/json"}, try:
json=payload, r = requests.post(
timeout=timeout_s, f"{self.base_url}/tools/invoke",
) headers={
r.raise_for_status() "Authorization": f"Bearer {self.token}",
logger.info("openclaw.tools_invoke: ok", extra={"tool": tool, "status": r.status_code}) "Content-Type": "application/json",
return r.json() },
json=payload,
# connect timeout, read timeout
timeout=(2.0, timeout_s),
)
r.raise_for_status()
logger.info(
"openclaw.tools_invoke: ok",
extra={"tool": tool, "status": r.status_code, "attempt": attempt + 1},
)
return r.json()
except ReadTimeout as e:
last_err = e
logger.warning(
"openclaw.tools_invoke: timeout",
extra={"tool": tool, "attempt": attempt + 1, "timeout_s": timeout_s},
)
time.sleep(0.2 * (attempt + 1))
except RequestException as e:
last_err = e
logger.warning(
"openclaw.tools_invoke: request error",
extra={"tool": tool, "attempt": attempt + 1, "error": str(e)},
)
time.sleep(0.2 * (attempt + 1))
assert last_err is not None
raise last_err