fix endpoint_sup
This commit is contained in:
parent
b9f16f3524
commit
a3c428f408
@ -29,7 +29,7 @@ start_link(Endpoint = #endpoint{id = Id, config = #mysql_endpoint{}}) ->
|
||||
Name = get_name(Id),
|
||||
endpoint_mysql:start_link(Name, Endpoint).
|
||||
|
||||
-spec get_name(Name :: binary() | #endpoint{}) -> atom().
|
||||
-spec get_name(Id :: integer()) -> atom().
|
||||
get_name(Id) when is_integer(Id) ->
|
||||
list_to_atom("endpoint:" ++ integer_to_list(Id)).
|
||||
|
||||
|
||||
@ -10,8 +10,6 @@
|
||||
-export([start/2, stop/1]).
|
||||
|
||||
start(_StartType, _StartArgs) ->
|
||||
lager:debug("[endpoint] started"),
|
||||
|
||||
endpoint_sup:start_link().
|
||||
|
||||
stop(_State) ->
|
||||
|
||||
@ -6,8 +6,10 @@
|
||||
-module(endpoint_sup).
|
||||
|
||||
-behaviour(supervisor).
|
||||
-include("endpoint.hrl").
|
||||
|
||||
-export([start_link/0]).
|
||||
-export([ensured_endpoint_started/1, delete_endpoint/1, stat/0]).
|
||||
|
||||
-export([init/1]).
|
||||
|
||||
@ -26,10 +28,40 @@ start_link() ->
|
||||
%% type => worker(), % optional
|
||||
%% modules => modules()} % optional
|
||||
init([]) ->
|
||||
SupFlags = #{strategy => one_for_all,
|
||||
intensity => 0,
|
||||
period => 1},
|
||||
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.
|
||||
|
||||
stat() ->
|
||||
Children = supervisor:which_children(?MODULE),
|
||||
lists:foreach(fun({Id, Pid, _, _}) ->
|
||||
Stat = catch endpoint:get_stat(Pid),
|
||||
lager:debug("[iot_endpoint] id: ~p, stat: ~p", [Id, Stat])
|
||||
end, Children).
|
||||
|
||||
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']}.
|
||||
Loading…
x
Reference in New Issue
Block a user