From 0ae4a0f1af6a916f7836d57c18cd6befdd038ca3 Mon Sep 17 00:00:00 2001 From: Ery Lee Date: Sat, 17 Jan 2015 23:08:59 +0800 Subject: [PATCH] supervisor_spec --- apps/emqtt/src/emqtt_app.erl | 55 ++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/apps/emqtt/src/emqtt_app.erl b/apps/emqtt/src/emqtt_app.erl index ce65a92ec..82f4bd366 100644 --- a/apps/emqtt/src/emqtt_app.erl +++ b/apps/emqtt/src/emqtt_app.erl @@ -60,56 +60,61 @@ print_vsn() -> start_servers(Sup) -> {ok, SessOpts} = application:get_env(session), + {ok, RetainOpts} = application:get_env(retain), lists:foreach( fun({Name, F}) when is_function(F) -> ?PRINT("~s is starting...", [Name]), F(), ?PRINT_MSG("[done]~n"); - ({Name, Server}) when is_atom(Server) -> + ({Name, Server}) -> ?PRINT("~s is starting...", [Name]), start_child(Sup, Server), ?PRINT_MSG("[done]~n"); - ({Name, Server, Opts}) when is_atom(Server) -> + ({Name, Server, Opts}) -> ?PRINT("~s is starting...", [ Name]), start_child(Sup, Server, Opts), ?PRINT_MSG("[done]~n") end, [{"emqtt config", emqtt_config}, + {"emqtt server", emqtt_server, RetainOpts}, {"emqtt client manager", emqtt_cm}, {"emqtt session manager", emqtt_sm}, - %%TODO: fixme - {"emqtt session supervisor", fun() -> - Mod = emqtt_session_sup, - supervisor:start_child(Sup, - {Mod, - {Mod, start_link, [SessOpts]}, - permanent, 1000, supervisor, [Mod]}) - end}, + {"emqtt session supervisor", {supervisor, emqtt_session_sup}, SessOpts}, {"emqtt auth", emqtt_auth}, - {"emqtt retained", emqtt_retained}, {"emqtt pubsub", emqtt_pubsub}, {"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} ]). -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)). -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)). +%%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) -> - {Name, {Name, start_link, []}, - permanent, 5000, worker, [Name]}. -worker_spec(Name, Opts) -> - {Name, {Name, start_link, [Opts]}, - permanent, 5000, worker, [Name]}. + {Name, + {Name, start_link, []}, + permanent, 5000, worker, [Name]}. +worker_spec(Name, Opts) -> + {Name, + {Name, start_link, [Opts]}, + permanent, 5000, worker, [Name]}. %% %% @spec stop(atom) -> 'ok'