Files
VacatureRadar/apps/core/health.py
T
Jens b8091e59bd
deploy / deploy (push) Canceled after 0s
Initial deploy setup
2026-07-21 14:00:00 +02:00

30 lines
730 B
Python

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)