Files

30 lines
1.1 KiB
Python
Raw Permalink Normal View History

2026-09-13 16:19:24 +08:00
"""NL2SQL 依赖健康检查。"""
from __future__ import annotations
import asyncio
import time
from collections.abc import Awaitable, Callable
async def _check_one(checker: Callable[[], Awaitable[None]]) -> dict:
started = time.perf_counter()
try:
await checker()
except Exception as exc: # noqa: BLE001 对外只返回异常类型
return {
"status": f"down: {type(exc).__name__}",
"ms": round((time.perf_counter() - started) * 1000, 1),
}
return {"status": "ok", "ms": round((time.perf_counter() - started) * 1000, 1)}
async def check_nl2sql_health(checkers: dict[str, Callable[[], Awaitable[None]]]) -> dict:
"""并行检查依赖,返回不含异常详情的状态摘要。"""
names = ("mysql", "redis", "milvus", "llm")
values = await asyncio.gather(
*(_check_one(checkers[name]) for name in names if name in checkers)
)
result = {name: value for name, value in zip((name for name in names if name in checkers), values)}
result["ready"] = bool(result) and all(item["status"] == "ok" for item in result.values())
return result