我写了一个名为director的新库。
这是一个主管图书馆。
它的一个特点是给director带来了arity 2的乐趣,director会在每次进程崩溃时调用函数,第一个参数是崩溃原因,第二个是崩溃计数,例如:
-module(director_test).
-behaviour(director).
-export([start_link/0, init/1]).
start_link() ->
director:start_link(?MODULE, []).
init([]) ->
ChildSpec = #{id => foo,
start => {m, f, args},
plan => [fun my_plan/2],
count => infinity},
{ok, [ChildSpec]}.
my_plan(normal, Count) when Count rem 10 == 0 ->
%% If process crashed with reason normal after every 10 times
%%, director will restart it after spending 3000 milliseconds.
{restart, 3000};
my_plan(normal, _Count) ->
%% If process crashed with reason normal director will restart its
restart;
my_plan(killed, _Count) ->
%% If process was killed, Director will delete it from its children
delete;
my_plan(Reason, Count) ->
%% For other reasons, director will crash with reason {foo_crashed, Reason}
{stop, {foo_crashed, Reason}}.
我在 Slack 中宣布了我的库,他们想知道以这种方式编写新主管!有人说“我倾向于不让主管处理退避”。
最后他们没有告诉我干净的信息,我认为我需要更多地了解主管及其职责等。我认为主管是一个应该了解何时重新启动哪个孩子以及何时删除哪个孩子以及何时不重新启动的过程重启哪个孩子。我对吗?
你能告诉我一些我在 Director 中没有的 OTP/Supervisor 的好功能吗?(导演特征列表)