Elixir 1.6

Chapter 21 

OTP: Applications

Application

In the OTP world an application is a bundle of code that comes with a descriptor

That descriptor tells the runtime what
dependencies the code has,
what global names it registers,
and so on. In fact, an OTP application is more like a dynamic link library or a shared object than a conventional application.

It might help to see the word application and think of it as a component or service.

The Application Specification File

Every now and then mix will talk about a file called name.app, where name is your application’s name.

 

This file is called an application specification and is used to define your application to the runtime environment.

 

When you run your application this file is consulted to get things loaded.

Sequence Program already OTP App

Our sequence program from last chapter is already an OTP application

def application do
​ [
​   mod: { Sequence.Application, [] },
​   extra_applications: [:logger],
​ ]
​end

This says that the top-level module of our application is called Sequence. OTP assumes this module will implement a start function, and it will pass that function an empty list as a parameter.

/mix.exs

Sequence OTP Appliction

Let's change the default value from mix.exs

def application do
​ [
​   mod: { Sequence.Application, 456 },
​   extra_applications: [:logger],
​ ]
​end

The mod: option tells OTP the module that is the main entry point for our app. OTP will call this modules start function.

Sequence OTP Appliction

defmodule Sequence.Application do
  @moduledoc false

  use Application

  def start(_type, initial_number) do
    children = [
      { Sequence.Stash,  initial_number},
      { Sequence.Server, nil},
    ]

    opts = [strategy: :rest_for_one, name: Sequence.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Modify Sequence module like this:

Sequence OTP Appliction

$ ​​iex​​ ​​-S​​ ​​mix​
​Compiling 5 files (.ex)
​Generated sequence app
​​iex>​ Sequence.Server.next_number
​456

Sequence OTP Appliction

Let's add another option to application.

defmodule Sequence.MixProject do
  use Mix.Project

  def application do
    [
      mod: {
        Sequence.Application, 456
      },
      registered: [
        Sequence.Server,
      ],
      extra_applications: [:logger],
    ]
  end
end

The registered: option lists the names that our application will register. This ensures each name is unique across all loaded applications in a node or cluster.

Sequence OTP Appliction

We can also set up the env:

# Configuration for the OTP application
def application do
  [
    mod: { Sequence, [] },
    env: [initial_number: 456],
    registered: [ Sequence.Server ]
  ]
end

# Application.get_env can retrieve values anywhere in the code
defmodule Sequence do
  use Application
  def start(_type, _args) do
    Sequence.Supervisor.start_link(
      Application.get_env(:sequence, :initial_number)
    )
  end
end

Supervision Is the Basis of Reliability

We can also set up the env:

# Configuration for the OTP application
def application do
  [
    mod: { Sequence, [] },
    env: [initial_number: 456],
    registered: [ Sequence.Server ]
  ]
end

defmodule Sequence do
  use Application
  def start(_type, _args) do
    Sequence.Supervisor.start_link(
      Application.get_env(:sequence, :initial_number)
    )
  end
end

Thank You

Programming Elixir 1.6 Chapter 21

By Dustin McCraw

Programming Elixir 1.6 Chapter 21

  • 927