A module for working with applications and defining application callbacks.
In Elixir (actually, in Erlang/OTP), an application is a component implementing some specific functionality, that can be started and stopped as a unit, and which can be re-used in other systems as well.
Applications are defined with an application file named APP.app
where
APP
is the APP name, usually in underscore_case
convention. The
application file must reside in the same ebin
directory as the
application's modules bytecode.
In Elixir, Mix is responsible for compiling your source code and
generating your application .app
file. Furthermore, Mix is also
responsible for configuring, starting and stopping your application
and its dependencies. For this reason, this documentation will focus
on the remaining aspects of your application: the application environment,
and the application callback module.
You can learn more about Mix compilation of .app
files by typing
mix help compile.app
.
Application environment
Once an application is started, OTP provides an application environment that can be used to configure applications.
Assuming you are inside a Mix project, you can edit your application
function in the mix.exs
file to the following:
def application do
[env: [hello: :world]]
end
In the application function, we can define the default environment values
for our application. By starting your application with iex -S mix
, you
can access the default value:
Application.get_env(:APP_NAME, :hello)
#=> {:ok, :hello}
It is also possible to put and delete values from the application value, including new values that are not defined in the environment file (although those should be avoided).
In the future, we plan to support configuration files which allows developers to configure the environment of their dependencies.
Keep in mind that each application is responsible for its environment. Do not use the functions in this module for directly access or modify the environment of other application (as it may lead to inconsistent data in the application environment).
Application module callback
Often times, an application defines a supervision tree that must be started
and stopped when the application starts and stops. For such, we need to
define an application module callback. The first step is to define the
module callback in the application definition in the mix.exs
file:
def application do
[mod: {MyApp, []}]
end
Our application now requires the MyApp
module to provide an application
callback. This can be done by invoking use Application
in that module
and defining a start/2
callback, for example:
defmodule MyApp do
use Application
def start(_type, _args) do
MyApp.Supervisor.start_link()
end
end
start/2
most commonly returns {:ok, pid}
or {:ok, pid, state}
where
pid
identifies the supervision tree and the state is the application state.
args
is second element of the tuple given to the :mod
option.
The type
passed into start/2
is usually :normal
unless in a distributed
setup where applications takeover and failovers are configured. This particular
aspect of applications can be read with more detail in the OTP documentation:
- http://www.erlang.org/doc/man/application.html
- http://www.erlang.org/doc/design_principles/applications.html
A developer may also implement the stop/1
callback (automatically defined
by use Application
) which does any application cleanup. It receives the
application state and can return any value. Notice that shutting down the
supervisor is automatically handled by the VM;