supervisor(3erl) | Erlang Module Definition | supervisor(3erl) |
supervisor - Generic supervisor behavior.
This behavior module provides a supervisor, a process that supervises other processes called child processes. A child process can either be another supervisor or a worker process. Worker processes are normally implemented using one of the gen_event, gen_server, or gen_statem behaviors. A supervisor implemented using this module has a standard set of interface functions and include functionality for tracing and error reporting. Supervisors are used to build a hierarchical process structure called a supervision tree, a nice way to structure a fault-tolerant application. For more information, see Supervisor Behaviour in OTP Design Principles.
A supervisor expects the definition of which child processes to supervise to be specified in a callback module exporting a predefined set of functions.
Unless otherwise stated, all functions in this module fail if the specified supervisor does not exist or if bad arguments are specified.
The supervisor is responsible for starting, stopping, and monitoring its child processes. The basic idea of a supervisor is that it must keep its child processes alive by restarting them when necessary.
The children of a supervisor are defined as a list of child specifications. When the supervisor is started, the child processes are started in order from left to right according to this list. When the supervisor terminates, it first terminates its child processes in reversed start order, from right to left.
The supervisor properties are defined by the supervisor flags. The type definition for the supervisor flags is as follows:
sup_flags() = #{strategy => strategy(), % optional
intensity => non_neg_integer(), % optional
period => pos_integer()} % optional
A supervisor can have one of the following restart strategies specified with the strategy key in the above map:
Functions delete_child/2 and restart_child/2 are invalid for simple_one_for_one supervisors and return {error,simple_one_for_one} if the specified supervisor uses this restart strategy.
Function terminate_child/2 can be used for children under simple_one_for_one supervisors by specifying the child's pid() as the second argument. If instead the child specification identifier is used, terminate_child/2 return {error,simple_one_for_one}.
As a simple_one_for_one supervisor can have many children, it shuts them all down asynchronously. This means that the children do their cleanup in parallel, and therefore the order in which they are stopped is not defined.
To prevent a supervisor from getting into an infinite loop of child process terminations and restarts, a maximum restart intensity is defined using two integer values specified with keys intensity and period in the above map. Assuming the values MaxR for intensity and MaxT for period, then, if more than MaxR restarts occur within MaxT seconds, the supervisor terminates all child processes and then itself. The termination reason for the supervisor itself in that case will be shutdown. intensity defaults to 1 and period defaults to 5.
The type definition of a child specification is as follows:
child_spec() = #{id => child_id(), % mandatory
start => mfargs(), % mandatory
restart => restart(), % optional
shutdown => shutdown(), % optional
type => worker(), % optional
modules => modules()} % optional
The old tuple format is kept for backwards compatibility, see child_spec(), but the map is preferred.
The id key is mandatory.
Notice that this identifier on occations has been called "name". As far as possible, the terms "identifier" or "id" are now used but to keep backward compatibility, some occurences of "name" can still be found, for example in error messages.
The start function must create and link to the child process, and must return {ok,Child} or {ok,Child,Info}, where Child is the pid of the child process and Info any term that is ignored by the supervisor.
The start function can also return ignore if the child process for some reason cannot be started, in which case the child specification is kept by the supervisor (unless it is a temporary child) but the non-existing child process is ignored.
If something goes wrong, the function can also return an error tuple {error,Error}.
Notice that the start_link functions of the different behavior modules fulfill the above requirements.
The start key is mandatory.
The restart key is optional. If it is not specified, it defaults to permanent.
If the child process is another supervisor, the shutdown time must be set to infinity to give the subtree ample time to shut down.
It is also allowed to set it to infinity, if the child process is a worker.
Notice that all child processes implemented using the standard OTP behavior modules automatically adhere to the shutdown protocol.
The shutdown key is optional. If it is not specified, it defaults to 5000 if the child is of type worker and it defaults to infinity if the child is of type supervisor.
The type key is optional. If it is not specified, it defaults to worker.
The modules key is optional. If it is not specified, it defaults to [M], where M comes from the child's start {M,F,A}.
child() = undefined | pid()
child_id() = term()
Not a pid().
child_spec() =
#{id := child_id(),
start := mfargs(),
restart => restart(),
shutdown => shutdown(),
type => worker(),
modules => modules()} |
{Id :: child_id(),
StartFunc :: mfargs(),
Restart :: restart(),
Shutdown :: shutdown(),
Type :: worker(),
Modules :: modules()}
The tuple format is kept for backward compatibility only. A map is preferred; see more details above.
mfargs() =
{M :: module(), F :: atom(), A :: [term()] | undefined}
Value undefined for A (the argument list) is only to be used internally in supervisor. If the restart type of the child is temporary, the process is never to be restarted and therefore there is no need to store the real argument list. Value undefined is then stored instead.
modules() = [module()] | dynamic
restart() = permanent | transient | temporary
shutdown() = brutal_kill | timeout()
strategy() =
one_for_all | one_for_one | rest_for_one | simple_one_for_one
sup_flags() =
#{strategy => strategy(),
intensity => integer() >= 0,
period => integer() >= 1} |
{RestartStrategy :: strategy(),
Intensity :: integer() >= 0,
Period :: integer() >= 1}
The tuple format is kept for backward compatibility only. A map is preferred; see more details above.
sup_ref() =
(Name :: atom()) |
{Name :: atom(), Node :: node()} |
{global, Name :: atom()} |
{via, Module :: module(), Name :: any()} |
pid()
worker() = worker | supervisor
check_childspecs(ChildSpecs) -> Result
Types:
Takes a list of child specification as argument and returns ok if all of them are syntactically correct, otherwise {error,Error}.
count_children(SupRef) -> PropListOfCounts
Types:
Returns a property list (see proplists) containing the counts for each of the following elements of the supervisor's child specifications and managed processes:
For a description of SupRef, see start_child/2.
delete_child(SupRef, Id) -> Result
Types:
Tells supervisor SupRef to delete the child specification identified by Id. The corresponding child process must not be running. Use terminate_child/2 to terminate it.
For a description of SupRef, see start_child/2.
If successful, the function returns ok. If the child specification identified by Id exists but the corresponding child process is running or is about to be restarted, the function returns {error,running} or {error,restarting}, respectively. If the child specification identified by Id does not exist, the function returns {error,not_found}.
get_childspec(SupRef, Id) -> Result
Types:
Returns the child specification map for the child identified by Id under supervisor SupRef. The returned map contains all keys, both mandatory and optional.
For a description of SupRef, see start_child/2.
restart_child(SupRef, Id) -> Result
Types:
Tells supervisor SupRef to restart a child process corresponding to the child specification identified by Id. The child specification must exist, and the corresponding child process must not be running.
Notice that for temporary children, the child specification is automatically deleted when the child terminates; thus, it is not possible to restart such children.
For a description of SupRef, see start_child/2.
If the child specification identified by Id does not exist, the function returns {error,not_found}. If the child specification exists but the corresponding process is already running, the function returns {error,running}.
If the child process start function returns {ok,Child} or {ok,Child,Info}, the pid is added to the supervisor and the function returns the same value.
If the child process start function returns ignore, the pid remains set to undefined and the function returns {ok,undefined}.
If the child process start function returns an error tuple or an erroneous value, or if it fails, the function returns {error,Error}, where Error is a term containing information about the error.
start_child(SupRef, ChildSpec) -> startchild_ret()
Types:
startchild_ret() =
{ok, Child :: child()} |
{ok, Child :: child(), Info :: term()} |
{error, startchild_err()}
startchild_err() =
already_present | {already_started, Child :: child()} | term()
Dynamically adds a child specification to supervisor SupRef, which starts the corresponding child process.
SupRef can be any of the following:
ChildSpec must be a valid child specification (unless the supervisor is a simple_one_for_one supervisor; see below). The child process is started by using the start function as defined in the child specification.
For a simple_one_for_one supervisor, the child specification defined in Module:init/1 is used, and ChildSpec must instead be an arbitrary list of terms List. The child process is then started by appending List to the existing start function arguments, that is, by calling apply(M, F, A++List), where {M,F,A} is the start function defined in the child specification.
For a simple_one_for_one supervisor, when a child process start function returns ignore, the functions returns {ok,undefined} and no child is added to the supervisor.
If the child process start function returns an error tuple or an erroneous value, or if it fails, the child specification is discarded, and the function returns {error,Error}, where Error is a term containing information about the error and child specification.
start_link(Module, Args) -> startlink_ret()
start_link(SupName, Module, Args) -> startlink_ret()
Types:
startlink_ret() =
{ok, pid()} | ignore | {error, startlink_err()}
startlink_err() =
{already_started, pid()} | {shutdown, term()} | term()
sup_name() =
{local, Name :: atom()} |
{global, Name :: atom()} |
{via, Module :: module(), Name :: any()}
Creates a supervisor process as part of a supervision tree. For example, the function ensures that the supervisor is linked to the calling process (its supervisor).
The created supervisor process calls Module:init/1 to find out about restart strategy, maximum restart intensity, and child processes. To ensure a synchronized startup procedure, start_link/2,3 does not return until Module:init/1 has returned and all child processes have been started.
If no name is provided, the supervisor is not registered.
Module is the name of the callback module.
Args is any term that is passed as the argument to Module:init/1.
terminate_child(SupRef, Id) -> Result
Types:
Tells supervisor SupRef to terminate the specified child.
If the supervisor is not simple_one_for_one, Id must be the child specification identifier. The process, if any, is terminated and, unless it is a temporary child, the child specification is kept by the supervisor. The child process can later be restarted by the supervisor. The child process can also be restarted explicitly by calling restart_child/2. Use delete_child/2 to remove the child specification.
If the child is temporary, the child specification is deleted as soon as the process terminates. This means that delete_child/2 has no meaning and restart_child/2 cannot be used for these children.
If the supervisor is simple_one_for_one, Id must be the pid() of the child process. If the specified process is alive, but is not a child of the specified supervisor, the function returns {error,not_found}. If the child specification identifier is specified instead of a pid(), the function returns {error,simple_one_for_one}.
If successful, the function returns ok. If there is no child specification with the specified Id, the function returns {error,not_found}.
For a description of SupRef, see start_child/2.
which_children(SupRef) -> [{Id, Child, Type, Modules}]
Types:
Returns a newly created list with information about all child specifications and child processes belonging to supervisor SupRef.
Notice that calling this function when supervising many childrens under low memory conditions can cause an out of memory exception.
For a description of SupRef, see start_child/2.
The following information is given for each child specification/process:
The following function must be exported from a supervisor callback module.
Module:init(Args) -> Result
Types:
Whenever a supervisor is started using start_link/2,3, this function is called by the new process to find out about restart strategy, maximum restart intensity, and child specifications.
Args is the Args argument provided to the start function.
SupFlags is the supervisor flags defining the restart strategy and maximum restart intensity for the supervisor. [ChildSpec] is a list of valid child specifications defining which child processes the supervisor must start and monitor. See the discussion in section Supervision Principles earlier.
Notice that when the restart strategy is simple_one_for_one, the list of child specifications must be a list with one child specification only. (The child specification identifier is ignored.) No child process is then started during the initialization phase, but all children are assumed to be started dynamically using start_child/2.
The function can also return ignore.
Notice that this function can also be called as a part of a code upgrade procedure. Therefore, the function is not to have any side effects. For more information about code upgrade of supervisors, see section Changing a Supervisor in OTP Design Principles.
gen_event(3erl), gen_statem(3erl), gen_server(3erl), sys(3erl)
stdlib 3.7.1 | Ericsson AB |