fix endpoint_sup

This commit is contained in:
anlicheng 2024-05-07 21:55:31 +08:00
parent b9f16f3524
commit a3c428f408
3 changed files with 36 additions and 6 deletions

View File

@ -29,7 +29,7 @@ start_link(Endpoint = #endpoint{id = Id, config = #mysql_endpoint{}}) ->
Name = get_name(Id), Name = get_name(Id),
endpoint_mysql:start_link(Name, Endpoint). 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) -> get_name(Id) when is_integer(Id) ->
list_to_atom("endpoint:" ++ integer_to_list(Id)). list_to_atom("endpoint:" ++ integer_to_list(Id)).

View File

@ -10,8 +10,6 @@
-export([start/2, stop/1]). -export([start/2, stop/1]).
start(_StartType, _StartArgs) -> start(_StartType, _StartArgs) ->
lager:debug("[endpoint] started"),
endpoint_sup:start_link(). endpoint_sup:start_link().
stop(_State) -> stop(_State) ->

View File

@ -6,8 +6,10 @@
-module(endpoint_sup). -module(endpoint_sup).
-behaviour(supervisor). -behaviour(supervisor).
-include("endpoint.hrl").
-export([start_link/0]). -export([start_link/0]).
-export([ensured_endpoint_started/1, delete_endpoint/1, stat/0]).
-export([init/1]). -export([init/1]).
@ -26,10 +28,40 @@ start_link() ->
%% type => worker(), % optional %% type => worker(), % optional
%% modules => modules()} % optional %% modules => modules()} % optional
init([]) -> init([]) ->
SupFlags = #{strategy => one_for_all, SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
intensity => 0,
period => 1},
ChildSpecs = [], ChildSpecs = [],
{ok, {SupFlags, ChildSpecs}}. {ok, {SupFlags, ChildSpecs}}.
%% internal functions %% 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']}.