60 lines
2.0 KiB
Erlang
60 lines
2.0 KiB
Erlang
%%%-------------------------------------------------------------------
|
|
%% @doc endpoint top level supervisor.
|
|
%% @end
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(endpoint_sup).
|
|
|
|
-behaviour(supervisor).
|
|
-include("endpoint.hrl").
|
|
|
|
-export([start_link/0]).
|
|
-export([ensured_endpoint_started/1, delete_endpoint/1]).
|
|
|
|
-export([init/1]).
|
|
|
|
-define(SERVER, ?MODULE).
|
|
|
|
start_link() ->
|
|
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
|
|
|
|
%% sup_flags() = #{strategy => strategy(), % optional
|
|
%% intensity => non_neg_integer(), % optional
|
|
%% period => pos_integer()} % optional
|
|
%% child_spec() = #{id => child_id(), % mandatory
|
|
%% start => mfargs(), % mandatory
|
|
%% restart => restart(), % optional
|
|
%% shutdown => shutdown(), % optional
|
|
%% type => worker(), % optional
|
|
%% modules => modules()} % optional
|
|
init([]) ->
|
|
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
|
|
ChildSpecs = [],
|
|
{ok, {SupFlags, ChildSpecs}}.
|
|
|
|
%% internal functions
|
|
|
|
-spec ensured_endpoint_started(Endpoint :: #endpoint{}) -> {ok, Pid :: pid()} | {error, Reason :: any()}.
|
|
ensured_endpoint_started(Endpoint = #endpoint{}) ->
|
|
case supervisor:start_child(?MODULE, child_spec(Endpoint)) of
|
|
{ok, Pid} when is_pid(Pid) ->
|
|
{ok, Pid};
|
|
{error, {'already_started', Pid}} when is_pid(Pid) ->
|
|
{ok, Pid};
|
|
{error, Error} ->
|
|
{error, Error}
|
|
end.
|
|
|
|
delete_endpoint(Id) when is_integer(Id) ->
|
|
Name = endpoint:get_name(Id),
|
|
supervisor:terminate_child(?MODULE, Name),
|
|
supervisor:delete_child(?MODULE, Name).
|
|
|
|
child_spec(Endpoint = #endpoint{id = Id}) ->
|
|
Name = endpoint:get_name(Id),
|
|
#{id => Name,
|
|
start => {endpoint, start_link, [Name, Endpoint]},
|
|
restart => permanent,
|
|
shutdown => 2000,
|
|
type => worker,
|
|
modules => ['endpoint']}. |