Packages

Elixir-native host management with inspectable plans, package locks, systemd isolation, and remote bootstrap.

Current section

Files

Jump to
host_kit lib host_kit agent monitor_worker.ex
Raw

lib/host_kit/agent/monitor_worker.ex

defmodule HostKit.Agent.MonitorWorker do
@moduledoc "Supervised worker for scheduled monitoring checks."
use GenServer
alias HostKit.Agent.{Schedule, State}
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, opts, name: Keyword.get(opts, :name, __MODULE__))
end
@spec run_once(keyword()) :: {:ok, [HostKit.Monitor.Result.t()]} | {:error, term()}
def run_once(opts \\ []) do
opts
|> run_monitor()
|> tap(&State.record_monitor/1)
end
@impl true
def init(opts) do
{:ok, opts |> Schedule.init() |> Schedule.schedule()}
end
@impl true
def handle_info(:run, state) do
run_once(state.opts)
{:noreply, Schedule.reschedule(state)}
end
defp run_monitor(opts) do
snapshot = State.snapshot()
case snapshot.project do
nil ->
{:error, :agent_not_configured}
project ->
monitor_opts =
opts |> Keyword.get(:monitor_opts, []) |> Keyword.put_new(:target, snapshot.target)
HostKit.Monitor.run(project, monitor_opts)
end
end
end