主管不会在econnrefused上重启(在init / 1中抛出)

时间:2017-06-09 09:49:31

标签: erlang otp supervisor

我有一个gen_server的init函数我连接到rabbitmq。当一切都很好然后它完全正常,但是当连接到rabbitmq失败并且我调用exit时,该过程没有重新启动。

我想在调用exit之后让主管重启此过程。

从概念上讲,我的init功能是这样的:

init(_Args) ->
  process_flag(trap_exit, true),
  case connect() of
    {error, econnrefused} ->
            timer:sleep(1000),
            exit(econnrefused);
    {ok, Connection} ->
            .....
  end,
  {ok, {}}.

这是我的主管:

-module(tasks_manager_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
-define(SERVER, ?MODULE).

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

init([]) ->
    {ok,
     {#{strategy => one_for_one, 
        intensity => 50,
        period => 10},
      [#{id => tasks_manager_serv_id,
         start => {tasks_manager_serv, start_link, []},
         restart => permanent,
         shutdown => brutal_kill,
         type => worker,
         modules => [tasks_manager_serv]}]}}.

我回忆的错误如下。您可以看到此错误没有重新启动,它只是终止:

Starting {global,tasks_da_serv} (<0.479.0>)
Starting {global,tasks_manager_serv} (<0.483.0>)

 =INFO REPORT==== 9-Jun-2017::09:52:46 ===
     application: tasks
     exited: {{shutdown,
                  {failed_to_start_child,tasks_manager_sup_id,
                      {shutdown,
                          {failed_to_start_child,tasks_manager_serv_id,
                              econnrefused}}}},
              {tasks_app,start,[normal,[]]}}
     type: permanent
 {"Kernel pid terminated",application_controller,"{application_start_failure,tasks,{{shutdown,{failed_to_start_child,tasks_manager_sup_id,{shutdown,{failed_to_start_child,tasks_manager_serv_id,econnrefused}}}},{tasks_app,start,[normal,[]]}}}"}
 Kernel pid terminated (application_controller) ({application_start_failure,tasks,{{shutdown,{failed_to_start_child,tasks_manager_sup_id,{shutdown,{failed_to_start_child,tasks_manager_serv_id,econnrefu

我还尝试向self()(来自init函数)投射邮件并在handle_cast中连接到兔子,但它也不起作用。

我还在学习Erlang / OTP,请原谅我,如果我问的是明显但我在文档中找不到任何答案。

1 个答案:

答案 0 :(得分:0)

感谢下面的评论,我能够解决它。 基本上问题是由于exit(econnrefused)init/1函数中,因此进程未正确启动。这就是为什么主管没有重新启动流程的原因 - 它不会重新启动无法初始化的进程。

现在我发送消息给self(),然后像hangle_info/2一样抓住它:

init(_Args) ->
    process_flag(trap_exit, true),
    io:format("Starting ~p (~p)~n", [{global, ?MODULE}, self()]),
    self() ! connect,
    {ok, {}}.

handle_info(connect, State) ->
    {ok, Connection, Channel} = establish_rabbit_connection(),
    {noreply, #state{connection = Connection, channel = Channel}};