与 simple_one_for_one 主管开始 child

Starting a child with simple_one_for_one supervisor

如何与 simple_one_for_one 主管开始 child? 我正在阅读 LYSE 书,目前在 Dynamic Supervision 部分:http://learnyousomeerlang.com/supervisors#dynamic-supervision

作者开始child是这样的,但是我不知道djembe是在哪里定义的:

1> supervisor:start_child(band_supervisor, [djembe, good]).
Musician Janet Tennelli, playing the djembe entered the room
{ok,<0.690.0>}
2> supervisor:start_child(band_supervisor, [djembe, good]).
{error,{already_started,<0.690.0>}}

这是我的尝试:

2> supervisor:start_link(band_supervisor, jamband).
{ok,<0.40.0>}
3> supervisor:start_child(band_supervisor, [djembe, good]).

=ERROR REPORT==== 28-Feb-2016::03:52:56 ===
** Generic server <0.40.0> terminating
** Last message in was {'EXIT',<0.33.0>,
                           {{noproc,
                                {gen_server,call,
                                    [band_supervisor,
                                     {start_child,[djembe,good]},
                                     infinity]}},
                            [{gen_server,call,3,
                                 [{file,"gen_server.erl"},{line,212}]},
                             {erl_eval,do_apply,6,
                                 [{file,"erl_eval.erl"},{line,673}]},
                             {shell,exprs,7,[{file,"shell.erl"},{line,686}]},
                             {shell,eval_exprs,7,
                                 [{file,"shell.erl"},{line,641}]},
                             {shell,eval_loop,3,
                                 [{file,"shell.erl"},{line,626}]}]}}
** When Server state == {state,
                            {<0.40.0>,band_supervisor},
                            simple_one_for_one,
                            [{child,undefined,jam_musician,
                                 {musicians,start_link,[]},
                                 temporary,1000,worker,
                                 [musicians]}],
                            undefined,3,60,[],band_supervisor,jamband}
** Reason for termination ==
** {{noproc,{gen_server,call,
                        [band_supervisor,
                         {start_child,[djembe,good]},
                         infinity]}},
    [{gen_server,call,3,[{file,"gen_server.erl"},{line,212}]},
     {erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,673}]},
     {shell,exprs,7,[{file,"shell.erl"},{line,686}]},
     {shell,eval_exprs,7,[{file,"shell.erl"},{line,641}]},
     {shell,eval_loop,3,[{file,"shell.erl"},{line,626}]}]}
** exception exit: {noproc,{gen_server,call,
                                       [band_supervisor,
                                        {start_child,[djembe,good]},
                                        infinity]}}
     in function  gen_server:call/3 (gen_server.erl, line 212)

主管长相如下:

-module(band_supervisor).
-behaviour(supervisor).

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

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

%% The band supervisor will allow its band members to make a few
%% mistakes before shutting down all operations, based on what
%% mood he's in. A lenient supervisor will tolerate more mistakes
%% than an angry supervisor, who'll tolerate more than a
%% complete jerk supervisor
init(lenient) ->
    init({one_for_one, 3, 60});
init(angry) ->
    init({rest_for_one, 2, 60});
init(jerk) ->
    init({one_for_all, 1, 60});
init(jamband) ->
    {ok, {{simple_one_for_one, 3, 60},
         [{jam_musician,
           {musicians, start_link, []},
           temporary, 1000, worker, [musicians]}
         ]}};
init({RestartStrategy, MaxRestart, MaxTime}) ->
    {ok, {{RestartStrategy, MaxRestart, MaxTime},
         [{singer,
           {musicians, start_link, [singer, good]},
           permanent, 1000, worker, [musicians]},
          {bass,
           {musicians, start_link, [bass, good]},
           temporary, 1000, worker, [musicians]},
          {drum,
           {musicians, start_link, [drum, bad]},
           transient, 1000, worker, [musicians]},
          {keytar,
           {musicians, start_link, [keytar, good]},
           transient, 1000, worker, [musicians]}
         ]}}.

以下是音乐家:

-module(musicians).
-behaviour(gen_server).

-export([start_link/2, stop/1]).
-export([init/1, handle_call/3, handle_cast/2,
         handle_info/2, code_change/3, terminate/2]).

-record(state, {name="", role, skill=good}).
-define(DELAY, 750).

start_link(Role, Skill) ->
    gen_server:start_link({local, Role}, ?MODULE, [Role, Skill], []).

stop(Role) -> gen_server:call(Role, stop).

init([Role, Skill]) ->
    %% To know when the parent shuts down
    process_flag(trap_exit, true),
    %% sets a seed for random number generation for the life of the process
    %% uses the current time to do it. Unique value guaranteed by now()
    random:seed(now()),
    TimeToPlay = random:uniform(3000),
    Name = pick_name(),
    StrRole = atom_to_list(Role),
    io:format("Musician ~s, playing the ~s entered the room~n",
              [Name, StrRole]),
    {ok, #state{name=Name, role=StrRole, skill=Skill}, TimeToPlay}.

handle_call(stop, _From, S=#state{}) ->
    {stop, normal, ok, S};
handle_call(_Message, _From, S) ->
    {noreply, S, ?DELAY}.

handle_cast(_Message, S) ->
    {noreply, S, ?DELAY}.

handle_info(timeout, S = #state{name=N, skill=good}) ->
    io:format("~s produced sound!~n",[N]),
    {noreply, S, ?DELAY};
handle_info(timeout, S = #state{name=N, skill=bad}) ->
    case random:uniform(5) of
        1 ->
            io:format("~s played a false note. Uh oh~n",[N]),
            {stop, bad_note, S};
        _ ->
            io:format("~s produced sound!~n",[N]),
            {noreply, S, ?DELAY}
    end;
handle_info(_Message, S) ->
    {noreply, S, ?DELAY}.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

terminate(normal, S) ->
    io:format("~s left the room (~s)~n",[S#state.name, S#state.role]);
terminate(bad_note, S) ->
    io:format("~s sucks! kicked that member out of the band! (~s)~n",
              [S#state.name, S#state.role]);
terminate(shutdown, S) ->
    io:format("The manager is mad and fired the whole band! "
              "~s just got back to playing in the subway~n",
              [S#state.name]);
terminate(_Reason, S) ->
    io:format("~s has been kicked out (~s)~n", [S#state.name, S#state.role]).

%% Yes, the names are based off the magic school bus characters
%% 10 names!
pick_name() ->
    %% the seed must be set for the random functions. Use within the
    %% process that started with init/1
    lists:nth(random:uniform(10), firstnames())
    ++ " " ++
    lists:nth(random:uniform(10), lastnames()).

firstnames() ->
    ["Valerie", "Arnold", "Carlos", "Dorothy", "Keesha",
     "Phoebe", "Ralphie", "Tim", "Wanda", "Janet"].

lastnames() ->
    ["Frizzle", "Perlstein", "Ramon", "Ann", "Franklin",
     "Terese", "Tennelli", "Jamal", "Li", "Perlstein"].

learnyousomeerlang.com 是一个很好的信息来源,但有时我觉得它过于复杂。我通过阅读 supervisor OTP documentation. You probably don't need the gen_server if you just want to understand dynamic children. See my simple implementation:

直接了解了 OTP 的那一部分
-module(estp_proj_sup).
-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1, add/2]).

-define(SERVER, ?MODULE).

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

init([]) ->
    Child = ?WORKER(estp_project),
    {ok, {{simple_one_for_one, 3, 30}, [Child]}}.

add(Name, Cfg) ->
    Server = estp_project:server_name(Name),
    State = [{name, Name}|Cfg],
    case whereis(Server) of
        undefined ->
            add_child(Server, State);
        Pid ->
            delete_child(Server, Pid),
            add_child(Server, State)
    end.

add_child(Server, State) ->
    supervisor:start_child(?SERVER, [Server, State]).

delete_child(Server, Pid) ->
    ok = supervisor:terminate_child(?SERVER, Pid).

动态 child 名称 estp_project:server_name/1 的创建方式如下:

server_name(Name) ->
    Module = atom_to_binary(?MODULE, utf8),
    Binary = atom_to_binary(Name, utf8),
    binary_to_atom(<<Module/binary, <<"$">>/binary, Binary/binary>>, utf8).

worker定义为:

-define(SHUTDOWN_TIMEOUT, 5000).
-define(WORKER(I), {I, {I, start_link, []}, permanent, ?SHUTDOWN_TIMEOUT, worker, [I]}).

然后您只需调用 estp_proj_sup:add(Name, Cfg) 即可添加 children,就像在 this code:

中一样
process(Res) ->
    %% Res contains parsed list of project names and their configurations
    [set_project(Name, Cfg) || {Name, {ok, Cfg}} <- Res].

set_project(Name, Cfg) ->
    case estp_proj_sup:add(Name, Cfg) of
        {ok, _Pid} -> ok;
        {error, _} = Err -> Err
    end.

无论如何,我试过你的例子,它似乎有效:

4> {ok, S} = band_supervisor:start_link(jamband).
{ok,<0.47.0>}
5> supervisor:start_child(band_supervisor, [djembe, good]).
Musician Wanda Terese, playing the djembe entered the room
{ok,<0.49.0>}
Wanda Terese produced sound!
Wanda Terese produced sound!
Wanda Terese produced sound!
Wanda Terese produced sound!
Wanda Terese produced sound!
6> supervisor:start_child(band_supervisor, [djembe, good]).
{error,{already_started,<0.49.0>}}
Wanda Terese produced sound!
Wanda Terese produced sound!