繁体   English   中英

Erlang simple_one_for_one 主管不重启孩子

[英]Erlang simple_one_for_one supervisor does not restart child

我有一个测试模块和一个 simple_one_for_one 主管。

测试.erl

-module(test).

-export([
    run/1,
    do_job/1
]).

run(Fun) ->
    test_sup:start_child([Fun]).


do_job(Fun) ->
    Pid = spawn(Fun),
    io:format("started ~p~n", [Pid]),
    {ok, Pid}.

test_sup.erl

-module(test_sup).
-behaviour(supervisor).

-export([start_link/0]).
-export([init/1]).
-export([start_child/1]).

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


init(_Args) ->
    SupFlags = #{strategy => simple_one_for_one, intensity => 2, period => 20},

    ChildSpecs = [#{id => test,
                    start => {test, do_job, []},
                    restart => permanent,
                    shutdown => brutal_kill,
                    type => worker,
                    modules => [test]}],

    {ok, {SupFlags, ChildSpecs}}.


start_child(Args) ->
    supervisor:start_child(?MODULE, Args).

我通过命令test_sup:start_link()在 shell 中启动主管。 之后我运行这个命令: test:run(fun() -> erlang:throw(err) end). 我除了函数do_job restart 2 次,但它从来没有。 问题是什么?

这是外壳:

1> test_sup:start_link().
{ok,<0.36.0>}
2> test:run(fun() -> erlang:throw(err) end).
started <0.38.0>
{ok,<0.38.0>}
3> 
=ERROR REPORT==== 16-Dec-2016::22:08:41 ===
Error in process <0.38.0> with exit value:
{{nocatch,err},[{erlang,apply,2,[]}]}

重新启动孩子与simple_one_for_one主管的定义方式相反。 根据主管文档

函数 delete_child/2 和 restart_child/2 对 simple_one_for_one 主管无效,如果指定的主管使用此重启策略,则返回 {error,simple_one_for_one}。

换句话说,你所要求的永远不会发生。 那是因为simple_one_for_one用于动态子项,这些子项是通过在请求子项时传入额外的启动参数来动态定义的。 其他主管能够重新启动他们的孩子,因为启动参数是在主管中静态定义的。

基本上,这种类型的主管严格用于确保在需要动态工作人员池时有序关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM