from __future__ import annotations from dataclasses import asdict, dataclass from django.db import connections from django.db.utils import OperationalError @dataclass(frozen=True) class HealthResult: ok: bool checks: dict[str, str] def to_dict(self) -> dict[str, object]: return asdict(self) def readiness() -> HealthResult: checks: dict[str, str] = {} ok = True try: with connections["default"].cursor() as cursor: cursor.execute("SELECT 1") cursor.fetchone() checks["database"] = "ok" except OperationalError as exc: ok = False checks["database"] = f"error:{exc.__class__.__name__}" return HealthResult(ok=ok, checks=checks)