Add JSON health check with database and tablebase status (#106)

This commit is contained in:
2026-11-09 22:24:09 +01:00
parent 4c39f231f1
commit 56d51347b9
4 changed files with 135 additions and 3 deletions
+62
View File
@@ -0,0 +1,62 @@
defmodule Chesstrainer.Health do
@moduledoc """
Runtime health snapshot for deploy smoke tests and uptime monitors.
The database is the only hard dependency. Tablebase status is informational:
local Syzygy files are preferred, but falling back to Lichess is not an
outage, so a missing or broken local cache reports `degraded` with HTTP 200.
"""
alias Chesstrainer.Endgames.Tablebase.Local
alias Chesstrainer.Repo
@type status :: %{
status: String.t(),
db: String.t(),
tablebase: String.t(),
process: String.t()
}
@doc "Returns the current health status."
@spec status() :: status()
def status do
db = if database_up?(), do: "ok", else: "down"
tablebase = tablebase_status()
overall =
cond do
db == "down" -> "down"
tablebase == "local" -> "ok"
true -> "degraded"
end
%{status: overall, db: db, tablebase: tablebase, process: "ok"}
end
defp database_up? do
case Application.get_env(:chesstrainer, :health_database_check) do
fun when is_function(fun, 0) -> fun.()
_ -> default_database_up?()
end
end
defp default_database_up? do
case Repo.query("SELECT 1") do
{:ok, _result} -> true
_ -> false
end
rescue
_ -> false
catch
_, _ -> false
end
defp tablebase_status do
case Local.check() do
{:ok, largest} when largest > 0 -> "local"
{:ok, _largest} -> "fallback"
{:error, :not_configured} -> "not_configured"
{:error, _reason} -> "fallback"
end
end
end
@@ -1,13 +1,17 @@
defmodule ChesstrainerWeb.HealthController do
@moduledoc """
Lightweight health check used by deploys and uptime monitors.
JSON health check used by deploys and uptime monitors.
"""
use ChesstrainerWeb, :controller
def index(conn, _params) do
health = Chesstrainer.Health.status()
status = if health.status == "down", do: :service_unavailable, else: :ok
conn
|> put_resp_content_type("text/plain")
|> send_resp(200, "ok")
|> put_resp_header("x-robots-tag", "noindex")
|> put_status(status)
|> json(health)
end
end