dotfiles/app/lib/prymn/agents.ex
Nikos Papadakis be7f584010
app: a nice poc background connection with the agent
Dynamically supervise a GRPC connection and keep it alive for a while,
using a timeout to kill the process on inactivity.

The Connection GenServer keeps the connection alive, while listening to
a health stream from the GRPC endpoint, notifying any subscribed
listeners about changes to health.

This health is then propagated to the main servers page.

Also create a looping get_sys_info which retrieves some basic system
information.
2023-08-26 00:51:04 +03:00

48 lines
1.4 KiB
Elixir

defmodule Prymn.Agents do
@moduledoc ~S"""
Prymn Agents are programs that manage a remote client machine. Prymn backend
communicates with them using GRPC calls. GRPC connections are started using
the Prymn.Agents.ConnectionSupervisor and are book-kept using the
Prymn.Agents.Registry.
"""
@doc """
Establishes a dynamically supervised, background connection with the target
host agent, keeping it alive if it is already started.
"""
@spec start_connection_or_keep_alive(String.t()) :: :ok
def start_connection_or_keep_alive(host_address) do
spec = {Prymn.Agents.Connection, host_address}
case DynamicSupervisor.start_child(Prymn.Agents.ConnectionSupervisor, spec) do
{:error, {:already_started, pid}} ->
:ok = Prymn.Agents.Connection.keep_alive(pid)
:ok
{:ok, _pid} ->
:ok
end
end
@doc """
Subscribe to the host's Health using Phoenix.PubSub Broadcasted messages are
in the form of:
{host_address, %Prymn.Agents.Health{}}
## Returns
This function returns the last health status of the Agent that was saved.
"""
@spec subscribe_to_health(String.t()) :: Prymn.Agents.Health.t()
def subscribe_to_health(host_address) do
Prymn.Agents.Health.subscribe(host_address)
Prymn.Agents.Health.lookup(host_address)
end
@spec get_channel(String.t()) :: GRPC.Channel.t()
def get_channel(host_address) do
Prymn.Agents.Connection.get_channel(host_address)
end
end