2025-11-21 12:00:00 +00:00
|
|
|
"""Health check endpoints."""
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Response, status
|
|
|
|
|
|
2026-01-07 20:51:13 -05:00
|
|
|
from app.db import db
|
|
|
|
|
from app.taskqueue import task_queue
|
2025-11-21 12:00:00 +00:00
|
|
|
|
|
|
|
|
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]]:
|
|
|
|
|
"""
|
2026-01-07 20:51:13 -05:00
|
|
|
Readiness probe - checks database and task queue connectivity.
|
2025-11-21 12:00:00 +00:00
|
|
|
- Check Postgres status
|
2026-01-07 20:51:13 -05:00
|
|
|
- Check configured task queue backend
|
2025-11-21 12:00:00 +00:00
|
|
|
- Return overall healthiness
|
|
|
|
|
"""
|
|
|
|
|
checks = {
|
|
|
|
|
"postgres": False,
|
2026-01-07 20:51:13 -05:00
|
|
|
"task_queue": False,
|
2025-11-21 12:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if db.pool:
|
|
|
|
|
async with db.connection() as conn:
|
|
|
|
|
await conn.fetchval("SELECT 1")
|
|
|
|
|
checks["postgres"] = True
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
|
2026-01-07 20:51:13 -05:00
|
|
|
checks["task_queue"] = await task_queue.ping()
|
2025-11-21 12:00:00 +00:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
}
|