This section should be read in conjunction with
A supervisor is responsible for starting, stopping and monitoring its child processes. The basic idea of a supervisor is that it should keep its child processes alive by restarting them when necessary.
Which child processes to start and monitor is specified by a
list of
The callback module for a supervisor starting the server from
the
-module(ch_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link(ch_sup, []).
init(_Args) ->
{ok, {{one_for_one, 1, 60},
[{ch3, {ch3, start_link, []},
permanent, brutal_kill, worker, [ch3]}]}}.
1 and 60 defines the
The tuple
If a child process terminates, only that process is restarted.
If a child process terminates, all other child processes are terminated and then all child processes, including the terminated one, are restarted.
If a child process terminates, the 'rest' of the child processes -- i.e. the child processes after the terminated process in start order -- are terminated. Then the terminated child process and the rest of the child processes are restarted.
The supervisors have a built-in mechanism to limit the number of
restarts which can occur in a given time interval. This is
determined by the values of the two parameters
init(...) ->
{ok, {{RestartStrategy, MaxR, MaxT},
[ChildSpec, ...]}}.
If more than
When the supervisor terminates, then the next higher level supervisor takes some action. It either restarts the terminated supervisor, or terminates itself.
The intention of the restart mechanism is to prevent a situation where a process repeatedly dies for the same reason, only to be restarted again.
This is the type definition for a child specification:
It should be (or result in) a call to
Be careful by setting the
This information is used by the release handler during
upgrades and downgrades, see
Example: The child specification to start the server
{ch3,
{ch3, start_link, []},
permanent, brutal_kill, worker, [ch3]}
Example: A child specification to start the event manager from
the chapter about
{error_man,
{gen_event, start_link, [{local, error_man}]},
permanent, 5000, worker, dynamic}
Both the server and event manager are registered processes which
can be expected to be accessible at all times, thus they are
specified to be
Example: A child specification to start another supervisor:
{sup,
{sup, start_link, []},
transient, infinity, supervisor, [sup]}
In the example above, the supervisor is started by calling
start_link() ->
supervisor:start_link(ch_sup, []).
In this case, the supervisor is not registered. Instead its pid
must be used. A name can be specified by calling
The new supervisor process calls the callback function
init(_Args) ->
{ok, {{one_for_one, 1, 60},
[{ch3, {ch3, start_link, []},
permanent, brutal_kill, worker, [ch3]}]}}.
The supervisor then starts all its child processes according to
the child specifications in the start specification. In this case
there is one child process,
Note that
In addition to the static supervision tree, we can also add dynamic child processes to an existing supervisor with the following call:
supervisor:start_child(Sup, ChildSpec)
Child processes added using
Any child process, static or dynamic, can be stopped in accordance with the shutdown specification:
supervisor:terminate_child(Sup, Id)
The child specification for a stopped child process is deleted with the following call:
supervisor:delete_child(Sup, Id)
As with dynamically added child processes, the effects of deleting a static child process is lost if the supervisor itself restarts.
A supervisor with restart strategy
Example of a callback module for a simple_one_for_one supervisor:
-module(simple_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link(simple_sup, []).
init(_Args) ->
{ok, {{simple_one_for_one, 0, 1},
[{call, {call, start_link, []},
temporary, brutal_kill, worker, [call]}]}}.
When started, the supervisor will not start any child processes. Instead, all child processes are added dynamically by calling:
supervisor:start_child(Sup, List)
For example, adding a child to
supervisor:start_child(Pid, [id1])
results in the child process being started by calling
call:start_link(id1)
A child under a
supervisor:terminate_child(Sup, Pid)
where
Since the supervisor is part of a supervision tree, it will automatically be terminated by its supervisor. When asked to shutdown, it will terminate all child processes in reversed start order according to the respective shutdown specifications, and then terminate itself.