Current section

Files

Jump to
sworm lib sworm manager.ex
Raw

lib/sworm/manager.ex

defmodule Sworm.Manager do
@moduledoc false
use GenServer
import Sworm.Util
def child_spec({sworm, opts}) do
%{
id: manager_name(sworm),
start: {__MODULE__, :start_link, [{sworm, opts}]},
restart: :transient
}
end
def start_link({sworm, opts}) do
GenServer.start_link(__MODULE__, {sworm, opts}, name: manager_name(sworm))
end
###
require Logger
defmodule State do
@moduledoc false
defstruct sworm: nil, nodes: [], opts: []
end
def init({sworm, opts}) do
:timer.send_interval(1000, :check)
{:ok, %State{sworm: sworm, opts: opts}}
end
def handle_info(:check, state) do
{:noreply, update_nodes(state)}
end
def update_nodes(state) do
match = [{{{state.sworm, :"$1"}, :"$2", :"$3"}, [], [:"$1"]}]
nodes =
Horde.Registry.select(Sworm.Directory, match)
|> Enum.filter(fn n -> :pong == Node.ping(n) end)
|> Enum.sort()
case nodes == state.nodes do
true ->
state
false ->
Logger.debug("[#{state.sworm}] Node list updated to #{inspect(nodes)}")
for mod <- [supervisor_name(state.sworm), registry_name(state.sworm)] do
Horde.Cluster.set_members(mod, Enum.map(nodes, fn node -> {mod, node} end))
end
%State{state | nodes: nodes}
end
end
end