Supervisor


start_link(module, [arg], options \\ []) -> 
      :ignore 
    | {:error, term}
    | {:ok, pid}

starts a supervisor module with the given arg. the init/1 callback will be invoked in the given module. the init/1 callback must return a supervision specification

if the init/1 callback returns :ignore, this function returns :ignore as well and the supervisor terminates with reason :normal

if the init/1 fails or returns an incorrect value, this function returns {:error, term} where term is a term with information about the error, and the supervisor terminates with reason term

the :name option can be given in order to register a supervisor name


        defmodule MyApp.Supervisor do

          use Supervisor

          def start_link do Supervisor.start_link(__MODULE__, [])  end

          def init([]) do
             ch = [ ]
             supervise(ch , strategy: :one_for_one)
          end

        end

strategies

:one_for_one
only that process is restarted
:one_for_all
all other child processes are terminated and then all child processes are restarted
:rest_for_one
the child processes after in start order, are terminated and restarted with terminaded child proccess
:simple_one_for_one
similar to :one_for_one but suits better when dynamically attaching children requires the supervisor specification to contain only one child. many functions behave slightly differently when this strategy is used

strategy :simple_one_for_one allows us to specify a worker template and supervise many children based on this template.

with this strategy, no workers are started during the supervisor initialization, and a new worker is started each time start_child/2 is called

exit reasons

the transient restart strategy for the worker does not restart the child in case it crashes with reason :normal, :shutdown or {:shutdown, term}

which exit reason should you choose when exiting my worker? there are three options:

:normal
the exit won’t be logged, there is no restart in transient mode and linked processes do not exit
:shutdown or {:shutdown, term}
the exit won’t be logged, there is no restart in transient mode and linked processes exit with the same reason unless trapping exits
any other term
the exit will be logged, there are restarts in transient mode and linked processes exit with the same reason unless trapping exits

types


  name ::  atom | {:global, term} | {:via, module, term}

  on_start_child ::  
         {:ok, child}
      |  {:ok, child, info :: term}
      |  {:error, {:already_started, child}}




   supervise([spec], strategy: strategy, max_restarts: non_neg_integer,
             max_seconds: non_neg_integer) -> {:ok, tuple}

   receives a list of children (workers or supervisors) and a set of options
   returns a tuple containing the supervisor specification



   spec


     * :id   - a name used to identify the child specification internally
     by the supervisor

     * :function - the function to invoke on the child to start it

     * :restart  - an atom that defines when a terminated child process
     should be restarted

     * :shutdown - an atom that defines how a child process should be
     terminated

     * :modules  - it should be a list with one element [module],
        where 'module' is the name of the callback module only
        if the child process is
        a Supervisor or GenServer;
        if the child process is a GenEvent, :modules should
        be :dynamic


:restart option:

     * :permanent - the child process is always restarted

     * :temporary - the child process is never restarted

     * :transient - the child process is restarted only if it terminates
     abnormally


:shutdown option:

     * :brutal_kill - the child process is unconditionally terminated using
     exit(child, :kill)

     * :infinity - if the child process is a supervisor, this is a mechanism
     to give the subtree
       enough time to shutdown; it can also be used with workers with care

     * any integer - meaning that the supervisor tells the child process
     to terminate
       by calling Process.exit(child, :shutdown) and then waits for an exit
       signal back
       if no exit signal is received within the specified time (the value
       of this option,
       in milliseconds),
       the child process is unconditionally terminated using
       Process.exit(child, :kill)







   supervisor(module, args, options \\ [])
   defines the given module as a supervisor which will be started with the
   given arguments

   worker(module, args, options \\ [])
   defines the given module as a worker which will be started with the
   given arguments



       options

     * :max_restarts - the maximum amount of restarts allowed in a time
     frame. Defaults to 3
     * :max_seconds - the time frame in which :max_restarts applies. Defaults
     to 5
     * strategy :: :simple_one_for_one | :one_for_one | :one_for_all |
     :rest_for_one

   the :strategy option is required and by default a maximum of 3 restarts
   is allowed within 5 seconds

     * spec  :: {child_id, start_fun :: {module, atom, [term]}, restart,
     shutdown, type, modules}
     * type  :: :worker | :supervisor


      supervisor(module, [term],
           restart: restart,
           shutdown: shutdown,
           id: term,
           function: atom,
           modules: modules) :: spec


    [id:    module,
     function:   :start_link,
     restart:   :permanent,
     shutdown:   :infinity,
     modules:   [module]]


     worker(module, [term],
      restart: restart,
      shutdown: shutdown,
      id: term,
      function: atom,
      modules: modules) :: spec

    [id:     module,
     function:    :start_link,
     restart:    :permanent,
     shutdown:     5000,
     modules:    [module]]



start_child(supervisor, Supervisor.Spec.spec | [term]) :: on_start_child
   dynamically adds and starts a child specification to the supervisor

child_spec should be a valid child specification (unless the supervisor is
a :simple_one_for_one supervisor)

in the case of :simple_one_for_one, the child specification defined in the
supervisor will be used and instead of a child_spec, an arbitrary list of
terms is expected. The child process will then be started by appending the
given list to the existing function arguments in the child specification

if a child specification with the specified id already exists, child_spec
is discarded and the function returns an error with :already_started or
:already_present if the corresponding child process is running or not

if the child process starts, function returns {:ok, child} or {:ok, child,
info}, the child specification and pid is added to the supervisor and the
function returns the same value

if the child process starts, function returns :ignore, the child specification
is added to the supervisor, the pid is set to undefined and the function
returns {:ok, :undefined}

if the child process starts, 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



restart_child(supervisor, Supervisor.Spec.child_id) ::
  {:ok, child} |
  {:ok, child, term} |
  {:error, error} when error: :not_found | :simple_one_for_one | :running |
  :restarting | term

the child specification must exist and the corresponding child process must
not be running

note that for temporary children, the child specification is automatically
deleted when the child terminates, and thus it is not possible to restart
such children

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}

this function may error with an appropriate error tuple if the child_id is
not found, or if the current process is running or being restarted

if the child process start function returns an error tuple or an erroneous
value, or if it fails, the function returns {:error, error}

this operation is not supported by simple_one_for_one supervisors



delete_child(supervisor, Supervisor.Spec.child_id) ::
  :ok |
  {:error, error} when error: :not_found | :simple_one_for_one | :running
  | :restarting

The corresponding child process must not be running, use terminate_child/2
to terminate it

If successful, the function returns :ok. This function may error with an
appropriate error tuple if the child_id is not found, or if the current
process is running or being restarted

This operation is not supported by simple_one_for_one supervisors



terminate_child(supervisor, pid | Supervisor.Spec.child_id) ::
  :ok | {:error, :not_found | :simple_one_for_one}

If the supervisor is not a simple_one_for_one, the child id is expected and
the process, if there is one, is terminated; the child specification is kept
unless the child is temporary

In case of a simple_one_for_one supervisor, a pid is expected. If the child
specification identifier is given instead of a pid, the function will return
{:error, :simple_one_for_one}

A non-temporary child process may 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 successful, the function returns :ok. If there is no child specification
or pid, the function returns {:error, :not_found}




which_children(supervisor) ::
  [{Supervisor.Spec.child_id | :undefined, child | :restarting,
  Supervisor.Spec.type, Supervisor.Spec.modules}]

returns a list with information about all children. calling this function when
supervising a large number of children under low memory conditions can cause
an out of memory exception. the function returns a list of tuples containing:





      stop(supervisor, reason :: term, timeout) :: :ok

It returns :ok if the supervisor terminates with the given reason, if it
terminates with another reason, the call will exit

if the reason is any other than :normal, :shutdown or {:shutdown, _}, an
error report will be logged