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
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
Functions
Function
As a
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
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
The
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
The start function can also return
If something goes wrong, the function can also return an
error tuple
Notice that the
The
The
If the child process is another supervisor, the shutdown time
is to be set to
Be careful when setting the shutdown time to
Notice that all child processes implemented using the standard OTP behavior modules automatically adhere to the shutdown protocol.
The
The
The
Internally, the supervisor also keeps track of the pid
Not a
The tuple format is kept for backward compatibility
only. A map is preferred; see more details
Value
The tuple format is kept for backward compatibility
only. A map is preferred; see more details
Takes a list of child specification as argument
and returns
Returns a property list (see
For a description of
Tells supervisor
For a description of
If successful, the function returns
Returns the child specification map for the child identified
by
For a description of
Tells supervisor
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
If the child specification identified
by
If the child process start function
returns
If the child process start function returns
If the child process start function returns an error tuple
or an erroneous value, or if it fails, the function returns
Dynamically adds a child specification to supervisor
For a
If there already exists a child specification with the specified
identifier,
If the child process start function returns
If the child process start function returns
For a
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
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
If
If
If
If no name is provided, the supervisor is not registered.
If the supervisor and its child processes are successfully
created (that is, if all child process start functions return
If there already exists a process with the specified
If
If
If any child process start function fails or returns an error
tuple or an erroneous value, the supervisor first terminates
all already started child processes with reason
Tells supervisor
If the supervisor is not
If the child is temporary, the child specification is deleted as
soon as the process terminates. This means
that
If the supervisor is
If successful, the function returns
For a description of
Returns a newly created list with information about all child
specifications and child processes belonging to
supervisor
Notice that calling this function when supervising many childrens under low memory conditions can cause an out of memory exception.
For a description of
The following information is given for each child specification/process:
The following function must be exported from a
Whenever a supervisor is started using
Notice that when the restart strategy is
The function can also return
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