Supervisor does not restart on econnrefused (thrown in init/1)

I have a gen_server's init function I connect to rabbitmq. When all is good then it works perfectly fine however when connecting to rabbitmq fails and I call exit the process is not restarted.

I would like to make supervisor restart this process after me calling exit .

Conceptually my init function is like this:

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

And here is my supervisor:

-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]}]}}.

The error I recive is as follows. You can see that there are no restarts on this error, it just terminates:

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

I've tried also casting a message to self() (from init function) and connecting to rabbit in handle_cast but it does not work as well.

I'm still learning Erlang/OTP so forgive me if I'm asking about something obvious but I couldn't find any answer in the docs to my problem.


Thanks to comments below the question I was able to solve it. Basically the problem was that the process was not started correctly because exit(econnrefused) was in init/1 function. That's why supervisor was not restarting the process - it does not restarts processes which are failing to initialize.

Now I'm sending a message to self() and then catch it in hangle_info/2 like this:

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}};
链接地址: http://www.djcxy.com/p/38226.html

上一篇: 自动重启Erlang应用程序

下一篇: Supervisor不会在econnrefused(在init / 1中抛出)