Current section
Files
Jump to
Current section
Files
lib/sailfish.ex
defmodule Sailfish.Client do
use GenServer
require Logger
def start_link(opts) do
GenServer.start_link(__MODULE__, opts)
end
def init(opts) do
Process.flag(:trap_exit, true)
send(self(), :connect)
{:ok, Map.merge(%{}, opts)}
end
def handle_info({:EXIT, _pid, _reason}, state) do
send(self(), :connect)
{:noreply, state}
end
def handle_info(:connect, state) do
Logger.info("Sailfish attempting connection")
state =
Map.merge(
%{
url: "wss://sail.fish/socket"
},
state
)
with {:ok, pid} <-
Riptide.Client.start_link(state.url, [Sailfish.Client.Commands.Console], name: Sailfish),
auth =
Map.merge(
%{
token: "unknown",
team: "unknown",
node: :inet.gethostname() |> elem(1) |> String.Chars.to_string(),
tags: %{}
},
state
),
{:ok, pid} <- Riptide.Client.call(Sailfish, "auth.node", auth) do
Logger.info("Sailfish authenticated")
else
_ -> Process.send_after(self(), :connect, :timer.seconds(5))
end
{:noreply, state}
end
end