erlang otp应用程序在崩溃时不会重新启动
我一直在阅读Erlang和OTP In Action这本书,并在第4章中尝试构建OTP应用程序的源代码。
有一个gen_server具有这些回调方法(完整源代码):
%%%===================================================================
%%% 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
%%%===================================================================
superviser init([])
方法看起来像这样(完整源代码):
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)
的应用application:start(tcp_rpc)
和telneted到应用程序。 当我退出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
的处理程序,但是我期待应用程序由主管重新启动,但事实并非如此。
主管是否应该重新启动应用程序?
你必须改变重启策略
RestartStrategy = {one_for_one, 10, 1},
因为0,1意味着应用程序根本无法重新启动。
链接地址: http://www.djcxy.com/p/38223.html上一篇: erlang otp application is not restarted when it crashes