towerops/lib/towerops_web/controllers/health_controller.ex
Graham McIntire b1c803e1d2 test: cut runtime on slow tests, fix time-sensitive flake
Performance:
- HealthController: honor `:timeout` from `:redis` config (was hardcoded
  2s) so the 503-when-unreachable test exits immediately
- DevicePollerBranches: drop `collect_events` window 1500ms → 100ms
  (PubSub broadcasts are synchronous from `run_perform`)
- Towerops.Unused: cache xref analysis in `:persistent_term` keyed by
  BEAM mtimes so repeated `mix unused` invocations skip re-analysis
- DiscoveryWorker: make wait_loop interval configurable via
  `:discovery_wait_interval_ms` (25ms in tests vs 500ms in prod)
- ProfileWatcher: configurable `:profile_reloader` so tests can stub
  the YAML reload instead of paying the full disk-load cost
- PingExecutor: tag the loopback test `:network` (it shells out to
  `ping`); coverage already provided by 4 other `:network`-tagged tests

Flake fix:
- FrequencyChange tests used hardcoded `~U[2026-05-09 12:00:00Z]`,
  which now falls outside the rule's 24h lookback window. Switched to
  `DateTime.utc_now()` so scans stay inside the window regardless of
  clock progression.
2026-05-10 10:39:36 -05:00

83 lines
2.1 KiB
Elixir

defmodule ToweropsWeb.HealthController do
@moduledoc false
use ToweropsWeb, :controller
alias Ecto.Adapters.SQL
alias Towerops.Repo
def index(conn, _params) do
require Logger
db_status = check_database()
redis_status = check_redis()
# Log health check results for debugging
Logger.debug("Health check: db=#{inspect(db_status)}, redis=#{inspect(redis_status)}")
all_healthy = db_status == :ok and redis_status in [:ok, :not_configured]
if all_healthy do
conn
|> put_resp_content_type("application/json")
|> send_resp(
200,
Jason.encode!(%{
status: "ok",
database: "connected",
redis: redis_status_string(redis_status),
version: :towerops |> Application.spec(:vsn) |> to_string()
})
)
else
conn
|> put_resp_content_type("application/json")
|> send_resp(
503,
Jason.encode!(%{
status: "error",
database: db_status_string(db_status),
redis: redis_status_string(redis_status)
})
)
end
end
defp check_database do
case SQL.query(Repo, "SELECT 1", []) do
{:ok, _} -> :ok
{:error, _} -> :error
end
end
defp check_redis do
require Logger
redis_config = Application.get_env(:towerops, :redis, [])
if Keyword.has_key?(redis_config, :host) do
# Redis is configured, check connectivity with short timeout for health check
timeout = Keyword.get(redis_config, :timeout, 2_000)
case Towerops.RedisHealthCheck.check_health(redis_config, timeout) do
:ok ->
:ok
{:error, reason} ->
Logger.error("Redis health check failed: #{inspect(reason)}")
:error
end
else
# Redis not configured (e.g., in development without Redis)
Logger.debug("Redis not configured")
:not_configured
end
end
defp db_status_string(:ok), do: "connected"
defp db_status_string(:error), do: "disconnected"
defp redis_status_string(:ok), do: "connected"
defp redis_status_string(:error), do: "disconnected"
defp redis_status_string(:not_configured), do: "not_configured"
end