Files
incidentops/app/api/v1/health.py

48 lines
1.1 KiB
Python
Raw Normal View History

"""Health check endpoints."""
from fastapi import APIRouter, Response, status
from app.db import db
from app.taskqueue import task_queue
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 task queue connectivity.
- Check Postgres status
- Check configured task queue backend
- Return overall healthiness
"""
checks = {
"postgres": False,
"task_queue": False,
}
try:
if db.pool:
async with db.connection() as conn:
await conn.fetchval("SELECT 1")
checks["postgres"] = True
except Exception:
pass
checks["task_queue"] = await task_queue.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,
}