aboutsummaryrefslogblamecommitdiffstats
path: root/lib/stdlib/doc/src/supervisor.xml
blob: ec607d6e4ccc45bff360eeecccf5d41d75b61e9e (plain) (tree)
1
2
3
4
5
6
7





                                        
                                        







                                                                         
 



                                                                            
 







































                                                                                                                                    






















                                                                                  
                                              



                                                                       





                                                                    























































                                                                                                                                   





                                                                         








                                                                      
                                                                                 



































                                                                                                                                      



































                                                                    

          

                                         
                                                       


                                  




                                                                      
                                                                                    


                                                                    
                                                                                
                                
                                                                                   
                                                            
                                                                                

                                                                       


                                                                             




                                                                      
                                                                             

                                                                   
                                                                                    

                                                                     
                                                                                   










                                                                        
                                          
                                                                           


                                   

                                                                   

                                                                                      







                                                                            
                                                                              





                                                                      


                                                                                  
                                             
                                                                                    

                                                             
                                                                                              
                                                                
                                                                              
                                                                

                                                                                                 













                                                                        
                                              
                                                                               
            
                                                                                 

                                                              
                                                                                





                                                                     
                                                                                 






                                                                     
                                                                               
                                                        
                                                        
                                         


             
                                           
                                                                          
            

                                                                                     

                                                                      

                                                                                            
                                                                      
                                                                       

                                                                    
                                                                                   



                                       
                                            
                                                                                        
            
                                                                                     
                                                                
                                                                        
                                                                  


                                                                                            



                                                                      

                                                                                                 





                                                                        
                                                                                                



                                          
                                             
                                                                                                                              
            
                                                                        
                                                         
                                                        


                                                                     

                                                                                            



                                                                     
                                                                                



                                                       
                                                                             


                                                                           
                                                                                    

                 
                                                                                       




                 
                                             
                                                                                                                

























                                                                                
                                               
                                                                                      


                                                                        
                                                                      















                                                                               


                                                                                            


































                                                                        
<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>1996</year><year>2011</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      The contents of this file are subject to the Erlang Public License,
      Version 1.1, (the "License"); you may not use this file except in
      compliance with the License. You should have received a copy of the
      Erlang Public License along with this software. If not, it can be
      retrieved online at http://www.erlang.org/.

      Software distributed under the License is distributed on an "AS IS"
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
      the License for the specific language governing rights and limitations
      under the License.

    </legalnotice>

    <title>supervisor</title>
    <prepared></prepared>
    <docno></docno>
    <date></date>
    <rev></rev>
  </header>
  <module>supervisor</module>
  <modulesummary>Generic Supervisor Behaviour</modulesummary>
  <description>
    <p>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 <c>gen_event</c>, <c>gen_fsm</c>, or <c>gen_server</c>
      behaviours. A supervisor implemented using this module will have
      a standard set of interface functions and include functionality
      for tracing and error reporting. Supervisors are used to build an
      hierarchical process structure called a supervision tree, a
      nice way to structure a fault tolerant application. Refer to
      <em>OTP Design Principles</em> for more information.</p>
    <p>A supervisor assumes the definition of which child processes to
      supervise to be located in a callback module exporting a
      pre-defined set of functions.</p>
    <p>Unless otherwise stated, all functions in this module will fail
      if the specified supervisor does not exist or if bad arguments
      are given.</p>
  </description>

  <section>
    <title>Supervision Principles</title>
    <p>The 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.</p>
    <p>The children of a supervisor is defined as a list of <em>child specifications</em>. 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.</p>
    <p>A supervisor can have one of the following <em>restart strategies</em>:</p>
    <list type="bulleted">
      <item>
        <p><c>one_for_one</c> - if one child process terminates and
          should be restarted, only that child process is affected.</p>
      </item>
      <item>
        <p><c>one_for_all</c> - if one child process terminates and
          should be restarted, all other child processes are terminated
          and then all child processes are restarted.</p>
      </item>
      <item>
        <p><c>rest_for_one</c> - if one child process terminates and
          should be restarted, the 'rest' of the child processes --
          i.e. the child processes after the terminated child process
          in the start order -- are terminated. Then the terminated
          child process and all child processes after it are restarted.</p>
      </item>
      <item>
        <p><c>simple_one_for_one</c> - a simplified <c>one_for_one</c>
          supervisor, where all child processes are dynamically added
          instances of the same process type, i.e. running the same
          code.</p>
        <p>The functions <c>delete_child/2</c>
          and <c>restart_child/2</c> are invalid for
          <c>simple_one_for_one</c> supervisors and will return
          <c>{error,simple_one_for_one}</c> if the specified supervisor
          uses this restart strategy.</p>
        <p>The function <c>terminate_child/2</c> can be used for
          children under <c>simple_one_for_one</c> supervisors by
          giving the child's <c>pid()</c> as the second argument. If
          instead the child specification identifier is used,
          <c>terminate_child/2</c> will return
          <c>{error,simple_one_for_one}</c>.</p>
      </item>
    </list>
    <p>To prevent a supervisor from getting into an infinite loop of
      child process terminations and restarts, a <em>maximum restart frequency</em> is defined using two integer values <c>MaxR</c>
      and <c>MaxT</c>. If more than <c>MaxR</c> restarts occur within
      <c>MaxT</c> seconds, the supervisor terminates all child
      processes and then itself.
    </p>
    <marker id="child_spec"/>
    <p>This is the type definition of a child specification:</p>
    <pre>
