Supervisor不会在econnrefused(在init / 1中抛出)
我有一个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
消息并在handle_cast
连接到rabbit,但它不起作用。
我仍然在学习Erlang / OTP,所以如果我想知道某些明显的东西,请原谅我,但我无法在文档中找到任何答案来解决我的问题。
感谢下面的问题,我能解决这个问题。 基本上问题在于进程没有正确启动,因为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}};
链接地址: http://www.djcxy.com/p/38225.html
上一篇: Supervisor does not restart on econnrefused (thrown in init/1)
下一篇: erlang otp application is not restarted when it crashes