mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-18 17:13:25 +08:00
初始化:独立 oclaw 仓库首提交
- 在 oclaw/ 下重新初始化 Git 仓库 - 补齐子仓库 .gitignore,避免提交本地运行态数据(_local、node_modules、logs 等) - 提交当前工程代码与配置 Made-with: Cursor
This commit is contained in:
commit
ba3836f00f
579 changed files with 83112 additions and 0 deletions
60
gateway/server_methods/health.py
Normal file
60
gateway/server_methods/health.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from .shared_types import GatewayRequestHandlers
|
||||
from .validation import error_shape
|
||||
|
||||
HEALTH_REFRESH_INTERVAL_MS = 5_000
|
||||
ADMIN_SCOPE = "operator.admin"
|
||||
|
||||
|
||||
def _health_handler(opts: dict[str, Any]) -> Any:
|
||||
respond = opts.get("respond")
|
||||
context = opts.get("context") or {}
|
||||
params = opts.get("params") or {}
|
||||
if not callable(respond):
|
||||
return None
|
||||
wants_probe = bool(params.get("probe") is True)
|
||||
get_health_cache = context.get("get_health_cache")
|
||||
refresh_health_snapshot = context.get("refresh_health_snapshot")
|
||||
now = __import__("time").time() * 1000
|
||||
cached = get_health_cache() if callable(get_health_cache) else None
|
||||
if (
|
||||
not wants_probe
|
||||
and isinstance(cached, dict)
|
||||
and isinstance(cached.get("ts"), (int, float))
|
||||
and now - float(cached["ts"]) < HEALTH_REFRESH_INTERVAL_MS
|
||||
):
|
||||
respond(True, cached, None, {"cached": True})
|
||||
return None
|
||||
try:
|
||||
snap = refresh_health_snapshot({"probe": wants_probe}) if callable(refresh_health_snapshot) else {}
|
||||
respond(True, snap, None, None)
|
||||
except Exception as exc:
|
||||
respond(False, None, error_shape("UNAVAILABLE", str(exc)), None)
|
||||
return None
|
||||
|
||||
|
||||
def _status_handler(opts: dict[str, Any]) -> Any:
|
||||
respond = opts.get("respond")
|
||||
client = opts.get("client") or {}
|
||||
if not callable(respond):
|
||||
return None
|
||||
scopes = []
|
||||
connect = client.get("connect")
|
||||
if isinstance(connect, dict) and isinstance(connect.get("scopes"), list):
|
||||
scopes = [str(x) for x in connect.get("scopes", [])]
|
||||
status = {
|
||||
"includeSensitive": ADMIN_SCOPE in scopes,
|
||||
"ok": True,
|
||||
}
|
||||
respond(True, status, None, None)
|
||||
return None
|
||||
|
||||
|
||||
health_handlers: GatewayRequestHandlers = {
|
||||
"health": _health_handler,
|
||||
"status": _status_handler,
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue