Packages

Keeping nodes connected

Current section

Files

Jump to
liaison lib application.ex
Raw

lib/application.ex

defmodule Liaison.Application do
@moduledoc false
@ type child :: pid | :undefined
@type on_start_child ::
{:ok, child}
| {:ok, child, info :: term}
| {:error, {:already_started, child} | :already_present | term}
defp sup_name(), do: Liaison.Supervisor
@spec start(any, any) :: {:error, any} | {:ok, pid}
def start(_type, _args) do
children = strategies()
opts = [strategy: :one_for_one, name: sup_name()]
Supervisor.start_link(children, opts)
end
defp strategies() do
Application.get_env(:liaison, :strategies, [])
|> Enum.reduce(%{}, fn strat_config, acc ->
Map.put(acc, strat_config[:strategy], strat_config)
end)
|> Map.to_list()
end
defp strategy_to_child(strat_config) do
{strat_config[:strategy], strat_config}
end
@spec add_child(list()) :: on_start_child
def add_child(strat_config) do
child =
strat_config
|> strategy_to_child()
Supervisor.start_child(sup_name(), child)
end
end