A behaviour module for implementing a supervisor, a process which
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 pre-defined set of functions.
Unless otherwise stated, all functions in this module will fail if the specified supervisor does not exist or if bad arguments are given.
The supervisor is responsible for starting, stopping and monitoring its child processes. The basic idea of a supervisor is that it shall 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 properties of a supervisor are defined by the supervisor flags. This is the type definition for the supervisor flags:
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
The functions
The function
Because 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 the
This is the type definition of a child specification:
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
Note that this identifier on occations has been called "name". As far as possible, the terms "identifier" or "id" are now used but in order to keep backwards 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 may also return an
error tuple
Note that the
The
The
If the child process is another supervisor, the shutdown time
should be set to
Be careful when setting the shutdown time to
Note that all child processes implemented using the standard OTP behaviour 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 backwards compatibility
only. A map is preferred; see more details
The value
The tuple format is kept for backwards compatibility
only. A map is preferred; see more details
Creates a supervisor process as part of a supervision tree. The function will, among other things, ensure that the supervisor is linked to the calling process (its supervisor).
The created supervisor process calls
If
If no name is provided, the supervisor is not registered.
If the supervisor and its child processes are successfully
created (i.e. if all child process start functions return
If
If any child process start function fails or returns an error
tuple or an erroneous value, the supervisor will first terminate
all already started child processes with reason
Dynamically adds a child specification to the supervisor
In the case of 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
In the case of 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
Tells the 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
See
Tells the supervisor
See
If successful, the function returns
Tells the supervisor
Note that for temporary children, the child specification is automatically deleted when the child terminates; thus it is not possible to restart such children.
See
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
Returns a newly created list with information about all child
specifications and child processes belonging to
the supervisor
Note that calling this function when supervising a large number of children under low memory conditions can cause an out of memory exception.
See
The information given for each child specification/process is:
Returns a property list (see
See
This function takes a list of child specification as argument
and returns
Returns the child specification map for the child identified
by
See
The following functions must be exported from a
Whenever a supervisor is started using
Note that when the restart strategy is
The function may also return
Note that this function might also be called as a part of a
code upgrade procedure. For this reason, the function should
not have any side effects. See