personabot-api / app /api /health.py
GitHub Actions
Deploy 5a96418
bbe01fe
from typing import Any
from fastapi import APIRouter, Request
from app.core.logging import get_logger
router = APIRouter()
logger = get_logger(__name__)
@router.get("/health")
async def health_check(request: Request):
"""Basic liveness probe. Returns ok if the process is running."""
client_ip = request.headers.get("X-Forwarded-For", "direct")
cf_ip = request.headers.get("CF-Connecting-IP", "none")
return {
"status": "ok",
"proxy": {
"x_forwarded_for": client_ip,
"cf_connecting_ip": cf_ip,
"via_cloudflare": cf_ip != "none",
},
}
@router.get("/ready")
async def readiness_probe(request: Request) -> dict[str, Any]:
"""Readiness probe: checks Qdrant connection and semantic cache are initialised."""
failing = []
try:
qdrant = request.app.state.qdrant
qdrant.get_collections()
except Exception as e:
logger.error("Qdrant connection failed during readiness check", exc_info=e)
failing.append("qdrant")
# Semantic cache: check it is initialised (not None).
# In-memory cache never fails to connect — only check it exists.
if getattr(request.app.state, "semantic_cache", None) is None:
failing.append("semantic_cache")
if failing:
return {"status": "degraded", "failing": failing}
return {"status": "ready"}