# Copyright 2026 zhaoxi826 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """健康检查端点:用于容器存活/就绪探针。""" from fastapi import APIRouter from fastapi.responses import JSONResponse from kilostar.utils.ray_hook import ray_actor_hook health_router = APIRouter(tags=["health"]) @health_router.get("/health/live", include_in_schema=True) async def liveness(): """存活探针:进程能响应即视为存活。""" return {"status": "alive"} @health_router.get("/health/ready", include_in_schema=True) async def readiness(): """就绪探针:检查关键依赖(Postgres / GSM Actor)是否可达。""" checks = {"postgres": False, "global_state_machine": False} try: postgres_database = ray_actor_hook("postgres_database").postgres_database await postgres_database.ping.remote() checks["postgres"] = True except Exception: pass try: gsm = ray_actor_hook("global_state_machine").global_state_machine await gsm.get_skill_list.remote() checks["global_state_machine"] = True except Exception: pass all_ok = all(checks.values()) return JSONResponse( status_code=200 if all_ok else 503, content={"status": "ready" if all_ok else "not_ready", "checks": checks}, )