"""容器健康检查:判断进程是否还"在干活",而不只是端口通。 规则: - 夜间暂停 / 尚未建立 Cookie 校验 -> 视为健康(属于预期状态) - 最近一次成功轮询距今超过 15 分钟(且不是夜间)-> 不健康,交给 Docker 重启 """ from __future__ import annotations import json import sys import urllib.request def main() -> int: try: with urllib.request.urlopen("http://127.0.0.1:8089/healthz", timeout=5) as resp: data = json.load(resp) except Exception as exc: # noqa: BLE001 print(f"healthz 不可达: {exc}") return 1 mode = data.get("mode") age = data.get("last_poll_age_sec") print(json.dumps(data, ensure_ascii=False)) if mode == "night": return 0 if age is None: # 启动阶段还没跑出第一轮,给 15 分钟宽限 return 0 if (data.get("uptime_sec") or 0) < 900 else 1 return 0 if age < 900 else 1 if __name__ == "__main__": sys.exit(main())