Преглед изворни кода

refactor(emqx): create emqx_machine app

Zaiming Shi пре 4 година
родитељ
комит
4bbd398550

+ 0 - 0
apps/.gitkeep


+ 1 - 1
apps/emqx/src/emqx.app.src

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

+ 18 - 60
apps/emqx/src/emqx_app.erl

@@ -46,24 +46,15 @@
 
 
 start(_Type, _Args) ->
 start(_Type, _Args) ->
     ok = maybe_load_config(),
     ok = maybe_load_config(),
-    ok = set_backtrace_depth(),
-    print_otp_version_warning(),
-    print_banner(),
     %% Load application first for ekka_mnesia scanner
     %% Load application first for ekka_mnesia scanner
-    _ = load_ce_modules(),
     ekka:start(),
     ekka:start(),
     ok = ekka_rlog:wait_for_shards(?EMQX_SHARDS, infinity),
     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, Sup} = emqx_sup:start_link(),
+    ok = maybe_start_listeners(),
     ok = start_autocluster(),
     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(),
     ok = emqx_alarm_handler:load(),
-    print_vsn(),
+    register(emqx, self()),
     {ok, Sup}.
     {ok, Sup}.
 
 
 prep_stop(_State) ->
 prep_stop(_State) ->
@@ -89,52 +80,22 @@ maybe_load_config() ->
             emqx_config:init_load(emqx_schema, ConfFiles)
             emqx_config:init_load(emqx_schema, ConfFiles)
     end.
     end.
 
 
-set_backtrace_depth() ->
-    Depth = emqx_config:get([node, backtrace_depth]),
-    _ = erlang:system_flag(backtrace_depth, Depth),
-    ok.
-
--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.
+maybe_start_listeners() ->
+    case emqx_boot:is_enabled(listeners) of
+        true ->
+            ok = emqx_listeners:start();
+        false ->
+            ok
+    end.
 
 
--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() ->
 get_description() ->
     {ok, Descr0} = application:get_key(?APP, description),
     {ok, Descr0} = application:get_key(?APP, description),
@@ -164,9 +125,6 @@ get_release() ->
 release_in_macro() ->
 release_in_macro() ->
     element(2, ?EMQX_RELEASE).
     element(2, ?EMQX_RELEASE).
 
 
-%%--------------------------------------------------------------------
-%% Autocluster
-%%--------------------------------------------------------------------
 start_autocluster() ->
 start_autocluster() ->
     ekka:callback(prepare, fun emqx:shutdown/1),
     ekka:callback(prepare, fun emqx:shutdown/1),
     ekka:callback(reboot,  fun emqx:reboot/0),
     ekka:callback(reboot,  fun emqx:reboot/0),

+ 1 - 1
apps/emqx/src/emqx_schema.erl

@@ -160,7 +160,7 @@ fields("node") ->
     , {"dist_net_ticktime", t(duration(), "vm_args.-kernel net_ticktime", "2m")}
     , {"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_min", t(range(1024, 65535), "kernel.inet_dist_listen_min", 6369)}
     , {"dist_listen_max", t(range(1024, 65535), "kernel.inet_dist_listen_max", 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") ->
 fields("rpc") ->

+ 15 - 0
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 <contact@emqx.io>"]},
+  {links, [{"Homepage", "https://emqx.io/"},
+           {"Github", "https://github.com/emqx/emqx"}
+          ]}
+]}.

+ 77 - 0
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.

+ 34 - 0
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, []}}.

+ 2 - 1
rebar.config.erl

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