Botica.Check.Behaviour behaviour (Botica v2.0.0)

Copy Markdown View Source

Behaviour for defining health checks.

Use this behaviour to create custom check modules with consistent structure and built-in support for Botica's health check infrastructure.

Example

defmodule MyApp.DatabaseCheck do
  use Botica.Check.Behaviour

  @impl true
  def check_def(_opts) do
    %{
      id: :database,
      name: "Database",
      description: "Database connectivity",
      priority: 1,
      tags: [:database, :critical],
      timeout: 5000,
      check: &__MODULE__.do_check/0,
      fix: &__MODULE__.do_fix/0,
      fix_command: "sudo systemctl restart myapp-db"
    }
  end

  defp do_check do
    # Your check logic here
    {:ok, "Database is reachable"}
  end

  defp do_fix do
    # Your fix logic here
    {:ok, "Database restarted"}
  end
end

Then in your config:

Botica.Doctor.run(%{
  app_name: "myapp",
  checks: [MyApp.DatabaseCheck.check_def([])]
})

Summary

Callbacks

Returns a check definition map for this check module.

Optional callback to cleanup state after checks complete.

Optional callback to prepare state before running checks.

Optional callback to validate configuration before running checks.

Functions

Macro to use this behaviour in a module.

Callbacks

check_def(opts)

@callback check_def(opts :: keyword()) :: Botica.Types.check_def()

Returns a check definition map for this check module.

Override this callback to define your check's metadata and behaviour.

Options

  • timeout - Override the default timeout for this check (in milliseconds)
  • Any other options your check implementation needs

cleanup(opts)

(optional)
@callback cleanup(opts :: keyword()) :: :ok

Optional callback to cleanup state after checks complete.

Use this to release any resources acquired in prepare/1. The default implementation does nothing.

prepare(opts)

(optional)
@callback prepare(opts :: keyword()) :: :ok | {:error, String.t()}

Optional callback to prepare state before running checks.

Use this to set up any resources needed by the check. The default implementation does nothing.

validate_config(opts)

(optional)
@callback validate_config(opts :: keyword()) :: :ok | {:error, String.t()}

Optional callback to validate configuration before running checks.

Return :ok if configuration is valid, or {:error, reason} if not. The default implementation always returns :ok.

Functions

__using__(opts)

(macro)

Macro to use this behaviour in a module.