ルートスーパーバイザー
-module(root_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1, shutdown/0]).
start_link() ->
supervisor:start_link({local,?MODULE}, ?MODULE, []).
init(_Args) ->
RestartStrategy = {one_for_one, 10, 60},
ListenerSup = {popd_listener_sup,
{popd_listener_sup, start_link, []},
permanent, infinity, supervisor, [popd_listener_sup]},
Children = [ListenerSup],
{ok, {RestartStrategy, Children}}.
% supervisor can be shutdown by calling exit(SupPid,shutdown)
% or, if it's linked to its parent, by parent calling exit/1.
shutdown() ->
exit(whereis(?MODULE), shutdown).
% or
% exit(normal).
子プロセスが別のスーパーバイザーである場合、サブツリーにシャットダウンする十分な時間を与えるためにShutdown
、子の仕様で を に設定する必要があり、に設定する必要があります。infinity
Type
supervisor
子守
-module(popd_listener_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link({local,?MODULE}, ?MODULE, []).
init(_Args) ->
RestartStrategy = {one_for_one, 10, 60},
Listener = {ch1, {ch1, start_link, []},
permanent, 2000, worker, [ch1]},
Children = [Listener],
{ok, {RestartStrategy, Children}}.
ここでは、子仕様で、 の値を に設定Shutdown
します2000
。整数のタイムアウト値は、スーパーバイザーが呼び出しによって子プロセスを終了するように指示し、子プロセスからexit(Child,shutdown)
シャットダウン理由が返される終了シグナルを待機することを意味します。
リスナー
-module(ch1).
-behaviour(gen_server).
% Callback functions which should be exported
-export([init/1]).
-export([handle_cast/2, terminate/2]).
% user-defined interface functions
-export([start_link/0]).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init(_Args) ->
erlang:process_flag(trap_exit, true),
io:format("ch1 has started (~w)~n", [self()]),
% If the initialization is successful, the function
% should return {ok,State}, {ok,State,Timeout} ..
{ok, []}.
handle_cast(calc, State) ->
io:format("result 2+2=4~n"),
{noreply, State};
handle_cast(calcbad, State) ->
io:format("result 1/0~n"),
1 / 0,
{noreply, State}.
terminate(_Reason, _State) ->
io:format("ch1: terminating.~n"),
ok.
Erlang/OTP ドキュメントから:
gen_server が監視ツリーの一部であり、そのスーパーバイザーによって終了するように命令され
ている場合、次の条件が適用されると、関数Module:terminate(Reason, State)
が呼び出されます。Reason=shutdown
gen_server
終了シグナルをトラップするように設定されている
- スーパーバイザーの子仕様で定義されているシャットダウン戦略は、
brutal_kill
ではなく、整数のタイムアウト値です。
それが私たちが電話をかけた理由erlang:process_flag(trap_exit, true)
ですModule:init(Args)
。
サンプルラン
ルート スーパーバイザの起動:
1> root_sup:start_link().
ch1 has started (<0.35.0>)
{ok,<0.33.0>}
ルート スーパーバイザーが実行され、その子プロセス (この場合は子スーパーバイザー) が自動的に開始されます。子スーパーバイザは、その子プロセスを順番に開始します。私たちの場合、子供は 1 人だけch1
です。
ch1
通常のコードを評価させましょう:
2> gen_server:cast(ch1, calc).
result 2+2=4
ok
今いくつかの悪いコード:
3> gen_server:cast(ch1, calcbad).
result 1/0
ok
ch1: terminating.
=ERROR REPORT==== 31-Jan-2011::01:38:44 ===
** Generic server ch1 terminating
** Last message in was {'$gen_cast',calcbad}
** When Server state == []
** Reason for termination ==
** {badarith,[{ch1,handle_cast,2},
{gen_server,handle_msg,5},
{proc_lib,init_p_do_apply,3}]}
ch1 has started (<0.39.0>)
4> exit(normal).
ch1: terminating.
** exception exit: normal
ご覧のとおり、子プロセスch1
は子スーパーバイザーによって再起動されましたpopd_listener_sup
(notice ch1 has started (<0.39.0>)
)。
シェルとルート スーパーバイザは双方向にリンクされているため (ルート スーパーバイザ関数ではsupervisor:start_link
なくを呼び出す)、ルート スーパーバイザはシャットダウンされましたが、その子プロセスはクリーンアップする時間がありました。supervisor:start
start_link/0
exit(normal)