child_spec() = {Id,StartFunc,Restart,Shutdown,Type,Modules}
 Id = term()
 StartFunc = {M,F,A}
  M = F = atom()
  A = [term()]
 Restart = permanent | transient | temporary
 Shutdown = brutal_kill | int()>=0 | infinity
 Type = worker | supervisor
 Modules = [Module] | dynamic
  Module = atom()</pre>
    <list type="bulleted">
      <item>
        <p><c>Id</c> is a name that is used to identify the child
          specification internally by the supervisor.</p>
      </item>
      <item>
        <p><c>StartFunc</c> defines the function call used to start
          the child process. It should be a module-function-arguments
          tuple <c>{M,F,A}</c> used as <c>apply(M,F,A)</c>.</p>
        <p>          <br></br>
</p>
        <p>The start function <em>must create and link to</em> the child
          process, and should return <c>{ok,Child}</c> or
          <c>{ok,Child,Info}</c> where <c>Child</c> is the pid of
          the child process and <c>Info</c> an arbitrary term which is
          ignored by the supervisor.</p>
        <p>          <br></br>
</p>
        <p>The start function can also return <c>ignore</c> if the child
          process for some reason cannot be started, in which case
          the child specification will be kept by the supervisor but
          the non-existing child process will be ignored.</p>
        <p>          <br></br>
</p>
        <p>If something goes wrong, the function may also return an
          error tuple <c>{error,Error}</c>.</p>
        <p>          <br></br>
