2026-04-08 20:02:05 +08:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
"""
|
|
|
|
|
|
独立进程:轮询 Fish API 的两个结果接口,用 loguru 输出响应。
|
|
|
|
|
|
|
2026-04-09 11:54:30 +08:00
|
|
|
|
接口每次 GET 会「消费」一条未投递快照;仅当响应头 X-Fish-Biomass-New: 1(或 JSON code!=200)时打日志。
|
|
|
|
|
|
|
2026-04-08 20:02:05 +08:00
|
|
|
|
BIOMASS_API_BASE=http://127.0.0.1:8000 POLL_INTERVAL=5 \\
|
|
|
|
|
|
python scripts/biomass_poller.py
|
|
|
|
|
|
|
|
|
|
|
|
依赖(与 fish_api dev 组一致):httpx、loguru
|
|
|
|
|
|
cd fish_api && pip install -e ".[dev]"
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
import json
|
|
|
|
|
|
import os
|
|
|
|
|
|
import sys
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _fmt_body(resp: httpx.Response) -> Any:
|
|
|
|
|
|
try:
|
|
|
|
|
|
return resp.json()
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
return resp.text
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-04-09 11:54:30 +08:00
|
|
|
|
def _should_log(resp: httpx.Response, body: Any) -> bool:
|
|
|
|
|
|
"""有新投递的快照(X-Fish-Biomass-New: 1)或业务错误(JSON code!=200)时打日志。"""
|
2026-04-08 21:01:47 +08:00
|
|
|
|
if not isinstance(body, dict):
|
2026-04-09 11:54:30 +08:00
|
|
|
|
return bool(body)
|
2026-04-08 21:01:47 +08:00
|
|
|
|
if body.get("code") != 200:
|
2026-04-09 11:54:30 +08:00
|
|
|
|
return True
|
|
|
|
|
|
return resp.headers.get("X-Fish-Biomass-New", "").strip() == "1"
|
2026-04-08 21:01:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-04-08 20:02:05 +08:00
|
|
|
|
async def poll_once(client: httpx.AsyncClient, base: str) -> None:
|
|
|
|
|
|
base = base.rstrip("/")
|
|
|
|
|
|
camera_url = f"{base}/api/v1/biomass/real/camera/"
|
|
|
|
|
|
health_url = f"{base}/api/v1/biomass/health/result/"
|
|
|
|
|
|
r1 = await client.get(camera_url)
|
|
|
|
|
|
r2 = await client.get(health_url)
|
2026-04-08 21:01:47 +08:00
|
|
|
|
b1 = _fmt_body(r1)
|
|
|
|
|
|
b2 = _fmt_body(r2)
|
2026-04-09 11:54:30 +08:00
|
|
|
|
if _should_log(r1, b1):
|
2026-04-08 21:01:47 +08:00
|
|
|
|
logger.info("[real/camera/] HTTP {} | {}", r1.status_code, json.dumps(b1, ensure_ascii=False))
|
2026-04-09 11:54:30 +08:00
|
|
|
|
if _should_log(r2, b2):
|
2026-04-08 21:01:47 +08:00
|
|
|
|
logger.info("[health/result/] HTTP {} | {}", r2.status_code, json.dumps(b2, ensure_ascii=False))
|
2026-04-08 20:02:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def poll_loop(base: str, interval: float) -> None:
|
|
|
|
|
|
async with httpx.AsyncClient(timeout=30.0) as client:
|
|
|
|
|
|
while True:
|
|
|
|
|
|
try:
|
|
|
|
|
|
await poll_once(client, base)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
logger.exception("poll round failed")
|
|
|
|
|
|
await asyncio.sleep(max(interval, 0.5))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Poll Fish API biomass GET endpoints.")
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
|
"--base-url",
|
|
|
|
|
|
default=os.environ.get("BIOMASS_API_BASE", "http://127.0.0.1:8000"),
|
|
|
|
|
|
help="Fish API 根 URL(默认 BIOMASS_API_BASE 或 http://127.0.0.1:8000)",
|
|
|
|
|
|
)
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
|
"--interval",
|
|
|
|
|
|
type=float,
|
|
|
|
|
|
default=float(os.environ.get("POLL_INTERVAL", "5")),
|
|
|
|
|
|
help="轮询间隔秒数(默认 POLL_INTERVAL 或 5)",
|
|
|
|
|
|
)
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
logger.remove()
|
|
|
|
|
|
logger.add(
|
|
|
|
|
|
sys.stderr,
|
|
|
|
|
|
level=os.environ.get("LOG_LEVEL", "INFO"),
|
|
|
|
|
|
format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
|
|
|
|
|
|
"<level>{level: <8}</level> | "
|
|
|
|
|
|
"<level>{message}</level>",
|
|
|
|
|
|
)
|
|
|
|
|
|
logger.info(
|
|
|
|
|
|
"biomass_poller 启动 | base={} | interval={}s | GET /api/v1/biomass/real/camera/ 与 /health/result/",
|
|
|
|
|
|
args.base_url.rstrip("/"),
|
|
|
|
|
|
args.interval,
|
|
|
|
|
|
)
|
|
|
|
|
|
asyncio.run(poll_loop(args.base_url, args.interval))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
main()
|