OTP applications

An OTP application in Elixir is a package of modules with a supervision tree that starts at boot. The mix.exs file names the application callback, the callback starts the top supervisor, and every child of the tree is alive before the first request arrives. This is the layer above the supervisor: a release ships applications, and an application is a tree with a name and a version.

mix new --sup

The --sup flag makes mix new write the application callback and a supervisor ready for children:

mix new demo --sup

* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/demo.ex
* creating lib/demo/application.ex
* creating test
* creating test/test_helper.exs
* creating test/demo_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd demo
    mix test

Run "mix help" for more commands.

The tree the application starts

The generated application.ex starts an empty supervisor. A child is one line in its list, and the module below is a counter that the application owns:

demo/lib/demo/counter.ex

defmodule Demo.Counter do @moduledoc false use Agent def start_link(_opts), do: Agent.start_link(fn -> 0 end, name: __MODULE__) def bump, do: Agent.update(__MODULE__, fn n -> n + 1 end) def value, do: Agent.get(__MODULE__, fn n -> n end) end

The application callback puts that module in the list of children. The name of the supervisor is the name of the application plus Supervisor, so a second start of the same application fails instead of building a second tree:

demo/lib/demo/application.ex

defmodule Demo.Application do @moduledoc false use Application @impl true def start(_type, _args) do children = [Demo.Counter] Supervisor.start_link(children, strategy: :one_for_one, name: Demo.Supervisor) end end

application/0 in mix.exs

The application/0 function of mix.exs names the callback with mod: and lists the applications that must start first. The block is the function only; the rest of the file is generated by mix new and is not shown:

# in mix.exs def application do [ extra_applications: [:logger], mod: {Demo.Application, []} ] end

mod: is what makes the application start a tree. Without it the application is a bag of modules that other applications can call, and no callback runs at boot. The dependencies lesson shows the other key of the same function, extra_applications, and the packages that are not run here.

The application that really starts

mix compile builds the project and mix run starts the application before it runs the expression. The version and the callback come from the application specification that Mix wrote:

cd demo

mix compile

Compiling 3 files (.ex)
Generated demo app

mix run -e 'IO.puts(to_string(Application.spec(:demo, :vsn)))'

0.1.0

mix run -e 'IO.inspect(Application.spec(:demo, :mod))'

{Demo.Application, []}

The children are alive while the expression runs, which is how the application answers:

mix run -e 'Demo.Counter.bump(); IO.puts(Demo.Counter.value())'

1

--no-start loads the code without starting the tree. The application is in the list of the loaded ones and not in the list of the started ones, and the last command shows the difference:

mix run -e 'IO.puts(Enum.any?(Application.started_applications(), fn {app, _, _} -> app == :demo end))'

true

mix run --no-start -e 'IO.puts(Enum.any?(Application.started_applications(), fn {app, _, _} -> app == :demo end))'

false

The mistake: elixir instead of mix run

elixir does not know a Mix project: it does not add _build to the load path and does not start the application. A call to a module of the project therefore fails with UndefinedFunctionError, even after a successful build:

elixir -e 'Demo.Counter.value()'

** (UndefinedFunctionError) function Demo.Counter.value/0 is undefined (module Demo.Counter is not available)
    Demo.Counter.value()
    nofile:1: (file)
    (stdlib 6.2.2.5) erl_eval.erl:919: :erl_eval.do_apply/7
    (elixir 1.19.7) lib/code.ex:629: Code.validated_eval_string/3

The module was compiled; the plain interpreter did not load it. Use mix run for a project, elixir -pa _build/dev/lib/demo/ebin when a bare script needs the code, and elixir file.exs for a script that stands alone.

The commands on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the mod: key of application/0 name?

    Answer

    The callback module that starts the supervision tree

    mod: {Demo.Application, []} names the module with start/2, which starts the tree.

  2. What does mix new demo --sup add to a project?

    Answer

    The application callback and a supervisor

    The flag writes application.ex with start/2 and a one_for_one supervisor.

  3. Why does an application need a supervisor at the top?

    Answer

    So the children start at boot and are restarted when they fail

    The top supervisor starts the tree and owns the restarts for its lifetime.

  4. What does mix run --no-start do?

    Answer

    It loads the project without starting its application

    The code is available, but application/0 is not started, so the tree does not run.

  5. Which call reads the callback module of an application from its specification?

    Answer

    Application.spec(:demo, :mod)

    The specification of the application keeps mod together with its start arguments.

  6. What does Application.spec(:demo, :vsn) return for a project generated by mix new?

    Answer

    The charlist ~c"0.1.0"

    The version is a charlist in the application spec; to_string turns it into a binary.

  7. What does mix run --no-start change about Application.started_applications/0?

    Answer

    The project application is absent from the started list

    The code is loaded but application/0 is not started, so the project is not in the started list.

  8. What happens when a second start of the same application runs?

    Answer

    The start fails because the supervisor name is taken

    The top supervisor has a fixed name, so a second registration fails.

  9. What does a plain elixir -e 'Demo.Counter.value()' do after mix compile?

    Answer

    It fails, because elixir does not load the _build path or start the app

    Mix owns the load path and the start; a bare elixir call has neither.

  10. Which file names the children of the top supervisor?

    Answer

    lib/demo/application.ex

    The start/2 callback in application.ex builds the children list and starts the supervisor.

Article author: Arthur Isaev