feat: add /api/ready readiness probe (DB, data dir, local-first) (#1200)

/api/health is a liveness ping. This adds /api/ready as a readiness /
integrity self-check that returns 503 unless every critical subsystem is
whole, so an orchestrator (Docker/Compose/k8s) can gate traffic on real
readiness rather than mere process liveness:

  - database: opens a connection and runs SELECT 1
  - data_dir: confirms the data directory exists and is writable
  - local_first: reports whether storage stays on the host (informational;
    a remote database is a valid deployment, so it never fails readiness)

The check logic lives in src/readiness.py so it is unit-testable in
isolation; the route is a thin wrapper. Covered by tests/test_readiness.py.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
lekt8
2026-06-02 22:33:22 +08:00
committed by GitHub
parent 76a7685105
commit f2f437f4a8
3 changed files with 99 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
"""Tests for the readiness / integrity self-check (src/readiness.py)."""
from src.readiness import check_readiness
def test_readiness_reports_core_subsystems():
result = check_readiness()
assert {"ready", "version", "checks", "timestamp"}.issubset(result.keys())
checks = result["checks"]
for name in ("database", "data_dir", "local_first"):
assert name in checks, f"missing check: {name}"
# In the dev/test environment the local SQLite DB and data dir are present,
# so the critical checks must pass and overall readiness must be True.
assert checks["database"]["ok"] is True, checks["database"]
assert checks["data_dir"]["ok"] is True, checks["data_dir"]
assert result["ready"] is True, result
def test_local_first_check_is_informational_never_fatal():
result = check_readiness()
lf = result["checks"]["local_first"]
# local_first reports whether storage stays on-host but must never gate
# readiness — a remote database is a valid deployment.
assert lf["ok"] is True
assert "local" in lf