Erlang simple_one_for_one supervisor 不重启 child
Erlang simple_one_for_one supervisor does not restart child
我有一个测试模块和一个 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,[]}]}
重新启动 children 与 simple_one_for_one
主管的定义方式相反。根据 supervisor docs:
Functions delete_child/2 and restart_child/2 are invalid for simple_one_for_one supervisors and return {error,simple_one_for_one} if the specified supervisor uses this restart strategy.
换句话说,你所要求的永远不会发生。这是因为 simple_one_for_one
用于动态 children,当您请求 child 时,动态 children 是通过传入额外的启动参数动态定义的。其他主管能够重新启动他们的 children,因为启动参数是在主管中静态定义的。
基本上,这种类型的 supervisor 严格用于在需要动态工作池时确保整洁关闭。
我有一个测试模块和一个 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,[]}]}
重新启动 children 与 simple_one_for_one
主管的定义方式相反。根据 supervisor docs:
Functions delete_child/2 and restart_child/2 are invalid for simple_one_for_one supervisors and return {error,simple_one_for_one} if the specified supervisor uses this restart strategy.
换句话说,你所要求的永远不会发生。这是因为 simple_one_for_one
用于动态 children,当您请求 child 时,动态 children 是通过传入额外的启动参数动态定义的。其他主管能够重新启动他们的 children,因为启动参数是在主管中静态定义的。
基本上,这种类型的 supervisor 严格用于在需要动态工作池时确保整洁关闭。