supervisor_spec

This commit is contained in:
Ery Lee 2015-01-17 23:08:59 +08:00
parent 2f5d7634d2
commit 0ae4a0f1af
1 changed files with 30 additions and 25 deletions

View File

@ -60,56 +60,61 @@ print_vsn() ->
start_servers(Sup) -> start_servers(Sup) ->
{ok, SessOpts} = application:get_env(session), {ok, SessOpts} = application:get_env(session),
{ok, RetainOpts} = application:get_env(retain),
lists:foreach( lists:foreach(
fun({Name, F}) when is_function(F) -> fun({Name, F}) when is_function(F) ->
?PRINT("~s is starting...", [Name]), ?PRINT("~s is starting...", [Name]),
F(), F(),
?PRINT_MSG("[done]~n"); ?PRINT_MSG("[done]~n");
({Name, Server}) when is_atom(Server) -> ({Name, Server}) ->
?PRINT("~s is starting...", [Name]), ?PRINT("~s is starting...", [Name]),
start_child(Sup, Server), start_child(Sup, Server),
?PRINT_MSG("[done]~n"); ?PRINT_MSG("[done]~n");
({Name, Server, Opts}) when is_atom(Server) -> ({Name, Server, Opts}) ->
?PRINT("~s is starting...", [ Name]), ?PRINT("~s is starting...", [ Name]),
start_child(Sup, Server, Opts), start_child(Sup, Server, Opts),
?PRINT_MSG("[done]~n") ?PRINT_MSG("[done]~n")
end, end,
[{"emqtt config", emqtt_config}, [{"emqtt config", emqtt_config},
{"emqtt server", emqtt_server, RetainOpts},
{"emqtt client manager", emqtt_cm}, {"emqtt client manager", emqtt_cm},
{"emqtt session manager", emqtt_sm}, {"emqtt session manager", emqtt_sm},
%%TODO: fixme {"emqtt session supervisor", {supervisor, emqtt_session_sup}, SessOpts},
{"emqtt session supervisor", fun() ->
Mod = emqtt_session_sup,
supervisor:start_child(Sup,
{Mod,
{Mod, start_link, [SessOpts]},
permanent, 1000, supervisor, [Mod]})
end},
{"emqtt auth", emqtt_auth}, {"emqtt auth", emqtt_auth},
{"emqtt retained", emqtt_retained},
{"emqtt pubsub", emqtt_pubsub}, {"emqtt pubsub", emqtt_pubsub},
{"emqtt router", emqtt_router}, {"emqtt router", emqtt_router},
{"emqtt queue supervisor", fun() ->
Mod = emqtt_queue_sup,
supervisor:start_child(Sup,
{Mod,
{Mod, start_link, []},
permanent, 1000, supervisor, [Mod]})
end},
{"emqtt monitor", emqtt_monitor} {"emqtt monitor", emqtt_monitor}
]). ]).
start_child(Sup, Name) -> start_child(Sup, {supervisor, Name}) ->
supervisor:start_child(Sup, supervisor_spec(Name));
start_child(Sup, Name) when is_atom(Name) ->
{ok, _ChiId} = supervisor:start_child(Sup, worker_spec(Name)). {ok, _ChiId} = supervisor:start_child(Sup, worker_spec(Name)).
start_child(Sup, Name, Opts) ->
start_child(Sup, {supervisor, Name}, Opts) ->
supervisor:start_child(Sup, supervisor_spec(Name, Opts));
start_child(Sup, Name, Opts) when is_atom(Name) ->
{ok, _ChiId} = supervisor:start_child(Sup, worker_spec(Name, Opts)). {ok, _ChiId} = supervisor:start_child(Sup, worker_spec(Name, Opts)).
%%TODO: refactor...
supervisor_spec(Name) ->
{Name,
{Name, start_link, []},
permanent, infinity, supervisor, [Name]}.
supervisor_spec(Name, Opts) ->
{Name,
{Name, start_link, [Opts]},
permanent, infinity, supervisor, [Name]}.
worker_spec(Name) -> worker_spec(Name) ->
{Name, {Name, start_link, []}, {Name,
permanent, 5000, worker, [Name]}. {Name, start_link, []},
permanent, 5000, worker, [Name]}.
worker_spec(Name, Opts) -> worker_spec(Name, Opts) ->
{Name, {Name, start_link, [Opts]}, {Name,
permanent, 5000, worker, [Name]}. {Name, start_link, [Opts]},
permanent, 5000, worker, [Name]}.
%% %%
%% @spec stop(atom) -> 'ok' %% @spec stop(atom) -> 'ok'