Added logging to identify which component is failing health checks: - Log database and Redis status on each health check - Log Redis errors with full error details - This will help diagnose the production 503 errors
81 lines
2.1 KiB
Elixir
81 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
|
|
case Towerops.RedisHealthCheck.check_health(redis_config, 2_000) 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
|