concept Supervisor in category elixir

appears as: Supervisor, Supervisor, The Supervisor
Elixir in Action

This is an excerpt from Manning's book Elixir in Action.

Elixir provides its own wrappers for the most frequently used behaviours via the modules GenServer, Supervisor, and Application. This book focuses on these behaviours. The GenServer behaviour receives detailed treatment in this chapter and chapter 7, Supervisor is discussed in chapters 8 and 9, and Application is presented in chapter 11.

The remaining behaviours, although useful, are used less often and won’t be discussed in this book. Once you get a grip on GenServer and Supervisor, you should be able to research other behaviours on your own and use them when the need arises. You can find more about gen_event and gen_statem in the Erlang documentation (http://erlang.org/doc/design_principles/des_princ.html).

In order to do that, you need at least one supervisor process in the system. In Elixir, this can be done using the Supervisor module (https://hexdocs.pm/elixir/Supervisor.html). By invoking Supervisor.start_link/2, you can start the supervisor process, which then works as follows:

defmodule Todo.System do
 use Supervisor    #1  

  def start_link do
 Supervisor.start_link(__MODULE__, nil)    #2  
  end

 def init(_) do    #3  
 Supervisor.init([Todo.Cache], strategy: :one_for_one)    #3  
 end    #3  
end

#1   Includes some common boilerplate
#2   Starts the supervisor with Todo.System as the callback module
#3   Implements the required callback function

As with GenServer, you start with use Supervisor to get some common boilerplate in your module.

The Little Elixir & OTP Guidebook

This is an excerpt from Manning's book The Little Elixir & OTP Guidebook.

In this book, you’ll work with two of the most-used behaviors: GenServer and Supervisor. Once you’re comfortable with them, learning to use other behaviors will be straightforward. You could roll your own Supervisor behavior, but there’s no good reason to do so 99.999999999% of the time. The implementers have thought long and hard about the features that need to be included in most client-server programs, and they’ve also accounted for concurrency errors and all sorts of edge cases.

After teasing you in the previous chapter, in this chapter I’ll finally show you how to use the real thing: the OTP Supervisor behavior. The sole responsibility of a Supervisor is to observe an attached child process, check to see if it goes down, and take some action if that happens.

The OTP version offers a few more bells and whistles than your previous implementation of a Supervisor. Take restart strategies, for example, which dictate how a Supervisor should restart the children if something goes wrong. Supervisor also offers options for limiting the number of restarts within a specific timeframe; this is especially useful for preventing infinite restarts.

Listing 6.2. Initializing the Supervisor (lib/pooly/worker_supervisor.ex)
defmodule Pooly.WorkerSupervisor do
  #############
  # Callbacks #
  #############
  def init({m,f,a} = x) do
    worker_opts = [restart: :permanent,                       #1
                  function: f]                                #2
    children = [worker(m, a, worker_opts)]                    #3
    opts     = [strategy: :simple_one_for_one,                #4
                max_restarts: 5,                              #4
                max_seconds: 5]                               #4
    supervise(children, opts)                                 #5
  end
end
Figure 6.10. The Supervisor replaced a killed worker with a newly spawned worker.