fix router sup

This commit is contained in:
安礼成 2023-03-03 11:37:41 +08:00
parent a4aa4b5268
commit b276134de1
2 changed files with 71 additions and 43 deletions

View File

@ -8,29 +8,35 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(iot_router). -module(iot_router).
-author("licheng5"). -author("licheng5").
-include("iot.hrl").
-behaviour(gen_server). -behaviour(gen_server).
%% API %% API
-export([start_link/0]). -export([start_link/2]).
-export([get_name/1]).
%% gen_server callbacks %% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
code_change/3]).
-define(SERVER, ?MODULE). -define(SERVER, ?MODULE).
-record(iot_router_state, {}). -record(state, {
}).
get_name(Id) when is_integer(Id) ->
list_to_atom("iot_router:" ++ integer_to_list(Id)).
%%%=================================================================== %%%===================================================================
%%% API %%% API
%%%=================================================================== %%%===================================================================
%% @doc Spawns the server and registers the local name (unique) %% @doc Spawns the server and registers the local name (unique)
-spec(start_link() -> -spec(start_link(Name :: atom(), Router :: #router{}) ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}). {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() -> start_link(Name, Router = #router{}) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). gen_server:start_link({global, Name}, ?MODULE, [Router], []).
%%%=================================================================== %%%===================================================================
%%% gen_server callbacks %%% gen_server callbacks
@ -39,40 +45,41 @@ start_link() ->
%% @private %% @private
%% @doc Initializes the server %% @doc Initializes the server
-spec(init(Args :: term()) -> -spec(init(Args :: term()) ->
{ok, State :: #iot_router_state{}} | {ok, State :: #iot_router_state{}, timeout() | hibernate} | {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore). {stop, Reason :: term()} | ignore).
init([]) -> init([Router]) ->
{ok, #iot_router_state{}}. lager:debug("router is: ~p", [Router]),
{ok, #state{}}.
%% @private %% @private
%% @doc Handling call messages %% @doc Handling call messages
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()}, -spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
State :: #iot_router_state{}) -> State :: #state{}) ->
{reply, Reply :: term(), NewState :: #iot_router_state{}} | {reply, Reply :: term(), NewState :: #state{}} |
{reply, Reply :: term(), NewState :: #iot_router_state{}, timeout() | hibernate} | {reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
{noreply, NewState :: #iot_router_state{}} | {noreply, NewState :: #state{}} |
{noreply, NewState :: #iot_router_state{}, timeout() | hibernate} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #iot_router_state{}} | {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
{stop, Reason :: term(), NewState :: #iot_router_state{}}). {stop, Reason :: term(), NewState :: #state{}}).
handle_call(_Request, _From, State = #iot_router_state{}) -> handle_call(_Request, _From, State = #state{}) ->
{reply, ok, State}. {reply, ok, State}.
%% @private %% @private
%% @doc Handling cast messages %% @doc Handling cast messages
-spec(handle_cast(Request :: term(), State :: #iot_router_state{}) -> -spec(handle_cast(Request :: term(), State :: #state{}) ->
{noreply, NewState :: #iot_router_state{}} | {noreply, NewState :: #state{}} |
{noreply, NewState :: #iot_router_state{}, timeout() | hibernate} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #iot_router_state{}}). {stop, Reason :: term(), NewState :: #state{}}).
handle_cast(_Request, State = #iot_router_state{}) -> handle_cast(_Request, State = #state{}) ->
{noreply, State}. {noreply, State}.
%% @private %% @private
%% @doc Handling all non call/cast messages %% @doc Handling all non call/cast messages
-spec(handle_info(Info :: timeout() | term(), State :: #iot_router_state{}) -> -spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
{noreply, NewState :: #iot_router_state{}} | {noreply, NewState :: #state{}} |
{noreply, NewState :: #iot_router_state{}, timeout() | hibernate} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #iot_router_state{}}). {stop, Reason :: term(), NewState :: #state{}}).
handle_info(_Info, State = #iot_router_state{}) -> handle_info(_Info, State = #state{}) ->
{noreply, State}. {noreply, State}.
%% @private %% @private
@ -81,16 +88,16 @@ handle_info(_Info, State = #iot_router_state{}) ->
%% necessary cleaning up. When it returns, the gen_server terminates %% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored. %% with Reason. The return value is ignored.
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), -spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
State :: #iot_router_state{}) -> term()). State :: #state{}) -> term()).
terminate(_Reason, _State = #iot_router_state{}) -> terminate(_Reason, _State = #state{}) ->
ok. ok.
%% @private %% @private
%% @doc Convert process state when code is changed %% @doc Convert process state when code is changed
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #iot_router_state{}, -spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
Extra :: term()) -> Extra :: term()) ->
{ok, NewState :: #iot_router_state{}} | {error, Reason :: term()}). {ok, NewState :: #state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State = #iot_router_state{}, _Extra) -> code_change(_OldVsn, State = #state{}, _Extra) ->
{ok, State}. {ok, State}.
%%%=================================================================== %%%===================================================================

View File

@ -8,11 +8,12 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(iot_router_sup). -module(iot_router_sup).
-author("licheng5"). -author("licheng5").
-include("iot.hrl").
-behaviour(supervisor). -behaviour(supervisor).
%% API %% API
-export([start_link/0]). -export([start_link/0, start_new_router/1]).
%% Supervisor callbacks %% Supervisor callbacks
-export([init/1]). -export([init/1]).
@ -45,17 +46,37 @@ start_link() ->
init([]) -> init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
AChild = #{ %%
id => 'iot_router', {ok, Routers} = router_model:get_all_valid_routers(),
start => {'iot_router', start_link, []}, Specs = lists:map(fun generate_router_spec/1, Routers),
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['iot_router']
},
{ok, {SupFlags, [AChild]}}. {ok, {SupFlags, Specs}}.
%%%=================================================================== %%%===================================================================
%%% Internal functions %%% Internal functions
%%%=================================================================== %%%===================================================================
%%
-spec start_new_router(Router :: #router{}) -> {ok, Pid :: pid()} | {error, Reason :: any()}.
start_new_router(Router = #router{}) ->
Spec = generate_router_spec(Router),
case supervisor:start_child(?MODULE, Spec) of
{ok, Pid} ->
{ok, Pid};
{error, {already_started, Pid}} ->
{ok, Pid};
{error, Error} ->
lager:debug("start router get a error: ~p", [Error]),
{error, Error}
end.
generate_router_spec(Router = #router{router_id = RouterId}) ->
Id = iot_router:get_name(RouterId),
#{
id => Id,
start => {'iot_router', start_link, [Id, Router]},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['iot_router']
}.