【问题标题】:erlang otp application is not restarted when it crasheserlang otp应用程序崩溃时不会重新启动
【发布时间】:2015-05-12 23:09:29
【问题描述】:

我一直在阅读Erlang and OTP In Action一书,并尝试了第 4 章中构建 OTP 应用程序的源代码。

有一个 gen_server 有这些回调方法 (full source):

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

init([Port]) ->
    {ok, LSock} = gen_tcp:listen(Port, [{active, true}]),
    {ok, #state{port = Port, lsock = LSock}, 0}.

handle_call(get_count, _From, State) ->
    {reply, {ok, State#state.request_count}, State}.

handle_cast(stop, State) ->
    {stop, normal, State}.

handle_info({tcp, Socket, RawData}, State) ->
    do_rpc(Socket, RawData),
    RequestCount = State#state.request_count,
    {noreply, State#state{request_count = RequestCount + 1}};
handle_info(timeout, #state{lsock = LSock} = State) ->
    {ok, _Sock} = gen_tcp:accept(LSock),
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

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

%%%===================================================================
%%% Internal functions
%%%===================================================================

监督者init([]) 方法看起来像这样(full source):

init([]) ->
    Server = {tr_server, {tr_server, start_link, []},
              permanent, 2000, worker, [tr_server]},
    Children = [Server],
    RestartStrategy = {one_for_one, 0, 1},
    {ok, {RestartStrategy, Children}}.

我已经使用application:start(tcp_rpc) 启动了应用程序,并通过远程登录进入了应用程序。当我退出 telnet 会话时,抛出以下 erlang 错误:

=ERROR REPORT==== 11-Mar-2015::08:12:58 ===
** Generic server tr_server terminating 
** Last message in was {tcp_closed,#Port<0.733>}
** When Server state == {state,1055,#Port<0.725>,5}
** Reason for termination == 
** {function_clause,[{tr_server,handle_info,
                                [{tcp_closed,#Port<0.733>},
                                 {state,1055,#Port<0.725>,5}],
                                [{file,"src/tr_server.erl"},{line,87}]},
                     {gen_server,try_dispatch,4,
                                 [{file,"gen_server.erl"},{line,593}]},
                     {gen_server,handle_msg,5,
                                 [{file,"gen_server.erl"},{line,659}]},
                     {proc_lib,init_p_do_apply,3,
                               [{file,"proc_lib.erl"},{line,237}]}]}

=INFO REPORT==== 11-Mar-2015::08:12:58 ===
    application: tcp_rpc
    exited: shutdown
    type: temporary

我可以看到应用程序正在崩溃,因为它没有tcp_closed 的处理程序,但是我希望主管重新启动应用程序,但事实并非如此。

主管是否应该重新启动应用程序?

【问题讨论】:

  • 不,你写的意思是主管允许0次崩溃,所以在服务器第一次崩溃后主管给出的,它已经达到它的计数,这意味着应用程序放弃了。
  • 我想我只是在第一次阅读 RestartStrategy 的描述时并没有考虑它,并认为提供的配置可以重启崩溃的应用程序。

标签: erlang


【解决方案1】:

您必须将重启策略更改为类似

RestartStrategy = {one_for_one, 10, 1},

因为0、1表示app根本无法重启。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多