60 lines
1.3 KiB
Elixir
60 lines
1.3 KiB
Elixir
defmodule ChronoscopeWeb.ClientActivator do
|
|
use GenServer
|
|
|
|
require Logger
|
|
|
|
alias Chronoscope.NTS
|
|
alias Chronoscope.NTS.Parse
|
|
|
|
@activate_interval_in_milliseconds 300_000
|
|
|
|
@nts_file Application.compile_env(:chronoscope, :nts_file)
|
|
|
|
def start_link(_) do
|
|
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
|
|
end
|
|
|
|
@impl true
|
|
def init(_) do
|
|
nts_servers =
|
|
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
|
|
|
|
@impl true
|
|
def handle_info(:activate_clients, _state) do
|
|
nts_servers =
|
|
nts_servers()
|
|
|> tap(fn servers -> Enum.each(servers, &NTS.auto_refresh/1) end)
|
|
|
|
{:noreply, %{nts_servers: nts_servers}}
|
|
end
|
|
|
|
@impl true
|
|
def handle_call(:get_nts_servers, _from, state) do
|
|
{:reply, state.nts_servers, state}
|
|
end
|
|
|
|
defp nts_servers() do
|
|
File.touch(Application.app_dir(:chronoscope, @nts_file))
|
|
|
|
Application.app_dir(:chronoscope, @nts_file)
|
|
|> File.stream!()
|
|
|> Stream.map(&String.trim/1)
|
|
|> Stream.filter(&(&1 != ""))
|
|
|> Stream.map(&Parse.parse_nts_server/1)
|
|
|> Enum.to_list()
|
|
end
|
|
end
|