Skip to content

Instantly share code, notes, and snippets.

@seven1240
Created December 27, 2010 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save seven1240/756214 to your computer and use it in GitHub Desktop.
Save seven1240/756214 to your computer and use it in GitHub Desktop.
file: g.erl
-module(g).
-compile(export_all).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
%%-------------------------------------------------------------------
%% Public API
%%-------------------------------------------------------------------
start(Name) ->
gen_server:start({local, Name}, ?MODULE, [], []).
stop(Name) ->
gen_server:call(Name, stop).
state(Name) ->
gen_server:call(Name, state).
crash(Name) ->
gen_server:call(Name, crash).
%%-------------------------------------------------------------------
%% gen_server call backs
%%-------------------------------------------------------------------
init([]) ->
say("init", []),
{ok, []}.
handle_call(stop, _From, State) ->
say("stopping by ~p, state was ~p.", [_From, State]),
{stop, normal, stopped, State};
handle_call(state, _From, State) ->
say("~p is asking for the state.", [_From]),
{reply, State, State};
handle_call(crash, _From, State) ->
say("~p is asking for the state.", [_From]),
{xxxreply, State, State};
handle_call(_Request, _From, State) ->
say("call ~p, ~p, ~p.", [_Request, _From, State]),
{reply, ok, State}.
handle_cast(_Msg, State) ->
say("cast ~p, ~p.", [_Msg, State]),
{noreply, State}.
handle_info(_Info, State) ->
say("info ~p, ~p.", [_Info, State]),
{noreply, State}.
terminate(_Reason, _State) ->
say("terminate ~p, ~p", [_Reason, _State]),
ok.
code_change(_OldVsn, State, _Extra) ->
say("code_change ~p, ~p, ~p", [_OldVsn, State, _Extra]),
{ok, State}.
%%-------------------------------------------------------------------
%% Private functions
%%-------------------------------------------------------------------
say(Format) ->
say(Format, []).
say(Format, Data) ->
io:format("~p:~p: ~s~n", [?MODULE, self(), io_lib:format(Format, Data)]).
file: sup.erl
-module(sup).
-behaviour(supervisor).
%% External exports
-export([start_link/0, add_child/1]).
%% supervisor callbacks
-export([init/1]).
%% @spec start_link() -> ServerRet
%% @doc API for starting the supervisor.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
add_child(Name) ->
Spec = {Name,
{g, start, [Name]},
permanent, 1000, worker, dynamic},
supervisor:start_child(?MODULE, Spec).
%% @spec init([]) -> SupervisorTree
%% @doc supervisor callback.
init([]) ->
Names = [a, b, c],
Processes = [{N,
{g, start, [N]},
permanent, 1000, worker, dynamic} || N <- Names],
io:format("Processes: ~p~n", [Processes]),
{ok, {{one_for_one, 10, 10}, Processes}}.
file: a.erl
-module(a).
-behaviour(application).
-export([ start/2, stop/1 ]).
start(_Type, _StartArgs) ->
case sup:start_link() of
{ok, Pid} -> {ok, Pid};
Other -> {error, Other}
end.
stop(_State) -> ok.
file: a.app
{application, a,
[{description, "test"},
{vsn, "0.1.0"},
{modules, [a, sup, g]},
{registered, [sup]},
{applications, [kernel, stdlib]},
{mod, {a, []}} ]}.
console output:
Erlang R13B02 (erts-5.7.3) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [kernel-poll:false]
Eshell V5.7.3 (abort with ^G)
1> c(a).
{ok,a}
2> c(sup).
{ok,sup}
3> c(g).
{ok,g}
4> application:start(a).
Processes: [{a,{g,start,[a]},permanent,1000,worker,dynamic},
{b,{g,start,[b]},permanent,1000,worker,dynamic},
{c,{g,start,[c]},permanent,1000,worker,dynamic}]
g:<0.54.0>: init
g:<0.55.0>: init
g:<0.56.0>: init
ok
5> g:state(a).
g:<0.54.0>: {<0.33.0>,#Ref<0.0.0.186>} is asking for the state.
[]
6> supervisor:which_children(sup).
[{c,<0.56.0>,worker,dynamic},
{b,<0.55.0>,worker,dynamic},
{a,<0.54.0>,worker,dynamic}]
7> g:crash(a).
g:<0.54.0>: {<0.33.0>,#Ref<0.0.0.195>} is asking for the state.
g:<0.54.0>: terminate {bad_return_value,{xxxreply,[],[]}}, []
=ERROR REPORT==== 27-Dec-2010::23:32:11 ===
** Generic server a terminating
** Last message in was crash
** When Server state == []
** Reason for termination ==
** {bad_return_value,{xxxreply,[],[]}}
** exception exit: {{bad_return_value,{xxxreply,[],[]}},
{gen_server,call,[a,crash]}}
in function gen_server:call/2
8> supervisor:which_children(sup).
[{c,<0.56.0>,worker,dynamic},
{b,<0.55.0>,worker,dynamic},
{a,<0.54.0>,worker,dynamic}]
9> sup:add_child(d).
g:<0.63.0>: init
{ok,<0.63.0>}
10> supervisor:which_children(sup).
[{d,<0.63.0>,worker,dynamic},
{c,<0.56.0>,worker,dynamic},
{b,<0.55.0>,worker,dynamic},
{a,<0.54.0>,worker,dynamic}]
11> g:state(d).
g:<0.63.0>: {<0.60.0>,#Ref<0.0.0.215>} is asking for the state.
[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment