From 4bbd39855063feaa727ecd48067e9ef0a487d865 Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Wed, 28 Jul 2021 11:05:49 +0200 Subject: [PATCH] refactor(emqx): create emqx_machine app --- apps/.gitkeep | 0 apps/emqx/src/emqx.app.src | 2 +- apps/emqx/src/emqx_app.erl | 78 +++++----------------- apps/emqx/src/emqx_schema.erl | 2 +- apps/emqx_machine/src/emqx_machine.app.src | 15 +++++ apps/emqx_machine/src/emqx_machine_app.erl | 77 +++++++++++++++++++++ apps/emqx_machine/src/emqx_machine_sup.erl | 34 ++++++++++ rebar.config.erl | 3 +- 8 files changed, 148 insertions(+), 63 deletions(-) delete mode 100644 apps/.gitkeep create mode 100644 apps/emqx_machine/src/emqx_machine.app.src create mode 100644 apps/emqx_machine/src/emqx_machine_app.erl create mode 100644 apps/emqx_machine/src/emqx_machine_sup.erl diff --git a/apps/.gitkeep b/apps/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/apps/emqx/src/emqx.app.src b/apps/emqx/src/emqx.app.src index 546b70f14..57f7cd57f 100644 --- a/apps/emqx/src/emqx.app.src +++ b/apps/emqx/src/emqx.app.src @@ -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, []}, diff --git a/apps/emqx/src/emqx_app.erl b/apps/emqx/src/emqx_app.erl index a35c90747..47e886b9f 100644 --- a/apps/emqx/src/emqx_app.erl +++ b/apps/emqx/src/emqx_app.erl @@ -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), diff --git a/apps/emqx/src/emqx_schema.erl b/apps/emqx/src/emqx_schema.erl index 3fd060d9f..a35dfefc2 100644 --- a/apps/emqx/src/emqx_schema.erl +++ b/apps/emqx/src/emqx_schema.erl @@ -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") -> diff --git a/apps/emqx_machine/src/emqx_machine.app.src b/apps/emqx_machine/src/emqx_machine.app.src new file mode 100644 index 000000000..21da4a4c8 --- /dev/null +++ b/apps/emqx_machine/src/emqx_machine.app.src @@ -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 "]}, + {links, [{"Homepage", "https://emqx.io/"}, + {"Github", "https://github.com/emqx/emqx"} + ]} +]}. diff --git a/apps/emqx_machine/src/emqx_machine_app.erl b/apps/emqx_machine/src/emqx_machine_app.erl new file mode 100644 index 000000000..f86bb57e8 --- /dev/null +++ b/apps/emqx_machine/src/emqx_machine_app.erl @@ -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. diff --git a/apps/emqx_machine/src/emqx_machine_sup.erl b/apps/emqx_machine/src/emqx_machine_sup.erl new file mode 100644 index 000000000..367e6d13e --- /dev/null +++ b/apps/emqx_machine/src/emqx_machine_sup.erl @@ -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, []}}. diff --git a/rebar.config.erl b/rebar.config.erl index ac84ead5a..d8a6e7ea2 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -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}