I read Learn Some Erlang about supervisors and completely lost. They have stop and terminate functions
Whenever you want to terminate an application, you have the top supervisor of the VM shut down (this is done for you with functions like init:stop/1). Then that supervisor asks each of its children to terminate. If some of the children are supervisors, they do the same:
which seem to send shutdown message to recceive 'EXIT' confirmation
So, stop is called to shut processes down. But, later in the text however, they say that exit function (a new fruit!) must be called instead
When the top-level supervisor is asked to terminate, it calls exit(ChildPid, shutdown) on each of the Pids. If the child is a worker and trapping exits, it'll call its own terminate function. Otherwise, it's just going to die. When a supervisor gets the shutdown signal, it will forward it to its own children the same way.
Finally, they define stop function in the child module
-module(musicians).
-behaviour(gen_server).
-export([start_link/2, stop/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2]).
stop(Role) -> gen_server:call(Role, stop).
where is that init:stop defined?
They also send a stop message
handle_call(stop, _From, S=#state{}) -> {stop, normal, ok, S};
Their handle_info
handle_info(timeout, S = #state{name=N, skill=bad}) ->
case random:uniform(5) of
1 -> io:format("~s played a false note. Uh oh~n",[N]),
{stop, bad_note, S};
_ -> io:format("~s produced sound!~n",[N]),
{noreply, S, ?DELAY}
end;
sheds some light upon the connection between its reply and terminate
terminate(normal, S) ->
io:format("~s left the room (~s)~n",[S#state.name, S#state.role]);
terminate(bad_note, S) ->
io:format("~s sucks! kicked that member out of the band! (~s)~n",
[S#state.name, S#state.role]);
terminate(shutdown, S) ->
io:format("The manager is mad and fired the whole band! "
"~s just got back to playing in the subway~n", [S#state.name]);
Yet, it all looks a mess. Can you tie up the things together?