erlang otp application is not restarted when it crashes
I've been reading the book Erlang and OTP In Action and trying the source code in chapter 4 which builds an OTP application.
There is a gen_server that has these call back methods (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
%%%===================================================================
The superviser init([])
method looks like this (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}}.
I've started the application with application:start(tcp_rpc)
and telneted into the application. When I exit the telnet session, the following erlang error was thrown:
=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
I can see that the application is crashing because it doesn't have a handler for tcp_closed
, however I was expecting the application to be restarted by the supervisor, but it wasn't.
Should the supervisor have restarted the application?
You have to change restart strategy to something like
RestartStrategy = {one_for_one, 10, 1},
because 0, 1 means that app can't be restarted at all.
链接地址: http://www.djcxy.com/p/38224.html