Procházet zdrojové kódy

refactor(emqx_machine_terminator): future-proof try-catch

Ensure exceptions in emqx_machine:stop_apps/0 is caught
and call init:stop/0 in the after clause
Zaiming Shi před 4 roky
rodič
revize
81c9dcb6ae
1 změnil soubory, kde provedl 16 přidání a 2 odebrání
  1. 16 2
      apps/emqx_machine/src/emqx_machine_terminator.erl

+ 16 - 2
apps/emqx_machine/src/emqx_machine_terminator.erl

@@ -27,6 +27,8 @@
          handle_cast/2, handle_call/3, handle_info/2,
          handle_cast/2, handle_call/3, handle_info/2,
          terminate/2, code_change/3]).
          terminate/2, code_change/3]).
 
 
+-include_lib("emqx/include/logger.hrl").
+
 -define(TERMINATOR, ?MODULE).
 -define(TERMINATOR, ?MODULE).
 -define(DO_IT, graceful_shutdown).
 -define(DO_IT, graceful_shutdown).
 
 
@@ -62,8 +64,20 @@ init(_) ->
     {ok, #{}}.
     {ok, #{}}.
 
 
 handle_info(?DO_IT, State) ->
 handle_info(?DO_IT, State) ->
-    ok = emqx_machine:stop_apps(normal),
-    init:stop(),
+    try
+        emqx_machine:stop_apps(normal)
+    catch
+        C : E : St ->
+            Apps = [element(1, A) || A <- application:which_applications()],
+            ?SLOG(error, #{msg => "failed_to_stop_apps",
+                           exception => C,
+                           reason => E,
+                           stacktrace => St,
+                           remaining_apps => Apps
+                          })
+    after
+        init:stop()
+    end,
     {noreply, State};
     {noreply, State};
 handle_info(_, State) ->
 handle_info(_, State) ->
     {noreply, State}.
     {noreply, State}.