
Elixir Cookbook
By :

An application may be generated with a supervision tree to monitor processes. The supervision tree must be started and stopped with the application, and to do so, an application module callback must also be implemented. Mix provides a simple way to generate this type of application.
To generate an application with a supervision tree and an application module callback, we run mix new supervised_app –-sup
in the command line. This is shown in the following screenshot:
When mix new task
is invoked with the –-sup
option, although the generated application appears to be identical to the application created in the Creating a simple application recipe, a few things change, which are as follows:
supervised_app/mix.exs
def application do
[applications: [:logger],
mod: {SupervisedApp, []}]
end
An application module callback is added like this:
supervised_app/lib/supervised_app.ex defmodule SupervisedApp do use Application def start(_type, _args) do import Supervisor.Spec, warn: false children = [ # Define workers and child supervisors to be supervised # worker(SupervisedApp.Worker, [arg1, arg2, arg3]) ] opts = [strategy: :one_for_one, name: SupervisedApp.Supervisor] Supervisor.start_link(children, opts) end end
The Application
module behavior is declared, and a start
function must be defined to comply with this behavior. Inside the start
function, a list of children (usually worker processes) is declared, and so are the supervision options (opts
). The supervisor is then started, passing the list of processes to be supervised and the options.
Application
module can be accessed at http://elixir-lang.org/docs/stable/elixir/Application.html.Supervisor
module is available at http://elixir-lang.org/docs/stable/elixir/Supervisor.html.Change the font size
Change margin width
Change background colour