erlang otp 应用程序在崩溃时不会重新启动
erlang otp application is not restarted when it crashes
我一直在阅读 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
的处理程序,但是我希望该应用程序由主管重新启动,但事实并非如此。
主管是否应该重新启动应用程序?
您必须将重启策略更改为
RestartStrategy = {one_for_one, 10, 1},
因为0, 1表示应用根本无法重启
我一直在阅读 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
的处理程序,但是我希望该应用程序由主管重新启动,但事实并非如此。
主管是否应该重新启动应用程序?
您必须将重启策略更改为
RestartStrategy = {one_for_one, 10, 1},
因为0, 1表示应用根本无法重启