`Prymn.Agent.Connection` GenServer uses healthchecking with the new streaming RPC, retrieving asynchronously the health status of the agent. The Connection will also shut itself down when there's no usage (when the keepalive function is not called frequently)
55 lines
1.3 KiB
Elixir
55 lines
1.3 KiB
Elixir
defmodule PrymnWeb.ServerLive.Index do
|
|
require Logger
|
|
alias Prymn.{Servers, Agents}
|
|
|
|
use PrymnWeb, :live_view
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
servers = Servers.list_servers()
|
|
|
|
if connected?(socket) do
|
|
for %Servers.Server{status: :registered, public_ip: ip} <- servers do
|
|
Agents.start_connection_or_keep_alive(ip)
|
|
end
|
|
end
|
|
|
|
{:ok, assign(socket, :servers, servers)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_params(_params, _url, socket) do
|
|
socket =
|
|
case socket.assigns.live_action do
|
|
:new -> assign(socket, :page_title, gettext("Connect a Server"))
|
|
:index -> assign(socket, :page_title, gettext("Listing Servers"))
|
|
end
|
|
|
|
{:noreply, socket}
|
|
end
|
|
|
|
@impl true
|
|
def handle_info({:connect, %Servers.Server{} = server}, socket) do
|
|
{:noreply,
|
|
socket
|
|
|> update(:servers, fn servers -> [server | servers] end)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_info({:healthcheck, ip, _message}, socket) do
|
|
servers =
|
|
update_in(
|
|
socket.assigns.servers,
|
|
[Access.filter(&match?(%{public_ip: ^ip}, &1))],
|
|
&Map.merge(&1, %{connection_status: "Connected"})
|
|
)
|
|
|
|
{:noreply, assign(socket, :servers, servers)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_info(msg, state) do
|
|
Logger.debug("received unexpected message #{inspect(msg)}")
|
|
{:noreply, state}
|
|
end
|
|
end
|