Application

application file

applications are defined with an application file named 'APP.app' where APP is the application name, usually in underscore_case. the application file must reside in the same ebin directory as the compiled modules of the application

example of 'myapp.app' file:

  {
    application, myapp,
      [  
        {registered, []},
        {description, "myapp"},
        {vsn,"0.0.1"},
        {modules, ['Elixir.Mytask','Elixir.Myagent']},
        {applications, [kernel,stdlib,elixir]},
        {mod, {'Elixir.Myapp',[]}}
      ]
  }

'mix' is responsible for compiling your source code and generating your application .app file 'mix' is also responsible for configuring, starting and stopping your application and its dependencies you can learn more about Mix generation of .app files by typing

$> mix help compile.app

environment

you can define the default environment values for your application

assuming you are inside a Mix project, you can edit the application function in the mix.exs file to the following:

  def application do
    [env: [hello: "world"]]
  end

by starting your application with

      $> iex -S mix

you can access the default value:

     iex> Application.get_env(:APP_NAME, :hello)
     #=> "world"

module

the first step is to define the module in the application definition in the mix.exs file:

         def application
         do
           [mod: {MyApp, []}]
         end

the MyApp module should provide an application callback start/2:

        defmodule MyApp
        do

          use Application

          def start(_type, _args) 
          do
            MyApp.Supervisor.start_link()
          end

        end

start/2 typically returns {:ok, pid} or {:ok, pid, state} where pid identifies the supervision tree and state is the application state. args is the second element of the tuple given to the :mod option in mix.exs file

the type argument passed to start/2 is usually :normal unless in a distributed setup where application takeovers and failovers are configured

types

app, key :: atom

start_type :: :permanent | :transient | :temporary

value :: term

functions

load(app) -> :ok | {:error, term}

  in order to be loaded, an .app file must be in the load paths
  all :included_applications will also be loaded
  this function does not start application nor loads its modules,
  but it does load its environment

start(app, start_type) -> :ok | {:error, term}

   if the app is not loaded, the application will first be loaded using load/1
   any included application, defined in the :included_applications key of
   the .app file will also be loaded,
   but they won’t be started

   all applications listed in the :applications key must be explicitly
   started before this application is
   if not, {:error, {:not_started, app}} is returned, where app is the name
   of the missing application

   the type argument specifies the type of the application:
     * :permanent - if app terminates, all other applications and the entire
     node are also terminated
     * :transient - if app terminates with :normal reason, it is reported but
           no other applications are terminated
        if a transient application terminates abnormally, all
        other applications and
      the entire node are also terminated
     * :temporary - if app terminates, it is reported but no other applications
     are terminated (the default)

   the :transient type is of little practical use, since when a supervision
   tree terminates, the reason is set
   to :shutdown, not :normal


stop(app) -> :ok | {:error, term}


unload(app) -> :ok | {:error, term}

   it will also unload all :included_applications



get_all_env(app) :: [{key, value}]

get_env(app, key, value) :: value

   if the configuration parameter does not exist, the function returns the
   default value


fetch_env(app, key) :: {:ok, value} | :error

   if the configuration parameter does not exist, the function returns :error

fetch_env!(app, key) :: value | no_return

   if the configuration parameter does not exist, raises ArgumentError


put_env(app, key, value, timeout: timeout, persistent: boolean) :: :ok


delete_env(app, key, timeout: timeout, persistent: boolean) :: :ok



app_dir(app) :: String.t

   Gets the directory for app


app_dir(app, String.t) :: String.t

   Returns the given path inside app_dir/1


ensure_all_started(app, start_type) ::
  {:ok, [app]} |
  {:error, {app, term}}

   Ensures the given app and its applications are started

   Same as start/2 but also starts the applications listed under :applications
   in the .app file in
   case they were not previously started


ensure_started(app, start_type) ::
  :ok |
  {:error, term}

   Ensures the given app is started

   Same as start/2 but returns :ok if the application was already started. This
   is useful in
   scripts and in test setup, where test applications need to be explicitly
   started:

      :ok = Application.ensure_started(:my_test_dep)


format_error(any) :: String.t

   Formats the error reason returned by start/2, ensure_started/2, stop/1,
   load/1 and unload/1,
   returns a string


get_application(atom) :: atom | nil

   Get the application for the given module

   The application is located by analyzing the spec of all loaded
   applications. Returns nil if the
   module is not listed in any application spec


loaded_applications :: [tuple]

   Returns a list with information about the applications which have been
   loaded


started_applications(timeout) :: [tuple]

   Returns a list with information about the applications which are currently
   running

Options

     * :timeout - the timeout for the change (defaults to 5000ms)
     * :persistent - persists the given value on application load and reloads

   If put_env/4 is called before the application is loaded, the application
   environment values
   specified in the .app file will override the ones previously set

   The persistent option can be set to true when there is a need to guarantee
   parameters set with
   this function will not be overridden by the ones defined in the application
   resource file on
   load. This means persistent values will stick after the application is
   loaded and also on
   application reload



spec(app) :: [{key, value}] | nil

   Returns the spec for app

   The following keys are returned:
       :description
       :id
       :vsn
       :modules
       :maxP
       :maxT
       :registered
       :included_applications
       :applications
       :mod
       :start_phases

   Note the environment is not returned as it can be accessed via
   fetch_env/2. Returns nil if the
   application is not loaded



spec(app, key) :: value | nil

   Returns the value for key in app‘s specification

   See spec/1 for the supported keys. If the given specification parameter
   does not exist, this
   function will raise. Returns nil if the application is not loaded