2016-12-16 76 views
0

我有一個測試模塊和一個simple_one_for_one主管。Eralng 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()開始主管殼。之後,我運行這個命令:test:run(fun() -> erlang:throw(err) end).我除了函數do_job重新啓動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,[]}]} 

回答

2

重啓兒童是違反simple_one_for_one上司是如何定義的。每supervisor docs

功能delete_child/2和restart_child/2是simple_one_for_one監事和回報{錯誤,simple_one_for_one}如果指定的主管使用此重啓策略無效。

換句話說,你所要求的是永遠不會發生的。這是因爲simple_one_for_one適用於在請求孩子時通過傳遞額外的啓動參數即時定義的動態孩子。其他主管可以重新啓動他們的子項,因爲啓動參數在主管中是靜態定義的。

基本上,這種類型的主管嚴格地確保在您需要擁有一個動態的工作人員池時進行整齊關閉。