defmodule MicrowavepropWeb.HealthController do
  @moduledoc """
  Two separate probe endpoints for Kubernetes:

    * `live/2` — liveness only. Returns 200 as long as the BEAM can
      serve an HTTP request. Must NOT touch the database: a slow or
      saturated Ecto pool would otherwise cascade into SIGKILL across
      every replica simultaneously. Used as the `livenessProbe`
      target.

    * `check/2` — readiness. Also pings the Repo via `SELECT 1` so
      the pod is taken out of the Service endpoint set when the DB
      is unreachable. Used as the `readinessProbe` target.

  Splitting these two concerns is specifically a fix for an incident
  where PromEx's Oban queue-depth poller held a connection for > 15s,
  queued up every other checkout, and the liveness probe's `SELECT 1`
  timed out before it could ever acquire the pool — so the kubelet
  killed every pod every few minutes even though the BEAM was healthy.
  """
  use MicrowavepropWeb, :controller

  alias Ecto.Adapters.SQL

  def live(conn, _params) do
    conn
    |> put_resp_content_type("text/plain")
    |> send_resp(200, "ok")
  end

  def check(conn, _params) do
    _ = SQL.query!(Microwaveprop.Repo, "SELECT 1")

    conn
    |> put_resp_content_type("text/plain")
    |> send_resp(200, "ok")
  end
end
