Erlang: does the application behavior trap SIGTERM?

一曲冷凌霜 提交于 2020-01-24 19:55:10

问题


I have the following stop function in my behavior module:

start(_StartType, _StartArgs) ->
    ...
stop(_State) ->
    lager:info("Stop recieved."),
    erlang:display("Stop recieved."),
    ok.

My application supervisor looks like:

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

-define(SERVER, ?MODULE).

%%====================================================================
%% API functions
%%====================================================================

start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%%====================================================================
%% Supervisor callbacks
%%====================================================================

%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
init([]) ->
    {ok, { {one_for_all, 0, 1}, []} }.

I don't think I ever modified that file. In fact, it is a bit mystical to me how that connects to the start and stop functions above.

My question is, when I send SIGTERM to my running application, I do not see the logging statements in the stop function appear. That seems bad. Do I need to add something to either the application module or the supervisor module?

(I need to handle SIGTERM and do cleanup, because my application is Dockerized and SIGTERM gets sent to applications running inside of Docker on Docker stop, after which it sends SIGKILL after 10 seconds if the application does not catch SIGTERM.)


回答1:


You'll need to upgrade to the just released Erlang 19.3 as before that SIGTERM was ignored. From the release notes:

erts: A received SIGTERM signal to beam will generate a 'stop' message to the init process and terminate the Erlang VM nicely. This is equivalent to calling init:stop/0.

http://www.erlang.org/news/110




回答2:


shutdown defines how a child process is to be terminated.

brutal_kill means that the child process is unconditionally terminated using exit(Child, kill).

An integer time-out value means that the supervisor tells the child process to terminate by calling exit(Child, shutdown) and then waits for an exit signal back. If no exit signal is received within the specified time, the child process is unconditionally terminated using exit(Child, kill).

If the child process is another supervisor, it is to be set to infinity to give the subtree enough time to shut down. It is also allowed to set it to infinity, if the child process is a worker. See the warning below:

Warning Be careful when setting the shutdown time to infinity when the child process is a worker. Because, in this situation, the termination of the supervision tree depends on the child process; it must be implemented in a safe way and its cleanup procedure must always return.

The shutdown key is optional. If it is not given, and the child is of type worker, the default value 5000 will be used; if the child is of type supervisor, the default value infinity will be used.



来源:https://stackoverflow.com/questions/42912781/erlang-does-the-application-behavior-trap-sigterm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!