根主管
-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
设置为捕获退出信号,并且
- 主管子规范中定义的关闭策略
是一个整数超时值,而不是
野蛮杀戮。
这就是我们打电话erlang:process_flag(trap_exit, true)
来的原因Module:init(Args)
。
样品运行
启动根主管:
1> root_sup:start_link().
ch1 has started (<0.35.0>)
{ok,<0.33.0>}
根主管运行并自动启动其子进程,在我们的例子中是子主管。子主管依次启动其子进程;在我们的案例中,我们只有一个孩子,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
(注意ch1 has started (<0.39.0>)
)。
由于我们的 shell 和 root supervisor 是双向链接的(调用supervisor:start_link
,而不是supervisor:start
在 root supervisor function中start_link/0
),exit(normal)
导致 root supervisor 关闭,但它的子进程有一些时间来清理。