feat: /healthz route and Dockerfile HEALTHCHECK #36

Manually merged
jfro merged 2 commits from feat/17-healthz into main 2026-11-01 21:03:14 +00:00
Owner

Closes #17

What

  • LamplighterWeb.Healthz plug, first in the endpoint (ahead of Static, Telemetry, session and the router): GET/HEAD /healthz, no auth, no cookie, not in the request log.
  • 200 {"status":"ok","checks":{"database":"ok"}} when SELECT 1 succeeds within 2s; 503 {"status":"error","checks":{"database":"error"}} otherwise. Cache-Control: no-store.
  • Failures (error tuple, raised DBConnection.ConnectionError, exit) log healthz: database check failed after Nms: <reason> at error level. The reason never goes in the response body.
  • rel/overlays/bin/healthcheck: bash /dev/tcp probe of 127.0.0.1:$PORT/healthz, exit 0 only on 200, reason on stderr (shows in docker inspect). The final image has no curl, so no new package.
  • Dockerfile HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3.
  • Not affected by HSTS/secure cookies: no force_ssl, and the plug runs before the browser pipeline.

Checked

  • mix precommit: 800 passed; mix format --check-formatted clean.
  • New healthz_test.exs: 200 path, HEAD, POST and /healthz/extra fall through to 404, 503 + log for down/raising/exiting repos, no reason leak.
  • Dev server: curl /healthz → 200; bin/healthcheck → exit 0; against a server returning 404 → exit 1 with the status line; nothing listening → exit 1. No healthz lines in the request log.
Closes #17 **What** - `LamplighterWeb.Healthz` plug, first in the endpoint (ahead of Static, Telemetry, session and the router): `GET`/`HEAD /healthz`, no auth, no cookie, not in the request log. - `200 {"status":"ok","checks":{"database":"ok"}}` when `SELECT 1` succeeds within 2s; `503 {"status":"error","checks":{"database":"error"}}` otherwise. `Cache-Control: no-store`. - Failures (error tuple, raised `DBConnection.ConnectionError`, exit) log `healthz: database check failed after Nms: <reason>` at error level. The reason never goes in the response body. - `rel/overlays/bin/healthcheck`: bash `/dev/tcp` probe of `127.0.0.1:$PORT/healthz`, exit 0 only on 200, reason on stderr (shows in `docker inspect`). The final image has no curl, so no new package. - Dockerfile `HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3`. - Not affected by HSTS/secure cookies: no `force_ssl`, and the plug runs before the browser pipeline. **Checked** - `mix precommit`: 800 passed; `mix format --check-formatted` clean. - New `healthz_test.exs`: 200 path, HEAD, POST and `/healthz/extra` fall through to 404, 503 + log for down/raising/exiting repos, no reason leak. - Dev server: `curl /healthz` → 200; `bin/healthcheck` → exit 0; against a server returning 404 → exit 1 with the status line; nothing listening → exit 1. No healthz lines in the request log.
jfro added this to the v1.0 milestone 2026-11-01 20:18:47 +00:00
feat: /healthz route and Dockerfile HEALTHCHECK
All checks were successful
CI / test (pull_request) Successful in 26s
3ddf82c85d
GET/HEAD /healthz runs ahead of the router: no session, auth or request
log. It answers 200 when SELECT 1 succeeds within 2s, 503 otherwise, and
logs the failure reason with how long the check took (the body only says
which check failed). The image's HEALTHCHECK runs bin/healthcheck, a bash
/dev/tcp probe of $PORT, so the final image needs no curl.

Closes #17
Author
Owner

Docker check on the built image (docker build at 3ddf82c):

  • Real Postgres: docker inspect → healthy, probe exit 0.
  • DB at a dead port (RUN_MIGRATIONS=false): probe → healthcheck: HTTP/1.0 503 Service Unavailable, app logs healthz: database check failed after 2964ms: ... connection not available and request was dropped from queue.
    Note: with the pool unable to connect, DBConnection's queue gives up at ~3s (before the 2s query timeout applies), still inside the 5s HEALTHCHECK timeout.
Docker check on the built image (`docker build` at 3ddf82c): - Real Postgres: `docker inspect` → `healthy`, probe exit 0. - DB at a dead port (`RUN_MIGRATIONS=false`): probe → `healthcheck: HTTP/1.0 503 Service Unavailable`, app logs `healthz: database check failed after 2964ms: ... connection not available and request was dropped from queue`. Note: with the pool unable to connect, DBConnection's queue gives up at ~3s (before the 2s query timeout applies), still inside the 5s HEALTHCHECK timeout.
fix(healthz): bound the whole database check, not just the query
All checks were successful
CI / test (pull_request) Successful in 26s
1bbefe2d23
With a hung server the query's :timeout doesn't cover pool checkout or
Postgrex's cancel request, so a probe took 4-22s instead of 2s. Run the
check in a task and give up after the timeout.
jfro manually merged commit 43ca0c0b07 into main 2026-11-01 21:03:14 +00:00
Sign in to join this conversation.
No description provided.