Skip to content

Instantly share code, notes, and snippets.

View lpeppe's full-sized avatar
🎯
Focusing

Luca Peppe lpeppe

🎯
Focusing
View GitHub Profile
defmodule PhxHealthcheckWeb.HealthcheckController do
use PhxHealthcheckWeb, :controller
alias PhxHealthcheck.Healthcheck
def status(conn, _params) do
monitorable_services =
:phx_healthcheck
|> Application.get_env(:healthcheck, services: [])
|> Keyword.fetch!(:services)
defmodule PhxHealthcheckWeb.Router do
use PhxHealthcheckWeb, :router
get "/status", PhxHealthcheckWeb.HealthcheckController, :status
end
defmodule PhxHealthcheck.Healthcheck do
@callback check_status() :: String.t()
@spec get_service_status(atom()) :: String.t()
def get_service_status(service) do
GenServer.call(service, {:get_status})
end
end
config :phx_healthcheck, :healthcheck,
services: [
{PhxHealthcheck.Healthcheck.Services.Database, [name: "database", refresh_interval: 10000]}
]
defmodule PhxHealthcheck.Healthcheck.Supervisor do
alias PhxHealthcheck.Healthcheck.Worker
use Supervisor
def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
@impl true
defmodule PhxHealthcheck.Healthcheck.Worker do
use GenServer
@spec start_link(list()) :: GenServer.on_start()
def start_link([service | _] = args) do
GenServer.start_link(__MODULE__, args, name: service)
end
@impl true
def init([service, refresh_interval]) do
defmodule PhxHealthcheck.Healthcheck.Services.Database do
alias PhxHealthcheck.{Healthcheck, Repo}
import Ecto.Adapters.SQL
@behaviour Healthcheck
def check_status() do
try do
query(Repo, "SELECT 1")
rescue
defmodule PhxHealthcheck.Healthcheck do
@callback check_status() :: String.t()
end
@lpeppe
lpeppe / phx_healthcheck.json
Created April 12, 2021 14:36
phx_healthcheck_json
{
"database": "healthy",
"document_processor": "healthy",
"payment_system": "unhealthy"
}

Keybase proof

I hereby claim:

  • I am lpeppe on github.
  • I am luca_peppe (https://keybase.io/luca_peppe) on keybase.
  • I have a public key ASC-ca8kfZMvNK-cxab93IDHZRoFfiHkL-TZzZ-Ei8kmGAo

To claim this, I am signing this object: