为什么我的主管在带有 undef 的 start_child 上失败?

Ber*_*ian 2 erlang module erlang-supervisor

我正在尝试运行一个simple_one_for_one supervisorwheresupervisorworker放置在单独的模块中,并且在使用时我不断收到以下错误supervisor:start_child

>A=sup:start_link().
>B=supervisor:start_child(A,[]).
{error,{'EXIT',{undef,[{worker,start_link,[],[]},
                       {supervisor,do_start_child_i,3,
                                   [{file,"supervisor.erl"},{line,379}]},
                       {supervisor,handle_call,3,
                                   [{file,"supervisor.erl"},{line,404}]},
                       {gen_server,try_handle_call,4,
                                   [{file,"gen_server.erl"},{line,661}]},
                       {gen_server,handle_msg,6,
                                   [{file,"gen_server.erl"},{line,690}]},
                       {proc_lib,init_p_do_apply,3,
                                 [{file,"proc_lib.erl"},{line,249}]}]}}}
Run Code Online (Sandbox Code Playgroud)

导师

-module(sup).
-behaviour(supervisor).
-compile([export_all]).

start_link()->
    {ok,Pid}=supervisor:start_link(?MODULE,[]),
    io:format("sugi pl"),
    Pid.



init(_Args) ->
     RestartStrategy = {simple_one_for_one, 10, 60},
     ChildSpec = {
                  worker, 
                  {worker, start_link, []},  //tried adding here a parameter in the A
                  permanent,
                  brutal_kill, 
                  worker,
                  [sup]
                },
    {ok, {RestartStrategy,[ChildSpec]}}.
Run Code Online (Sandbox Code Playgroud)

工人

-module(worker).
-compile(export_all).


start_link([Arg])->  //tried both [Arg] and Arg
    {ok,Pid}=spawn_link(?MODULE,init,[]),
    Pid.


init([Time])->
    receive->
        {From,Msg}->From !{Time,Msg},
                     init(Time)
    end.
Run Code Online (Sandbox Code Playgroud)

命令

>c("[somepath]/sup.erl"),A=sup:start_link(),B=supervisor:start_child(A,[]).
Run Code Online (Sandbox Code Playgroud)

我可以清楚地看到,问题是当试图添加child.Somehow的init函数没有正确调用,但我不明白为什么。(Badmatch)
我曾尝试在添加一个参数AMFAChildSpec无果

Ste*_*ski 6

您的代码存在许多问题。

  • 的返回值sup:start_link/0错误;你正在返回 Pid 而不是{ok, Pid}.
  • 虽然它并不是真的不正确,但您使用的是supervisor:start_link/2,它没有注册主管名称。有名字很方便,所以最好使用supervisor:start_link/3

    supervisor:start_link({local, ?MODULE}, ?MODULE, []),
    
    Run Code Online (Sandbox Code Playgroud)

    这将模块名称与其进程 ID 相关联,允许您在 shell 命令中使用进程名称而不是使用 pid 变量。

  • 你有一个io:format/2电话sup:start_link/0,大概是为了调试。一个更好的调试方法是在sys:trace(sup, true)您启动sup主管后从您的 shell调用。您也可以从 shell 中关闭它,方法是指定false而不是true作为第二个参数。

解决上述问题后,使用以下定义sup:start_link/0

start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
Run Code Online (Sandbox Code Playgroud)

让我们重新编译,启动监督程序,然后编译worker(修复其语法错误后),然后在我们尝试启动子进程时跟踪监督程序:

1> c(sup).
sup.erl:3: Warning: export_all flag enabled - all functions will be exported
{ok,sup}
2> sup:start_link().
{ok,<0.94.0>}
3> sys:trace(sup, true).
ok
4> c(worker).
worker.erl:2: Warning: export_all flag enabled - all functions will be exported
worker.erl:5: Warning: variable 'Arg' is unused
{ok,worker}
5> supervisor:start_child(sup, []).
*DBG* sup got call {start_child,[]} from <0.81.0>
*DBG* sup sent {error,
                   {'EXIT',
                       {undef,
                           [{worker,start_link,[],[]},
                            {supervisor,do_start_child_i,3,
                                [{file,"supervisor.erl"},{line,379}]},
...
Run Code Online (Sandbox Code Playgroud)

这个简短的跟踪输出显示sup当它尝试worker通过调用启动 a 时死亡worker:start_link/0[]指示有零个参数)。子规范告诉sup以这种方式启动它,因为它包含

{worker, start_link, []}
Run Code Online (Sandbox Code Playgroud)

我们通过supervisor:start_child(sup, []). 对于simple_one_for_one子进程,发送到其 start 函数的参数由子规范中的参数列表与调用中指定的参数组成supervisor:start_child/2;在这种情况下,这相当于[] ++ []which 与 相同[],表示没有参数。让我们将worker:start_link/1函数改为worker:start_link/0改为,重新编译它,然后再试一次:

6> c(worker).
worker.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,worker}
7> supervisor:start_child(sup, []).
*DBG* sup got call {start_child,[]} from <0.81.0>
*DBG* sup sent {error,
                   {'EXIT',
                       {{badmatch,<0.94.0>},
                        [{worker,start_link,0,[{file,"worker.erl"},{line,6}]},
...
Run Code Online (Sandbox Code Playgroud)

这一次,缩写输出显示了一个badmatch. 这是因为spawn_link/3返回一个 pid,但worker:start_link/0期望它返回{ok, Pid}。让我们解决这个问题,并将返回值修复为,{ok, Pid}而不仅仅是Pid

start_link()->
    Pid = spawn_link(?MODULE,init,[]),
    {ok, Pid}.
Run Code Online (Sandbox Code Playgroud)

然后让我们重新编译并重试:

8> c(worker).
worker.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,worker}
9> supervisor:start_child(sup, []).
*DBG* sup got call {start_child,[]} from <0.81.0>
*DBG* sup sent {ok,<0.106.0>} to <0.81.0>, new state {state,
                                                      {local,sup},
                                                      simple_one_for_one,
                                                      {[worker],
                                                       #{worker =>
                                                          {child,undefined,
                                                           worker,
                                                           {worker,
                                                            start_link,[]},
                                                           permanent,
                                                           brutal_kill,worker,
                                                           [sup]}}},
                                                      {maps,
                                                       #{<0.106.0> => []}},
                                                      10,60,[],0,sup,[]}
*DBG* sup got {'EXIT',<0.106.0>,{undef,[{worker,init,[],[]}]}}
Run Code Online (Sandbox Code Playgroud)

好的,这次监督者实际上启动了孩子,但它立即死亡,因为它试图调用worker:init/0但仅worker:init/1被定义。因为孩子立即死亡,监督者根据其重启策略反复尝试启动它:

RestartStrategy = {simple_one_for_one, 10, 60},
Run Code Online (Sandbox Code Playgroud)

因为这是一个硬错误,它每次都会立即失败,并且主管会在 60 秒或更短的时间内重启 10 次失败后死亡,就像它应该的那样:

=SUPERVISOR REPORT==== 20-Apr-2020::10:43:43.557307 ===
    supervisor: {local,sup}
    errorContext: shutdown
    reason: reached_max_restart_intensity
    offender: [{pid,<0.117.0>},
               {id,worker},
               {mfargs,{worker,start_link,[]}},
               {restart_type,permanent},
               {shutdown,brutal_kill},
               {child_type,worker}]
** exception error: shutdown
Run Code Online (Sandbox Code Playgroud)

从您的代码看来,您正试图将某种Time参数传递给worker:init/1,因此让我们更改start_link/0为传递时间戳:

start_link()->
    Pid = spawn_link(?MODULE,init,[os:timestamp()]),
    {ok, Pid}.
Run Code Online (Sandbox Code Playgroud)

让我们也修复init/1直接获取参数,而不是在列表中:

init(Time) ->
    receive
        {From,Msg} ->
            From ! {Time,Msg},
            init(Time)
end.
Run Code Online (Sandbox Code Playgroud)

让我们重新启动主管,重新编译worker,然后再试一次:

10> sup:start_link().
{ok,<0.119.0>}
11> sys:trace(sup, true).
ok
12> c(worker).
worker.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,worker}
13> {ok, Child} = supervisor:start_child(sup, []).
*DBG* sup got call {start_child,[]} from <0.118.0>
*DBG* sup sent {ok,<0.127.0>} to <0.118.0>, new state {state,
                                                       {local,sup},
                                                       simple_one_for_one,
                                                       {[worker],
                                                        #{worker =>
                                                           {child,undefined,
                                                            worker,
                                                            {worker,
                                                             start_link,[]},
                                                            permanent,
                                                            brutal_kill,
                                                            worker,
                                                            [sup]}}},
                                                       {maps,
                                                        #{<0.127.0> => []}},
                                                       10,60,[],0,sup,[]}
{ok,<0.127.0>}
Run Code Online (Sandbox Code Playgroud)

看起来它奏效了。让我们看看主管是否同意,通过询问它有多少个孩子:

14> supervisor:count_children(sup).
...
[{specs,1},{active,1},{supervisors,0},{workers,1}]
Run Code Online (Sandbox Code Playgroud)

正如我们预期的那样,它只有一名工人。最后,让我们向 worker 发送一条消息,看看它是否按预期响应:

15> Child ! {self(), "are you there?"}.
{<0.118.0>,"are you there?"}
16> flush().
Shell got {{1587,394860,258120},"are you there?"}
ok
Run Code Online (Sandbox Code Playgroud)

现在这一切似乎都奏效了。

最后一个解决方法是更改​​子规范中的模块;而不是[sup]它应该是模块本身,[worker]. 随着这一变化,您修改后的工作模块如下。您可能还想重新考虑是否要permanent为孩子使用,因为这是一个simple_one_for_one主管;transient可能是一个更好的选择,但我把它保留为最初写的。考虑查看主管行为文档以获取更多信息。

导师

-module(sup).
-behaviour(supervisor).
-compile([export_all]).

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

init(_Args) ->
     RestartStrategy = {simple_one_for_one, 10, 60},
     ChildSpec = {
                  worker,
                  {worker, start_link, []},
                  permanent,
                  brutal_kill,
                  worker,
                  [worker]
                },
    {ok, {RestartStrategy,[ChildSpec]}}.
Run Code Online (Sandbox Code Playgroud)

工人

-module(worker).
-compile(export_all).

start_link()->
    Pid = spawn_link(?MODULE,init,[os:timestamp()]),
    {ok, Pid}.

init(Time) ->
    receive
        {From,Msg} ->
            From ! {Time,Msg},
            init(Time)
    end.
Run Code Online (Sandbox Code Playgroud)