[erlang-questions] child processes order of termination
Vance Shipley
vances@REDACTED
Wed May 4 18:08:13 CEST 2011
On Wed, May 04, 2011 at 11:42:17AM +0300, Martin Dimitrov wrote:
} On application stop what is the order of termination of the supervised
} processes?
The children are stopped in reverse order of starting.
1> {ok, Sup} = supervisor:start_link(super_order, [[1,2,3], [4,5,6], [7,8,9]]).
init(1)
init(2)
init(3)
init(4)
init(5)
init(6)
init(7)
init(8)
init(9)
{ok,<0.33.0>}
2> exit(Sup, shutdown).
true
terminate(9)
terminate(8)
terminate(7)
terminate(6)
terminate(5)
terminate(4)
terminate(3)
terminate(2)
terminate(1)
} The module2 is also a supervisor, will its children be terminated before
} proceeding with the top one?
If you are sure to specify Shutdown=infinity and Type=supervisor in your
child specification for the supervisor the parent supervisor will wait
for it to exit before going on to shutdown the preceeding children.
--
-Vance
-------------- next part --------------
-module(super_order).
-export([init/1]).
-behaviour(supervisor).
init([IdsA, IdsB, IdsC]) ->
ChildSpecsA = [server_child_spec(Id) || Id <- IdsA],
ChildSpecsB = [super_child_spec(IdsB)],
ChildSpecsC = [server_child_spec(Id) || Id <- IdsC],
ChildSpecs = ChildSpecsA ++ ChildSpecsB ++ ChildSpecsC,
{ok,{{one_for_one, 10, 60}, ChildSpecs}};
init([Ids]) ->
ChildSpecs = [server_child_spec(Id) || Id <- Ids],
{ok,{{one_for_one, 10, 60}, ChildSpecs}}.
server_child_spec(Id) ->
StartMod = server_order,
StartFunc = {gen_server, start_link, [StartMod, [Id], []]},
{Id, StartFunc, permanent, 4000, worker, [StartMod]}.
super_child_spec(Ids) ->
StartMod = super_order,
StartFunc = {supervisor, start_link, [StartMod, [Ids]]},
{Ids, StartFunc, permanent, infinity, supervisor, [StartMod]}.
-------------- next part --------------
-module(server_order).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
init([Id]) ->
io:fwrite("init(~b)~n", [Id]),
process_flag(trap_exit, true),
{ok, Id}.
handle_cast(_, State) ->
{noreply, State}.
handle_call(_, _, State) ->
{noreply, State}.
handle_info(_, State) ->
{noreply, State}.
terminate(_, Id) ->
io:fwrite("terminate(~b)~n", [Id]).
code_change(_, State, _) ->
{ok, State}.
More information about the erlang-questions
mailing list