From a3c428f408bb3fce1376da13bef3ad765a4df5f3 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 7 May 2024 21:55:31 +0800 Subject: [PATCH] fix endpoint_sup --- apps/endpoint/src/endpoint.erl | 2 +- apps/endpoint/src/endpoint_app.erl | 2 -- apps/endpoint/src/endpoint_sup.erl | 38 +++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/apps/endpoint/src/endpoint.erl b/apps/endpoint/src/endpoint.erl index 41d3e79..b6697c5 100644 --- a/apps/endpoint/src/endpoint.erl +++ b/apps/endpoint/src/endpoint.erl @@ -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)). diff --git a/apps/endpoint/src/endpoint_app.erl b/apps/endpoint/src/endpoint_app.erl index c972e48..4ec2d1e 100644 --- a/apps/endpoint/src/endpoint_app.erl +++ b/apps/endpoint/src/endpoint_app.erl @@ -10,8 +10,6 @@ -export([start/2, stop/1]). start(_StartType, _StartArgs) -> - lager:debug("[endpoint] started"), - endpoint_sup:start_link(). stop(_State) -> diff --git a/apps/endpoint/src/endpoint_sup.erl b/apps/endpoint/src/endpoint_sup.erl index 8d8861b..09df645 100644 --- a/apps/endpoint/src/endpoint_sup.erl +++ b/apps/endpoint/src/endpoint_sup.erl @@ -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']}. \ No newline at end of file