public
Last active

  • Download Gist
erlang supervisor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
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.
[]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.