Re-activate clients after a failure

This commit is contained in:
Mike Cifelli 2024-06-05 17:01:57 -04:00
parent 3c3d078c4d
commit 13dd4002d0
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
2 changed files with 26 additions and 5 deletions

View File

@ -44,7 +44,9 @@ defmodule Chronoscope.NTS.Client do
@impl true @impl true
def handle_call(:auto_refresh, _from, state) do def handle_call(:auto_refresh, _from, state) do
:timer.send_after(1_000, :key_establishment)
{:ok, timer} = :timer.send_interval(@refresh_interval_in_milliseconds, :key_establishment) {:ok, timer} = :timer.send_interval(@refresh_interval_in_milliseconds, :key_establishment)
{:reply, :ok, Map.put(state, :timer, timer)} {:reply, :ok, Map.put(state, :timer, timer)}
end end

View File

@ -6,6 +6,8 @@ defmodule ChronoscopeWeb.ClientActivator do
alias Chronoscope.NTS alias Chronoscope.NTS
alias Chronoscope.NTS.Parse alias Chronoscope.NTS.Parse
@activate_interval_in_milliseconds 300_000
@nts_file Application.compile_env(:chronoscope, :nts_file) @nts_file Application.compile_env(:chronoscope, :nts_file)
def start_link(_) do def start_link(_) do
@ -14,13 +16,29 @@ defmodule ChronoscopeWeb.ClientActivator do
@impl true @impl true
def init(_) do def init(_) do
File.touch(Application.app_dir(:chronoscope, @nts_file)) nts_servers =
{:ok, %{nts_servers: activate_nts_clients()}} nts_servers()
|> tap(fn servers -> Enum.each(servers, &NTS.auto_refresh/1) end)
:timer.send_interval(@activate_interval_in_milliseconds, :ensure_activated)
{:ok, %{nts_servers: nts_servers}}
end
@impl true
def handle_info(:ensure_activated, state) do
Enum.each(state.nts_servers, &NTS.auto_refresh/1)
{:noreply, state}
end end
@impl true @impl true
def handle_info(:activate_clients, _state) do def handle_info(:activate_clients, _state) do
{:noreply, %{nts_servers: activate_nts_clients()}} nts_servers =
nts_servers()
|> tap(fn servers -> Enum.each(servers, &NTS.auto_refresh/1) end)
{:noreply, %{nts_servers: nts_servers}}
end end
@impl true @impl true
@ -28,13 +46,14 @@ defmodule ChronoscopeWeb.ClientActivator do
{:reply, state.nts_servers, state} {:reply, state.nts_servers, state}
end end
defp activate_nts_clients() do defp nts_servers() do
File.touch(Application.app_dir(:chronoscope, @nts_file))
Application.app_dir(:chronoscope, @nts_file) Application.app_dir(:chronoscope, @nts_file)
|> File.stream!() |> File.stream!()
|> Stream.map(&String.trim/1) |> Stream.map(&String.trim/1)
|> Stream.filter(&(&1 != "")) |> Stream.filter(&(&1 != ""))
|> Stream.map(&Parse.parse_nts_server/1) |> Stream.map(&Parse.parse_nts_server/1)
|> Enum.to_list() |> Enum.to_list()
|> tap(fn server -> Enum.each(server, &NTS.auto_refresh/1) end)
end end
end end