Pārlūkot izejas kodu

refactor: move `emqx_machine_boot:post_init` to app booter's `init`

Thales Macedo Garitezi 1 gadu atpakaļ
vecāks
revīzija
9fa7e16692

+ 1 - 1
apps/emqx/test/emqx_common_test_helpers.erl

@@ -908,7 +908,7 @@ setup_node(Node, Opts) when is_map(Opts) ->
                     %% setting extra apps to be restarted after
                     %% joining.
                     set_envs(Node, Env),
-                    ok = erpc:call(Node, emqx_machine_boot, start_autocluster, [])
+                    ok = erpc:call(Node, emqx_machine_app_booter, start_autocluster, [])
                 end,
             case rpc:call(Node, ekka, join, [JoinTo]) of
                 ok ->

+ 23 - 2
apps/emqx_machine/src/emqx_machine_app_booter.erl

@@ -39,6 +39,8 @@
     handle_info/2
 ]).
 
+%% Internal exports
+-export([start_autocluster/0]).
 %% Internal exports (for `emqx_machine_terminator' only)
 -export([do_stop_apps/0]).
 
@@ -73,11 +75,14 @@ init(_Opts) ->
     %% It would still be problematic if a join request arrives before this process is
     %% started, though.
     ekka:callback(stop, fun emqx_machine_boot:stop_apps/0),
+    do_start_apps(),
+    ok = print_vsn(),
+    ok = start_autocluster(),
     State = #{},
     {ok, State}.
 
 handle_call(#start_apps{}, _From, State) ->
-    handle_start_apps(),
+    do_start_apps(),
     {reply, ok, State};
 handle_call(#stop_apps{}, _From, State) ->
     do_stop_apps(),
@@ -106,11 +111,27 @@ do_stop_apps() ->
     ok = emqx_plugins:ensure_stopped(),
     lists:foreach(fun stop_one_app/1, lists:reverse(emqx_machine_boot:sorted_reboot_apps())).
 
+start_autocluster() ->
+    ekka:callback(stop, fun emqx_machine_boot:stop_apps/0),
+    ekka:callback(start, fun emqx_machine_boot:ensure_apps_started/0),
+    %% returns 'ok' or a pid or 'any()' as in spec
+    _ = ekka:autocluster(emqx),
+    ok.
+
 %%------------------------------------------------------------------------------
 %% Internal fns
 %%------------------------------------------------------------------------------
 
-handle_start_apps() ->
+-ifdef(TEST).
+print_vsn() -> ok.
+% TEST
+-else.
+print_vsn() ->
+    ?ULOG("~ts ~ts is running now!~n", [emqx_app:get_description(), emqx_app:get_release()]).
+% TEST
+-endif.
+
+do_start_apps() ->
     ?SLOG(notice, #{msg => "(re)starting_emqx_apps"}),
     lists:foreach(fun start_one_app/1, emqx_machine_boot:sorted_reboot_apps()),
     ?tp(emqx_machine_boot_apps_started, #{}).

+ 0 - 24
apps/emqx_machine/src/emqx_machine_boot.erl

@@ -18,10 +18,8 @@
 -include_lib("emqx/include/logger.hrl").
 -include_lib("snabbkaffe/include/snabbkaffe.hrl").
 
--export([post_boot/0]).
 -export([stop_apps/0, ensure_apps_started/0]).
 -export([sorted_reboot_apps/0]).
--export([start_autocluster/0]).
 -export([stop_port_apps/0]).
 -export([read_apps/0]).
 -export([restart_type/1]).
@@ -42,28 +40,6 @@
 %% release, depending on the build flags:
 -define(OPTIONAL_APPS, [bcrypt, observer]).
 
-post_boot() ->
-    ok = ensure_apps_started(),
-    ok = print_vsn(),
-    ok = start_autocluster(),
-    ignore.
-
--ifdef(TEST).
-print_vsn() -> ok.
-% TEST
--else.
-print_vsn() ->
-    ?ULOG("~ts ~ts is running now!~n", [emqx_app:get_description(), emqx_app:get_release()]).
-% TEST
--endif.
-
-start_autocluster() ->
-    ekka:callback(stop, fun emqx_machine_boot:stop_apps/0),
-    ekka:callback(start, fun emqx_machine_boot:ensure_apps_started/0),
-    %% returns 'ok' or a pid or 'any()' as in spec
-    _ = ekka:autocluster(emqx),
-    ok.
-
 stop_apps() ->
     emqx_machine_app_booter:stop_apps().
 

+ 1 - 3
apps/emqx_machine/src/emqx_machine_sup.erl

@@ -29,16 +29,14 @@ start_link() ->
 
 init([]) ->
     Terminator = child_worker(emqx_machine_terminator, [], transient),
+    %% Must start before `app_booter'.
     ReplicantHealthProbe = child_worker(emqx_machine_replicant_health_probe, [], transient),
-    %% Must start before `post_boot'.
     Booter = child_worker(emqx_machine_app_booter, [], permanent),
-    BootApps = child_worker(emqx_machine_boot, post_boot, [], temporary),
     GlobalGC = child_worker(emqx_global_gc, [], permanent),
     Children = [
         Terminator,
         ReplicantHealthProbe,
         Booter,
-        BootApps,
         GlobalGC
     ],
     SupFlags = #{

+ 1 - 1
apps/emqx_plugins/test/emqx_plugins_SUITE.erl

@@ -978,7 +978,7 @@ t_start_node_with_plugin_enabled(Config) when is_list(Config) ->
             %% order, and also we need to override the config loader to emulate what
             %% `emqx_cth_cluster' does and avoid the node crashing due to lack of config
             %% keys.
-            ok = ?ON(N2, emqx_machine_boot:start_autocluster()),
+            ok = ?ON(N2, emqx_machine_app_booter:start_autocluster()),
             ?ON(N2, begin
                 StartCallback0 =
                     case ekka:env({callback, start}) of