Eralng simple_one_for_one主管不会重启孩子

时间:2016-12-16 18:50:07

标签: erlang otp supervisor fault-tolerance

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

test.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重启2次但它从未这样做。有什么问题?

这是shell:

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,[]}]}

1 个答案:

答案 0 :(得分:2)

重新启动子项与simple_one_for_one主管的定义方式相反。根据{{​​3}}:

  

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

换句话说,你要求的东西永远不会发生。这是因为simple_one_for_one适用于通过在请求孩子时传入额外的启动参数而动态定义的动态子级。其他主管可以重新启动他们的孩子,因为启动args是在主管中静态定义的。

基本上,当您需要拥有一个动态的工作池时,这种类型的主管严格用于确保整齐关闭。

相关问题