fix(emqx_machine): handle early shutdown

This commit is contained in:
Zaiming Shi 2021-08-06 10:20:42 +02:00
parent 81c9dcb6ae
commit 304b322a0c
2 changed files with 18 additions and 4 deletions

View File

@ -16,8 +16,9 @@
-module(emqx_machine). -module(emqx_machine).
-export([start/0, -export([ start/0
graceful_shutdown/0 , graceful_shutdown/0
, is_ready/0
]). ]).
-export([ stop_apps/1 -export([ stop_apps/1
@ -40,7 +41,6 @@ start() ->
_ = load_modules(), _ = load_modules(),
ok = load_config_files(), ok = load_config_files(),
ok = ensure_apps_started(), ok = ensure_apps_started(),
_ = emqx_plugins:load(), _ = emqx_plugins:load(),
@ -48,6 +48,7 @@ start() ->
ok = print_vsn(), ok = print_vsn(),
ok = start_autocluster(), ok = start_autocluster(),
%% NOTE: keep this to the end
ok = emqx_machine_terminator:start(). ok = emqx_machine_terminator:start().
graceful_shutdown() -> graceful_shutdown() ->
@ -58,6 +59,10 @@ set_backtrace_depth() ->
_ = erlang:system_flag(backtrace_depth, Depth), _ = erlang:system_flag(backtrace_depth, Depth),
ok. ok.
%% @doc Return true if boot is complete.
is_ready() ->
emqx_machine_terminator:is_running().
-if(?OTP_RELEASE > 22). -if(?OTP_RELEASE > 22).
print_otp_version_warning() -> ok. print_otp_version_warning() -> ok.
-else. -else.

View File

@ -21,6 +21,7 @@
-export([ start/0 -export([ start/0
, graceful/0 , graceful/0
, graceful_wait/0 , graceful_wait/0
, is_running/0
]). ]).
-export([init/1, format_status/2, -export([init/1, format_status/2,
@ -40,6 +41,8 @@ start() ->
%% NOTE: Do not link this process under any supervision tree %% NOTE: Do not link this process under any supervision tree
ok. ok.
is_running() -> is_pid(whereis(?TERMINATOR)).
%% @doc Send a signal to activate the terminator. %% @doc Send a signal to activate the terminator.
graceful() -> graceful() ->
?TERMINATOR ! ?DO_IT, ?TERMINATOR ! ?DO_IT,
@ -49,7 +52,8 @@ graceful() ->
graceful_wait() -> graceful_wait() ->
case whereis(?TERMINATOR) of case whereis(?TERMINATOR) of
undefined -> undefined ->
exit(emqx_machine_not_started); ?SLOG(warning, #{msg => "shutdown_before_boot_is_complete"}),
exit_loop();
Pid -> Pid ->
ok = graceful(), ok = graceful(),
Ref = monitor(process, Pid), Ref = monitor(process, Pid),
@ -59,6 +63,11 @@ graceful_wait() ->
receive {'DOWN', Ref, process, Pid, _} -> ok end receive {'DOWN', Ref, process, Pid, _} -> ok end
end. end.
exit_loop() ->
init:stop(),
timer:sleep(100),
exit_loop().
init(_) -> init(_) ->
ok = emqx_machine_signal_handler:start(), ok = emqx_machine_signal_handler:start(),
{ok, #{}}. {ok, #{}}.