56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
"""健康检查端点。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter
|
|
|
|
from src import __version__
|
|
from src.engine.rapidocr import RapidOCREngine
|
|
from src.engine.paddleocr_vl import PaddleOCRVLEngine
|
|
from src.schemas.ocr import EngineStatus, HealthResponse
|
|
|
|
router = APIRouter()
|
|
|
|
# 全局引擎实例(由 main.py startup 注入)
|
|
rapidocr: RapidOCREngine | None = None
|
|
paddleocr_vl: PaddleOCRVLEngine | None = None
|
|
|
|
|
|
def init(
|
|
rapidocr_engine: RapidOCREngine,
|
|
paddleocr_vl_engine: PaddleOCRVLEngine,
|
|
) -> None:
|
|
"""注入引擎实例。"""
|
|
global rapidocr, paddleocr_vl
|
|
rapidocr = rapidocr_engine
|
|
paddleocr_vl = paddleocr_vl_engine
|
|
|
|
|
|
@router.get("/healthz", response_model=HealthResponse)
|
|
async def health() -> HealthResponse:
|
|
engines_status: dict = {}
|
|
|
|
for name, engine in [("ppocrv6", rapidocr), ("paddleocr-vl", paddleocr_vl)]:
|
|
if engine is None:
|
|
engines_status[name] = EngineStatus(status="unavailable")
|
|
else:
|
|
h = await engine.health()
|
|
engines_status[name] = EngineStatus(status=h.get("status", "error"))
|
|
|
|
# 整体状态
|
|
ok_count = sum(1 for e in engines_status.values() if e.status == "ok")
|
|
unavailable_count = sum(1 for e in engines_status.values() if e.status == "unavailable")
|
|
total = len(engines_status)
|
|
if ok_count == total:
|
|
overall = "ok"
|
|
elif unavailable_count == total:
|
|
overall = "error"
|
|
else:
|
|
overall = "degraded"
|
|
|
|
return HealthResponse(
|
|
status=overall,
|
|
engines=engines_status,
|
|
version=__version__,
|
|
)
|