dotfiles/app/lib/prymn/agents.ex

51 lines
1.5 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.Supervisor (a DynamicSupervisor) and are book-kept using the
Prymn.Agents.Registry.
## Examples
TODO
"""
@doc """
Ensures a connection with the Prymn Agent exists and is kept in memory.
Returns `:ok` when a new connection is successfuly established or is already established
Returns `{:error, reason}` when the connection could not be established
"""
@spec ensure_connection(String.t()) :: :ok | {:error, term}
def ensure_connection(public_ip) do
child = {Prymn.Agents.Connection, public_ip}
case DynamicSupervisor.start_child(Prymn.Agents.Supervisor, child) do
{:ok, _pid} -> :ok
{:error, {:already_started, _pid}} -> :ok
{:error, error} -> {:error, error}
end
end
@doc """
Terminates the process and drops the connection gracefully.
"""
@spec drop_connection(String.t()) :: :ok | {:error, :not_found}
def drop_connection(address) do
:ok = Prymn.Agents.Connection.drop(address)
catch
:exit, _ -> {:error, :not_found}
end
@doc """
Get the channel for the given `address`. The channel is used to make GRPC
calls.
"""
@spec get_channel(String.t()) :: GRPC.Channel.t() | {:error, :not_found}
def get_channel(address) do
Prymn.Agents.Connection.get_channel(address)
catch
:exit, _ -> {:error, :not_found}
end
end