refactor(emqx): create emqx_machine app

This commit is contained in:
Zaiming Shi 2021-07-28 11:05:49 +02:00
parent 7e00805af3
commit 4bbd398550
8 changed files with 148 additions and 63 deletions

View File

View File

@ -1,6 +1,6 @@
{application, emqx,
[{id, "emqx"},
{description, "EMQ X"},
{description, "EMQ X Core"},
{vsn, "5.0.0"}, % strict semver, bump manually!
{modules, []},
{registered, []},

View File

@ -46,24 +46,15 @@
start(_Type, _Args) ->
ok = maybe_load_config(),
ok = set_backtrace_depth(),
print_otp_version_warning(),
print_banner(),
%% Load application first for ekka_mnesia scanner
_ = load_ce_modules(),
ekka:start(),
ok = ekka_rlog:wait_for_shards(?EMQX_SHARDS, infinity),
false == os:getenv("EMQX_NO_QUIC")
andalso application:ensure_all_started(quicer),
ok = maybe_start_quicer(),
{ok, Sup} = emqx_sup:start_link(),
ok = maybe_start_listeners(),
ok = start_autocluster(),
% ok = emqx_plugins:init(),
_ = emqx_plugins:load(),
_ = start_ce_modules(),
emqx_boot:is_enabled(listeners) andalso (ok = emqx_listeners:start()),
register(emqx, self()),
ok = emqx_alarm_handler:load(),
print_vsn(),
register(emqx, self()),
{ok, Sup}.
prep_stop(_State) ->
@ -89,52 +80,22 @@ maybe_load_config() ->
emqx_config:init_load(emqx_schema, ConfFiles)
end.
set_backtrace_depth() ->
Depth = emqx_config:get([node, backtrace_depth]),
_ = erlang:system_flag(backtrace_depth, Depth),
ok.
maybe_start_listeners() ->
case emqx_boot:is_enabled(listeners) of
true ->
ok = emqx_listeners:start();
false ->
ok
end.
-ifndef(EMQX_ENTERPRISE).
load_ce_modules() ->
application:load(emqx_modules).
start_ce_modules() ->
application:ensure_all_started(emqx_modules).
-else.
load_ce_modules() ->
ok.
start_ce_modules() ->
ok.
-endif.
%%--------------------------------------------------------------------
%% Print Banner
%%--------------------------------------------------------------------
-if(?OTP_RELEASE> 22).
print_otp_version_warning() -> ok.
-else.
print_otp_version_warning() ->
?ULOG("WARNING: Running on Erlang/OTP version ~p. Recommended: 23~n",
[?OTP_RELEASE]).
-endif. % OTP_RELEASE
-ifndef(TEST).
print_banner() ->
?ULOG("Starting ~s on node ~s~n", [?APP, node()]).
print_vsn() ->
?ULOG("~s ~s is running now!~n", [get_description(), get_release()]).
-else. % TEST
print_vsn() ->
ok.
print_banner() ->
ok.
-endif. % TEST
maybe_start_quicer() ->
case os:getenv("EMQX_NO_QUIC") of
X when X =:= "1" orelse X =:= "true" ->
ok;
_ ->
{ok, _} = application:ensure_all_started(quicer),
ok
end.
get_description() ->
{ok, Descr0} = application:get_key(?APP, description),
@ -164,9 +125,6 @@ get_release() ->
release_in_macro() ->
element(2, ?EMQX_RELEASE).
%%--------------------------------------------------------------------
%% Autocluster
%%--------------------------------------------------------------------
start_autocluster() ->
ekka:callback(prepare, fun emqx:shutdown/1),
ekka:callback(reboot, fun emqx:reboot/0),

View File

@ -160,7 +160,7 @@ fields("node") ->
, {"dist_net_ticktime", t(duration(), "vm_args.-kernel net_ticktime", "2m")}
, {"dist_listen_min", t(range(1024, 65535), "kernel.inet_dist_listen_min", 6369)}
, {"dist_listen_max", t(range(1024, 65535), "kernel.inet_dist_listen_max", 6369)}
, {"backtrace_depth", t(integer(), undefined, 23)}
, {"backtrace_depth", t(integer(), "emqx_machine.backtrace_depth", 23)}
];
fields("rpc") ->

View File

@ -0,0 +1,15 @@
{application, emqx_machine,
[{id, "emqx_machine"},
{description, "The EMQ X Machine"},
{vsn, "0.1.0"}, % strict semver, bump manually!
{modules, []},
{registered, []},
{applications, [kernel,stdlib]},
{mod, {emqx_machine_app,[]}},
{env, []},
{licenses, ["Apache-2.0"]},
{maintainers, ["EMQ X Team <contact@emqx.io>"]},
{links, [{"Homepage", "https://emqx.io/"},
{"Github", "https://github.com/emqx/emqx"}
]}
]}.

View File

@ -0,0 +1,77 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2021 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_machine_app).
-export([ start/2
, stop/1
, prep_stop/1
]).
-behaviour(application).
-include_lib("emqx/include/logger.hrl").
start(_Type, _Args) ->
ok = set_backtrace_depth(),
ok = print_otp_version_warning(),
_ = load_modules(),
{ok, _} = application:ensure_all_started(emqx),
_ = emqx_plugins:load(),
_ = start_modules(),
ok = print_vsn(),
emqx_machine_sup:start_link().
prep_stop(_State) ->
application:stop(emqx).
stop(_State) ->
ok.
set_backtrace_depth() ->
{ok, Depth} = application:get_env(emqx_machine, backtrace_depth),
_ = erlang:system_flag(backtrace_depth, Depth),
ok.
-if(?OTP_RELEASE > 22).
print_otp_version_warning() -> ok.
-else.
print_otp_version_warning() ->
?ULOG("WARNING: Running on Erlang/OTP version ~p. Recommended: 23~n",
[?OTP_RELEASE]).
-endif. % OTP_RELEASE > 22
-ifdef(TEST).
print_vsn() -> ok.
-else. % TEST
print_vsn() ->
?ULOG("~s ~s is running now!~n", [emqx_app:get_description(), emqx_app:get_release()]).
-endif. % TEST
-ifndef(EMQX_ENTERPRISE).
load_modules() ->
application:load(emqx_modules).
start_modules() ->
application:ensure_all_started(emqx_modules).
-else.
load_modules() ->
ok.
start_modules() ->
ok.
-endif.

View File

@ -0,0 +1,34 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2021 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_machine_sup).
-behaviour(supervisor).
-export([ start_link/0
]).
-export([init/1]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
SupFlags = #{strategy => one_for_all,
intensity => 0,
period => 1
},
{ok, {SupFlags, []}}.

View File

@ -264,7 +264,8 @@ relx_apps(ReleaseType) ->
, inets
, compiler
, runtime_tools
, emqx
, {emqx, load} % started by emqx_machine
, emqx_machine
, {mnesia, load}
, {ekka, load}
, {emqx_plugin_libs, load}