Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Elixir Cookbook

You're reading from   Elixir Cookbook Unleash the full power of programming in Elixir with over 60 incredibly effective recipes

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher Packt
ISBN-13 9781784397517
Length 236 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Paulo Pereira Paulo Pereira
Author Profile Icon Paulo Pereira
Paulo Pereira
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Command Line FREE CHAPTER 2. Data Types and Structures 3. Strings and Binaries 4. Modules and Functions 5. Processes and Nodes 6. OTP – Open Telecom Platform 7. Cowboy and Phoenix 8. Interactions A. Installation and Further Reading Index

Generating a supervised application

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.

How to do it…

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:

How to do it…

How it works…

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.

See also

You have been reading a chapter from
Elixir Cookbook
Published in: Feb 2015
Publisher: Packt
ISBN-13: 9781784397517
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime