Record /health accesses to diagnose the keep-alive
Browse filesSpace 睡了,说明 UptimeRobot 没拦住它。但「没人打」和「打了不算活动」
从外面看不出区别 —— 之前日志里看到的是 HEAD 请求,怀疑 HF 不把 HEAD
算作活动。
记下每次 /health 请求的 User-Agent 和间隔,读取时返回上一次的,
这样远程轮询几次就能判断保活到底有没有打进来、以及是什么请求方式。
Co-Authored-By: Claude Code <noreply@anthropic.com>
app.py
CHANGED
|
@@ -602,8 +602,24 @@ async def build_status() -> dict:
|
|
| 602 |
}
|
| 603 |
|
| 604 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 605 |
async def health_check(request):
|
| 606 |
"""给 UptimeRobot 打。"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 607 |
api_ok = await check_kook_api()
|
| 608 |
|
| 609 |
payload = {
|
|
@@ -617,6 +633,10 @@ async def health_check(request):
|
|
| 617 |
# 而不是靠猜(secret 的值读不回来)
|
| 618 |
"member_gate": "on" if permission.member_gate_configured() else "off",
|
| 619 |
"admin_gate": "on" if permission.admin_configured() else "off",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
}
|
| 621 |
return web.json_response(payload, status=200 if api_ok else 503)
|
| 622 |
|
|
|
|
| 602 |
}
|
| 603 |
|
| 604 |
|
| 605 |
+
# /health 的访问记录。Space 会睡,说明保活没打进来 —— 但到底是
|
| 606 |
+
# 「没人打」还是「打了不算活动」,从外面看不出来。记下每次请求的
|
| 607 |
+
# User-Agent 和间隔,读取时返回**上一次**(不是本次)的情况。
|
| 608 |
+
_health_hits = 0
|
| 609 |
+
_last_health_ua = ""
|
| 610 |
+
_last_health_at = 0.0
|
| 611 |
+
|
| 612 |
+
|
| 613 |
async def health_check(request):
|
| 614 |
"""给 UptimeRobot 打。"""
|
| 615 |
+
global _health_hits, _last_health_ua, _last_health_at
|
| 616 |
+
prev_hits, prev_ua = _health_hits, _last_health_ua
|
| 617 |
+
prev_gap = round(time.time() - _last_health_at, 1) if _last_health_at else None
|
| 618 |
+
|
| 619 |
+
_health_hits += 1
|
| 620 |
+
_last_health_ua = (request.headers.get("User-Agent") or "")[:90]
|
| 621 |
+
_last_health_at = time.time()
|
| 622 |
+
|
| 623 |
api_ok = await check_kook_api()
|
| 624 |
|
| 625 |
payload = {
|
|
|
|
| 633 |
# 而不是靠猜(secret 的值读不回来)
|
| 634 |
"member_gate": "on" if permission.member_gate_configured() else "off",
|
| 635 |
"admin_gate": "on" if permission.admin_configured() else "off",
|
| 636 |
+
# 上一次访问 /health 的情况 —— 用来判断保活到底有没有打进来
|
| 637 |
+
"prev_health_hits": prev_hits,
|
| 638 |
+
"prev_health_ua": prev_ua,
|
| 639 |
+
"prev_health_gap_sec": prev_gap,
|
| 640 |
}
|
| 641 |
return web.json_response(payload, status=200 if api_ok else 503)
|
| 642 |
|