Supervisors with no children?
Vance Shipley
vances@REDACTED
Fri Jan 30 13:14:28 CET 2004
There are two ways of adding a worker to a supervisor
dynamically; the simple_one_for_one method and the
completely dynamic method.
When you create a supervisor with a simple_one_for_one
restart strategy you include exactly one child
specification in the return from the init/1 callback.
Calls to supervisor:start_child/2 start new instances
of the same child specification.
When the supervisor has a restart strategy other than
simple_one_for_one calls to supervisor:start_child/2
specify the child specification for the worker which
is added to the list of child specifications managed
by that supervisor. You must delete this entry when
it is no longer needed (i.e. the worker has exited).
You might have a gen_server process starting workers:
handle_cast(Event, State) ->
...
supervisor:start_child(Supervisor, ChildSpec),
{noreply, State}.
A gen_* process running as a worker would need to
signal back to the gen_server to clean up after it
died:
terminate(_Reason, _StateName, _State) ->
gen_server:cast(State#state.parent,
{stopped, State#state.supref}).
So the gen_server can clean up the supervisor:
handle_cast({stopped, SupRef}, State) ->
supervisor:delete_child(State#state.sup, SupRef),
{noreply, State}.
One thing I found I wanted to do but was unable to
was to start a supervisor with an empty list of
child specifications. If we can add and delete
the child specifications why not be able to start
the supervisor with no children to begin with?
-Vance
More information about the erlang-questions
mailing list