Merge branch 'boot' into develop

This commit is contained in:
Feng Lee 2019-09-10 16:26:20 +08:00
commit 594153c0fb
19 changed files with 101 additions and 11 deletions

View File

@ -35,10 +35,10 @@ start(_Type, _Args) ->
ok = emqx_modules:load(),
ok = emqx_plugins:init(),
emqx_plugins:load(),
ok = emqx_listeners:start(),
emqx_boot:is_enabled(listeners)
andalso (ok = emqx_listeners:start()),
start_autocluster(),
register(emqx, self()),
emqx_alarm_handler:load(),
print_vsn(),
{ok, Sup}.
@ -46,7 +46,8 @@ start(_Type, _Args) ->
-spec(stop(State :: term()) -> term()).
stop(_State) ->
emqx_alarm_handler:unload(),
emqx_listeners:stop(),
emqx_boot:is_enabled(listeners)
andalso emqx_listeners:stop(),
emqx_modules:unload().
%%--------------------------------------------------------------------

29
src/emqx_boot.erl Normal file
View File

@ -0,0 +1,29 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2019 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_boot).
-export([is_enabled/1]).
-define(BOOT_MODULES, [router, broker, listeners]).
-spec(is_enabled(all|list(router|broker|listeners)) -> boolean()).
is_enabled(Mod) ->
(BootMods = boot_modules()) =:= all orelse lists:member(Mod, BootMods).
boot_modules() ->
application:get_env(emqx, boot_modules, ?BOOT_MODULES).

View File

@ -62,18 +62,21 @@ stop_child(ChildId) ->
%%--------------------------------------------------------------------
init([]) ->
%% Kernel Sup
KernelSup = child_spec(emqx_kernel_sup, supervisor),
%% Router Sup
RouterSup = child_spec(emqx_router_sup, supervisor),
%% Broker Sup
BrokerSup = child_spec(emqx_broker_sup, supervisor),
%% CM Sup
CMSup = child_spec(emqx_cm_sup, supervisor),
%% Sys Sup
SysSup = child_spec(emqx_sys_sup, supervisor),
{ok, {{one_for_all, 0, 1},
[KernelSup, RouterSup, BrokerSup, CMSup, SysSup]}}.
Childs = [KernelSup] ++
[RouterSup || emqx_boot:is_enabled(router)] ++
[BrokerSup || emqx_boot:is_enabled(broker)] ++
[CMSup || emqx_boot:is_enabled(broker)] ++
[SysSup],
SupFlags = #{strategy => one_for_all,
intensity => 0,
period => 1
},
{ok, {SupFlags, Childs}}.
%%--------------------------------------------------------------------
%% Internal functions

View File

@ -63,6 +63,7 @@ groups() ->
}].
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules([router, broker]),
emqx_ct_helpers:start_apps([]),
Config.

View File

@ -26,6 +26,7 @@
all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([], fun set_special_configs/1),
Config.

43
test/emqx_boot_SUITE.erl Normal file
View File

@ -0,0 +1,43 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2019 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_boot_SUITE).
-compile(export_all).
-compile(nowarn_export_all).
-include_lib("eunit/include/eunit.hrl").
all() -> emqx_ct:all(?MODULE).
t_is_enabled(_) ->
ok = application:set_env(emqx, boot_modules, all),
?assert(emqx_boot:is_enabled(router)),
?assert(emqx_boot:is_enabled(broker)),
?assert(emqx_boot:is_enabled(listeners)),
ok = application:set_env(emqx, boot_modules, [router]),
?assert(emqx_boot:is_enabled(router)),
?assertNot(emqx_boot:is_enabled(broker)),
?assertNot(emqx_boot:is_enabled(listeners)),
ok = application:set_env(emqx, boot_modules, [router, broker]),
?assert(emqx_boot:is_enabled(router)),
?assert(emqx_boot:is_enabled(broker)),
?assertNot(emqx_boot:is_enabled(listeners)),
ok = application:set_env(emqx, boot_modules, [router, broker, listeners]),
?assert(emqx_boot:is_enabled(router)),
?assert(emqx_boot:is_enabled(broker)),
?assert(emqx_boot:is_enabled(listeners)).

View File

@ -49,6 +49,7 @@ groups() ->
}].
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules([router, broker]),
emqx_ct_helpers:start_apps([]),
Config.

View File

@ -31,6 +31,7 @@
all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules([router, broker]),
emqx_ct_helpers:start_apps([]),
Config.

View File

@ -70,6 +70,7 @@ groups() ->
].
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]),
Config.

View File

@ -24,6 +24,7 @@
all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]),
Config.

View File

@ -25,6 +25,7 @@
all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules([]),
emqx_ct_helpers:start_apps([]),
Config.
@ -51,4 +52,3 @@ test(_) ->
io:format("Hello world").

View File

@ -28,6 +28,7 @@
all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([emqx]),
Config.

View File

@ -37,6 +37,7 @@ init_per_suite(Config) ->
code:add_path(filename:join([AppPath, "_build", "default", "lib", "emqx_mini_plugin", "ebin"])),
put(loaded_file, filename:join([DataPath, "loaded_plugins"])),
emqx_ct_helpers:boot_modules([]),
emqx_ct_helpers:start_apps([], fun set_sepecial_cfg/1),
Config.

View File

@ -22,6 +22,7 @@
-include_lib("common_test/include/ct.hrl").
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]),
Config.

View File

@ -27,6 +27,7 @@
all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules([router]),
emqx_ct_helpers:start_apps([]),
Config.

View File

@ -32,6 +32,7 @@
all() -> emqx_ct:all(?SUITE).
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]),
Config.

View File

@ -42,6 +42,7 @@
all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]),
Config.

View File

@ -26,6 +26,7 @@
all() -> [t_start_traces].
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]),
Config.

View File

@ -24,6 +24,7 @@
all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]),
Config.