[erlang-questions] Supervisor on-demand

Vance Shipley <>
Fri Feb 29 14:32:02 CET 2008


On Tue, Feb 26, 2008 at 08:56:31AM +0100, Andreas Hillqvist wrote:
}  I do not belive that a simple_one_for_one supervisor restars a process
}  if it crash.

Were that true there wouldn't be a whole lot of reason to be under
supervision.  It is not the case however as demonstrated below.

	-Vance


1> {ok, Super} = supervisor:start_link(super, []).
{ok,<0.33.0>}
2> {ok, Server} = supervisor:start_child(Super, [[], []]).
server initializing.
{ok,<0.35.0>}
3> exit(Server, kill).
server initializing.


-------------- next part --------------
-module(super).
-export([init/1]).
-behaviour(supervisor).

init(_StartArgs) ->
	StartMod = server,
	StartFunc = {gen_server, start_link, [StartMod]},
	ChildSpec = {StartMod, StartFunc, permanent, 4000, worker, [StartMod]},
	{ok,{{simple_one_for_one, 10, 60}, [ChildSpec]}}.

-------------- next part --------------
-module(server).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

init(_) ->
	io:fwrite("~w initializing.~n", [?MODULE]),
	erlang:process_flag(trap_exit, true),
   {ok, []}.

handle_cast(_, State) ->
   {noreply, State}.

handle_call(_Request, _, State) ->
	io:fwrite("~w called.~n", [?MODULE]),
   {reply, ok, State}.

handle_info(_, State) ->
   {noreply, State}.

terminate(_, _) ->
	io:fwrite("~w terminating.~n", [?MODULE]),
	ok.

code_change(_, State, _) ->
	{ok, State}.



More information about the erlang-questions mailing list