47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
|
|
"""Health check endpoints."""
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Response, status
|
||
|
|
|
||
|
|
from app.db import db, redis_client
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/healthz")
|
||
|
|
async def healthz() -> dict[str, str]:
|
||
|
|
"""Liveness probe - returns 200 if the service is running."""
|
||
|
|
return {"status": "ok"}
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/readyz")
|
||
|
|
async def readyz(response: Response) -> dict[str, str | dict[str, bool]]:
|
||
|
|
"""
|
||
|
|
Readiness probe - checks database and Redis connectivity.
|
||
|
|
- Check Postgres status
|
||
|
|
- Check Redis status
|
||
|
|
- Return overall healthiness
|
||
|
|
"""
|
||
|
|
checks = {
|
||
|
|
"postgres": False,
|
||
|
|
"redis": False,
|
||
|
|
}
|
||
|
|
|
||
|
|
try:
|
||
|
|
if db.pool:
|
||
|
|
async with db.connection() as conn:
|
||
|
|
await conn.fetchval("SELECT 1")
|
||
|
|
checks["postgres"] = True
|
||
|
|
except Exception:
|
||
|
|
pass
|
||
|
|
|
||
|
|
checks["redis"] = await redis_client.ping()
|
||
|
|
|
||
|
|
all_healthy = all(checks.values())
|
||
|
|
if not all_healthy:
|
||
|
|
response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
|
||
|
|
|
||
|
|
return {
|
||
|
|
"status": "ok" if all_healthy else "degraded",
|
||
|
|
"checks": checks,
|
||
|
|
}
|