From 68d53c01b0b8e9a007a6a30158c19e34b2d2a34e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= A behaviour module for implementing a supervisor, a process which
+ 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 - pre-defined set of functions.
-Unless otherwise stated, all functions in this module will fail + 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 given.
+ are specified.The supervisor is responsible for starting, stopping and +
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 + 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 properties of a supervisor are defined by the supervisor - flags. This is the type definition for the supervisor flags: -
-sup_flags() = #{strategy => strategy(), % optional ++ +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
+ period => pos_integer()} % optionalstrategy key in the - above map: -
A supervisor can have one of the following restart strategies
+ specified with the
The functions
Functions
+
The function
Function
Because a
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 the
This is the type definition of a child specification:
-child_spec() = #{id => child_id(), % mandatory +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
+ but the map is preferred. +child_spec() , - but the map is preferred. -
id is used to identify the child specification internally by the supervisor.The
-id key is mandatory.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.
+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.
start defines the function call used to start the @@ -154,84 +168,86 @@ tuple{M,F,A} used asapply(M,F,A) .The start function must create and link to the child process, and must return
{ok,Child} or -{ok,Child,Info} whereChild is the pid of - the child process andInfo an arbitrary term which is +{ok,Child,Info} , whereChild is the pid of + the child process andInfo 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 will be kept by the supervisor - (unless it is a temporary child) but the non-existing child - process will be ignored.If something goes wrong, the function may also return an + 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} .Note that the
-start_link functions of the different - behaviour modules fulfill the above requirements.The
+start key is mandatory.Notice that the
+start_link functions of the different + behavior modules fulfill the above requirements.The
start key is mandatory.-
restart defines when a terminated child process - shall be restarted. Apermanent child process will - always be restarted, atemporary child process will - never be restarted (even when the supervisor's restart strategy + must be restarted. Apermanent child process is + always restarted. Atemporary child process is + never restarted (even when the supervisor's restart strategy isrest_for_one orone_for_all and a sibling's - death causes the temporary process to be terminated) and a -transient child process will be restarted only if - it terminates abnormally, i.e. with another exit reason - thannormal ,shutdown or{shutdown,Term} .The
+ death causes the temporary process to be terminated). + Arestart key is optional. If it is not given, the - default valuepermanent will be used.transient child process is restarted only if + it terminates abnormally, that is, with another exit reason + thannormal ,shutdown , or{shutdown,Term} . +The
restart key is optional. If it is not specified, + it defaults topermanent .- -
shutdown defines how a child process shall be - terminated.brutal_kill means the child process will - be unconditionally terminated usingexit(Child,kill) . - An integer timeout value means that the supervisor will tell +
shutdown defines how a child process must be + terminated.brutal_kill means that the child process + is unconditionally terminated usingexit(Child,kill) . + An integer time-out value means that the supervisor tells the child process to terminate by callingexit(Child,shutdown) and then wait for an exit signal - with reasonshutdown back from the child process. If - no exit signal is received within the specified number of milliseconds, + with reasonshutdown back from the child process. If no + exit signal is received within the specified number of milliseconds, the child process is unconditionally terminated usingexit(Child,kill) .If the child process is another supervisor, the shutdown time - should be set to
infinity to give the subtree ample + is to be set toinfinity to give the subtree ample time to shut down. It is also allowed to set it toinfinity , if the child process is a worker.- Be careful when setting the shutdown time to -
+infinity when the child process is a worker. Because, in this - situation, the termination of the supervision tree depends on the - child process, it must be implemented in a safe way and its cleanup - procedure must always return.infinity when the child process is a worker. Because, in this + situation, the termination of the supervision tree depends on the + child process, it must be implemented in a safe way and its cleanup + procedure must always return.Note that all child processes implemented using the standard - OTP behaviour modules automatically adhere to the shutdown +
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 given, - the default value5000 will be used if the child is - of typeworker ; andinfinity will be used if - the child is of typesupervisor .The
shutdown key is optional. If it is not specified, + it defaults to5000 if the child is + of typeworker and it defaults toinfinity if + the child is of typesupervisor .-
type specifies if the child process is a supervisor or a worker.The
+type key is optional. If it is not given, the - default valueworker will be used.The
type key is optional. If it is not specified, + it defaults toworker .-
modules is used by the release handler during code replacement to determine which processes are using a certain module. As a rule of thumb, if the child process is asupervisor ,gen_server , -gen_fsm orgen_statem - this should be a list with one element[Module] , - whereModule is the callback module. If the child - process is an event manager (gen_event ) with a - dynamic set of callback modules, the valuedynamic - shall be used. See OTP Design Principles for more - information about release handling.The
+modules key is optional. If it is not given, it - defaults to[M] , whereM comes from the - child's start{M,F,A} gen_statem , orgen_fsm , + this is to be a list with one element[Module] , + whereModule is the callback module. If the child + process is an event manager (gen_event ) with a + dynamic set of callback modules, valuedynamic + must be used. For more information about release handling, see ++ Release Handling + in OTP Design Principles. +The
modules key is optional. If it is not specified, it + defaults to[M] , whereM comes from the + child's start{M,F,A} .Internally, the supervisor also keeps track of the pid @@ -240,6 +256,7 @@
The tuple format is kept for backwards compatibility
- only. A map is preferred; see more details
-
The tuple format is kept for backward compatibility
+ only. A map is preferred; see more details
+
The value
Value
The tuple format is kept for backwards compatibility
- only. A map is preferred; see more details
-
The tuple format is kept for backward 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
Takes a list of child specification as argument
+ and returns
Dynamically adds a child specification to the supervisor
-
Returns a property list (see
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
For a description of
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
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 the supervisor
Tells 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
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
-
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:
+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
Returns a property list (see
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
See
This function takes a list of child specification as argument
- and returns
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 the child specification map for the child identified
- by
See
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 functions must be exported from a +
The following function must be exported from a
Whenever a supervisor is started using
-
Note that when the restart strategy is
+ must start and monitor. See the discussion in section
+
Notice 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
-
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
+