feat(health): 被动式健康检查 API(FastAPI,覆盖服务健康+数据健康)
在同步进程内内置 FastAPI/uvicorn HTTP endpoint (GET /health),返回结构化 健康快照,便于运维/监控被动探活: - 服务健康:cycle_lag(核心指标,超过 3×poll_interval 判 unhealthy)、 队列状态(pending/error/dead/cleaned 实时查)、capture 累计(enqueued/ deferred/aged_out)、最近 cycle 时间/耗时/错误。 - 数据健康:解析最近一次定时 compare_ids 报告(不跑全量 compare 太重)、 实时查 SyncLogArchive 异常痕迹(AgedOut/降级)、SyncQueue dead 行样本。 status 三态:healthy(200) / degraded(503, 有dead/error/aged_out/compare 不一致) / unhealthy(503, 主循环停滞)。状态码映射便于按码告警。 线程模型:uvicorn 占主线程,capture/apply/cleanup 循环跑后台 daemon 线程, NSSM 停服务时主线程退出、daemon 自动终止。health.enabled=false 时退化为 旧行为(同步循环占主线程)。Web 层(routes/)与业务逻辑(HealthChecker) 解耦,后期加运维接口(compare触发/死信管理/metrics)零结构改动。 零 SQL/零 schema 改动。新增依赖 fastapi/uvicorn[standard]。
This commit is contained in:
28
src/sync/web/app.py
Normal file
28
src/sync/web/app.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""FastAPI application factory.
|
||||
|
||||
``create_app(state, cfg)`` builds the app, binds the shared singletons (the
|
||||
process-wide :class:`ServiceState` and :class:`SyncConfig`) into the dependency
|
||||
graph, and mounts route modules. New operational endpoints are added by
|
||||
creating a ``routes/<name>.py`` with an ``APIRouter`` and ``include_router``-
|
||||
ing it here.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
from ..config import SyncConfig
|
||||
from ..health import ServiceState
|
||||
from . import deps
|
||||
from .routes import health as health_routes
|
||||
|
||||
|
||||
def create_app(state: ServiceState, cfg: SyncConfig) -> FastAPI:
|
||||
"""Build the FastAPI app with health state/config bound for injection."""
|
||||
app = FastAPI(
|
||||
title="DataMacroSync",
|
||||
description="Access -> SQL Server incremental sync operational API",
|
||||
version="1.0.0",
|
||||
)
|
||||
deps.bind(state, cfg)
|
||||
app.include_router(health_routes.router)
|
||||
return app
|
||||
Reference in New Issue
Block a user