</p>
        <p>Note that the <c>start_link</c> functions of the different
          behaviour modules fulfill the above requirements.</p>
      </item>
      <item>
        <p><c>Restart</c> defines when a terminated child process
          should be restarted. A <c>permanent</c> child process should
          always be restarted, a <c>temporary</c> child process should
          never be restarted (even when the supervisor's restart strategy
          is <c>rest_for_one</c> or <c>one_for_all</c> and a sibling's
          death causes the temporary process to be terminated) and a
          <c>transient</c> child process should be restarted only if
          it terminates abnormally, i.e. with another exit reason
          than <c>normal</c>.</p>
      </item>
      <item>
        <p><c>Shutdown</c> defines how a child process should be
          terminated. <c>brutal_kill</c> means the child process will
          be unconditionally terminated using <c>exit(Child,kill)</c>.
          An integer timeout value means that the supervisor will tell
          the child process to terminate by calling
          <c>exit(Child,shutdown)</c> and then wait for an exit signal
          with reason <c>shutdown</c> back from the child process. If
          no exit signal is received within the specified number of milliseconds,
          the child process is unconditionally terminated using
          <c>exit(Child,kill)</c>.</p>
        <p>If the child process is another supervisor, <c>Shutdown</c>
          should be set to <c>infinity</c> to give the subtree ample
          time to shutdown.</p>
        <p><em>Important note on simple-one-for-one supervisors:</em>
          The dynamically created child processes of a
          simple-one-for-one supervisor are not explicitly killed,
          regardless of shutdown strategy, but are expected to terminate
          when the supervisor does (that is, when an exit signal from
          the parent process is received).</p>
        <p>Note that all child processes implemented using the standard
          OTP behavior modules automatically adhere to the shutdown
          protocol.</p>
      </item>
      <item>
        <p><c>Type</c> specifies if the child process is a supervisor or
          a worker.</p>
      </item>
      <item>
        <p><c>Modules</c> is used by the release handler during code
          replacement to determine which processes are using a certain
          module. As a rule of thumb <c>Modules</c> should be a list
          with one element <c>[Module]</c>, where <c>Module</c> is
          the callback module, if the child process is a supervisor,
          gen_server or gen_fsm. If the child process is an event
          manager (gen_event) with a dynamic set of callback modules,
          <c>Modules</c> should be <c>dynamic</c>. See <em>OTP Design Principles</em> for more information about release handling.</p>
      </item>
      <item>
        <p>Internally, the supervisor also keeps track of the pid
          <c>Child</c> of the child process, or <c>undefined</c> if no
          pid exists.</p>
      </item>
    </list>
  </section>
  <datatypes>
    <datatype>
      <name name="child"/>
    </datatype>
    <datatype>
      <name name="child_id"/>
      <desc><p>Not a <c>pid()</c>.</p></desc>
    </datatype>
    <datatype>
      <name name="child_spec"/>
    </datatype>
    <datatype>
      <name name="mfargs"/>
      <desc><p><c>A</c> (the argument list) has the value
         <c>undefined</c> if <c>Restart</c> is <c>temporary</c>.</p>
      </desc>
    </datatype>
    <datatype>
      <name name="modules"/>
    </datatype>
    <datatype>
      <name name="restart"/>
    </datatype>
    <datatype>
      <name name="shutdown"/>
    </datatype>
    <datatype>
      <name name="strategy"/>
    </datatype>
    <datatype>
      <name name="sup_ref"/>
    </datatype>
    <datatype>
      <name name="worker"/>
    </datatype>
  </datatypes>
  <funcs>
    <func>
      <name name="start_link" arity="2"/>
      <name name="start_link" arity="3"/>
      <fsummary>Create a supervisor process.</fsummary>
      <type name="startlink_ret"/>
      <type name="startlink_err"/>
      <type name="sup_name"/>
      <desc>
        <p>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).</p>
        <p>The created supervisor process calls <c><anno>Module</anno>:init/1</c> to
          find out about restart strategy, maximum restart frequency
          and child processes. To ensure a synchronized start-up
          procedure, <c>start_link/2,3</c> does not return until
          <c><anno>Module</anno>:init/1</c> has returned and all child processes
          have been started.</p>
        <p>If <c><anno>SupName</anno>={local,Name}</c> the supervisor is registered
          locally as <c>Name</c> using <c>register/2</c>. If
          <c><anno>SupName</anno>={global,Name}</c> the supervisor is registered
          globally as <c>Name</c> using <c>global:register_name/2</c>.
          If no name is provided, the supervisor is not registered.</p>
        <p><c><anno>Module</anno></c> is the name of the callback module.</p>
        <p><c><anno>Args</anno></c> is an arbitrary term which is passed as
          the argument to <c><anno>Module</anno>:init/1</c>.</p>
        <p>If the supervisor and its child processes are successfully
          created (i.e. if all child process start functions return
          <c>{ok,Child}</c>, <c>{ok,Child,Info}</c>, or <c>ignore</c>)
          the function returns <c>{ok,Pid}</c>, where <c>Pid</c> is
          the pid of the supervisor. If there already exists a process
          with the specified <c><anno>SupName</anno></c> the function returns
          <c>{error,{already_started,Pid}}</c>, where <c>Pid</c> is
          the pid of that process.</p>
        <p>If <c><anno>Module</anno>:init/1</c> returns <c>ignore</c>, this function
          returns <c>ignore</c> as well and the supervisor terminates
          with reason <c>normal</c>.
          If <c><anno>Module</anno>:init/1</c> fails or returns an incorrect value,
          this function returns <c>{error,Term}</c> where <c>Term</c>
          is a term with information about the error, and the supervisor
          terminates with reason <c>Term</c>.</p>
        <p>If any child process start function fails or returns an error
          tuple or an erroneous value, the function returns
          <c>{error,shutdown}</c> and the supervisor terminates all
          started child processes and then itself with reason
          <c>shutdown</c>.</p>
      </desc>
    </func>
    <func>
      <name name="start_child" arity="2"/>
      <fsummary>Dynamically add a child process to a supervisor.</fsummary>
      <type name="child_spec"/>
      <type name="startchild_ret"/>
      <type name="startchild_err"/>
      <desc>
        <p>Dynamically adds a child specification to the supervisor
          <c><anno>SupRef</anno></c> which starts the corresponding child process.</p>
        <p><marker id="SupRef"><c><anno>SupRef</anno></c></marker> can be:</p>
        <list type="bulleted">
          <item>the pid,</item>
          <item><c>Name</c>, if the supervisor is locally registered,</item>
          <item><c>{Name,Node}</c>, if the supervisor is locally
           registered at another node, or</item>
          <item><c>{global,Name}</c>, if the supervisor is globally
           registered.</item>
        </list>
        <p><c><anno>ChildSpec</anno></c> should be a valid child specification
          (unless the supervisor is a <c>simple_one_for_one</c>
          supervisor, see below). The child process will be started by
          using the start function as defined in the child
          specification.</p>
        <p>If the case of a <c>simple_one_for_one</c> supervisor,
          the child specification defined in <c>Module:init/1</c> will
          be used and <c><anno>ChildSpec</anno></c> should instead be an arbitrary
          list of terms <c><anno>List</anno></c>. The child process will then be
          started by appending <c><anno>List</anno></c> to the existing start
          function arguments, i.e. by calling
          <c>apply(M, F, A++<anno>List</anno>)</c> where <c>{M,F,A}</c> is the start
          function defined in the child specification.</p>
        <p>If there already exists a child specification with
          the specified <c><anno>Id</anno></c>, <c><anno>ChildSpec</anno></c> is discarded and
          the function returns <c>{error,already_present}</c> or
          <c>{error,{already_started,<anno>Child</anno>}}</c>, depending on if
          the corresponding child process is running or not.</p>
        <p>If the child process start function returns <c>{ok,<anno>Child</anno>}</c>
          or <c>{ok,<anno>Child</anno>,<anno>Info</anno>}</c>, the child specification and pid is
          added to the supervisor and the function returns the same
          value.</p>
        <p>If the child process start function returns <c>ignore</c>,
          the child specification is added to the supervisor, the pid
          is set to <c>undefined</c> and the function returns
          <c>{ok,undefined}</c>.</p>
        <p>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 <c>{error,Error}</c> where
          <c>Error</c> is a term containing information about the error
          and child specification.</p>
      </desc>
    </func>
    <func>
      <name name="terminate_child" arity="2"/>
      <fsummary>Terminate a child process belonging to a supervisor.</fsummary>
      <desc>
        <p>Tells the supervisor <c><anno>SupRef</anno></c> to terminate the given
          child.</p>
        <p>If the supervisor is not <c>simple_one_for_one</c>,
          <c><anno>Id</anno></c> must be the child specification identifier. The
          process, if there is one, is terminated but the child
          specification is kept by the supervisor. The child process
          may later be restarted by the supervisor. The child process
          can also be restarted explicitly by calling
          <c>restart_child/2</c>. Use <c>delete_child/2</c> to remove
          the child specification.</p>
        <p>If the supervisor is <c>simple_one_for_one</c>, <c><anno>Id</anno></c>
          must be the child process' <c>pid()</c>. I the specified
          process is alive, but is not a child of the given
          supervisor, the function will return
          <c>{error,not_found}</c>. If the child specification
          identifier is given instead instead of a <c>pid()</c>, the
          function will return <c>{error,simple_one_for_one}</c>.</p>
        <p>If successful, the function returns <c>ok</c>. If there is
          no child specification with the specified <c><anno>Id</anno></c>, the
          function returns <c>{error,not_found}</c>.</p>
        <p>See <c>start_child/2</c> for a description of
          <c><anno>SupRef</anno></c>.</p>
      </desc>
    </func>
    <func>
      <name name="delete_child" arity="2"/>
      <fsummary>Delete a child specification from a supervisor.</fsummary>
      <desc>
        <p>Tells the supervisor <c><anno>SupRef</anno></c> to delete the child
          specification identified by <c><anno>Id</anno></c>. The corresponding child
          process must not be running, use <c>terminate_child/2</c> to
          terminate it.</p>
        <p>See <seealso marker="#SupRef"><c>start_child/2</c></seealso> for a description of
          <c>SupRef</c>.</p>
        <p>If successful, the function returns <c>ok</c>. If the child
          specification identified by <c><anno>Id</anno></c> exists but
          the corresponding child process is running, the function
          returns <c>{error,running}</c>. If the child specification
          identified by <c><anno>Id</anno></c> does not exist, the function returns
          <c>{error,not_found}</c>.</p>
      </desc>
    </func>
    <func>
      <name name="restart_child" arity="2"/>
      <fsummary>Restart a terminated child process belonging to a supervisor.</fsummary>
      <desc>
        <p>Tells the supervisor <c><anno>SupRef</anno></c> to restart a child process
          corresponding to the child specification identified by
          <c><anno>Id</anno></c>. The child specification must exist and
          the corresponding child process must not be running.</p>
        <p>See <seealso marker="#SupRef"><c>start_child/2</c></seealso> for a description of
          <c>SupRef</c>.</p>
        <p>If the child specification identified by <c><anno>Id</anno></c> does not
          exist, the function returns <c>{error,not_found}</c>. If
          the child specification exists but the corresponding process
          is already running, the function returns
          <c>{error,running}</c>.</p>
        <p>If the child process start function returns <c>{ok,<anno>Child</anno>}</c>
          or <c>{ok,<anno>Child</anno>,<anno>Info</anno>}</c>, the pid is added to the supervisor
          and the function returns the same value.</p>
        <p>If the child process start function returns <c>ignore</c>,
          the pid remains set to <c>undefined</c> and the function
          returns <c>{ok,undefined}</c>.</p>
        <p>If the child process start function returns an error tuple or
          an erroneous value, or if it fails, the function returns
          <c>{error,<anno>Error</anno>}</c> where <c><anno>Error</anno></c> is a term containing
          information about the error.</p>
      </desc>
    </func>
    <func>
      <name name="which_children" arity="1"/>
      <fsummary>Return information about all children specifications and child processes belonging to a supervisor.</fsummary>
      <desc>
        <p>Returns a newly created list with information about all child
          specifications and child processes belonging to
          the supervisor <c><anno>SupRef</anno></c>.</p>
        <p>Note that calling this function when supervising a large
          number of children under low memory conditions can cause an
          out of memory exception.</p>
        <p>See <seealso marker="#SupRef"><c>start_child/2</c></seealso> for a description of
          <c>SupRef</c>.</p>
        <p>The information given for each child specification/process
          is:</p>
        <list type="bulleted">
          <item>
            <p><c><anno>Id</anno></c> - as defined in the child specification or
              <c>undefined</c> in the case of a
              <c>simple_one_for_one</c> supervisor.</p>
          </item>
          <item>
            <p><c><anno>Child</anno></c> - the pid of the corresponding child
              process, or <c>undefined</c> if there is no such process.</p>
          </item>
          <item>
            <p><c><anno>Type</anno></c> - as defined in the child specification.</p>
          </item>
          <item>
            <p><c><anno>Modules</anno></c> - as defined in the child specification.</p>
          </item>
        </list>
      </desc>
    </func>
    <func>
      <name name="count_children" arity="1"/>
      <fsummary>Return counts for the number of childspecs, active children, supervisors and workers.</fsummary>
      <desc>
        <p>Returns a property list (see <c>proplists</c>) containing the
          counts for each of the following elements of the supervisor's
          child specifications and managed processes:</p>
        <list type="bulleted">
          <item>
            <p><c>specs</c> - the total count of children, dead or alive.</p>
          </item>
          <item>
            <p><c>active</c> - the count of all actively running child processes
              managed by this supervisor.</p>
          </item>
          <item>
            <p><c>supervisors</c> - the count of all children marked as
              child_type = supervisor in the spec list, whether or not the
              child process is still alive.</p>
          </item>
          <item>
            <p><c>workers</c> - the count of all children marked as
              child_type = worker in the spec list, whether or not the child
              process is still alive.</p>
          </item>
        </list>
      </desc>
    </func>
    <func>
      <name name="check_childspecs" arity="1"/>
      <fsummary>Check if children specifications are syntactically correct.</fsummary>
      <desc>
        <p>This function takes a list of child specification as argument
          and returns <c>ok</c> if all of them are syntactically
          correct, or <c>{error,<anno>Error</anno>}</c> otherwise.</p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>CALLBACK FUNCTIONS</title>
    <p>The following functions should be exported from a
      <c>supervisor</c> callback module.</p>
  </section>
  <funcs>
    <func>
      <name>Module:init(Args) -> Result</name>
      <fsummary>Return a supervisor specification.</fsummary>
      <type>
        <v>Args = term()</v>
        <v>Result = {ok,{{RestartStrategy,MaxR,MaxT},[ChildSpec]}} | ignore</v>
        <v>&nbsp;RestartStrategy = <seealso marker="#type-strategy">strategy()</seealso></v>
        <v>&nbsp;MaxR = MaxT = integer()>=0</v>
        <v>&nbsp;ChildSpec = <seealso marker="#type-child_spec">child_spec()</seealso></v>
      </type>
      <desc>
        <p>Whenever a supervisor is started using
          <c>supervisor:start_link/2,3</c>, this function is called by
          the new process to find out about restart strategy, maximum
          restart frequency and child specifications.</p>
        <p><c>Args</c> is the <c>Args</c> argument provided to the start
          function.</p>
        <p><c>RestartStrategy</c> is the restart strategy and
          <c>MaxR</c> and <c>MaxT</c> defines the maximum restart
          frequency of the supervisor. <c>[ChildSpec]</c> is a list of
          valid child specifications defining which child processes
          the supervisor should start and monitor. See the discussion
          about Supervision Principles above.</p>
        <p>Note that when the restart strategy is
          <c>simple_one_for_one</c>, the list of child specifications
          must be a list with one child specification only.
          (The <c>Id</c> is ignored). No child process is then started
          during the initialization phase, but all children are assumed
          to be started dynamically using
          <c>supervisor:start_child/2</c>.</p>
        <p>The function may also return <c>ignore</c>.</p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>SEE ALSO</title>
    <p><seealso marker="gen_event">gen_event(3)</seealso>, 
      <seealso marker="gen_fsm">gen_fsm(3)</seealso>, 
      <seealso marker="gen_server">gen_server(3)</seealso>, 
      <seealso marker="sys">sys(3)</seealso></p>
  </section>
</erlref>