Skip to content

Commit 9f61cbc

Browse files
author
José Valim
committed
Add more headings to Application docs
1 parent 0e406e5 commit 9f61cbc

File tree

1 file changed

+68
-63
lines changed

1 file changed

+68
-63
lines changed

lib/elixir/lib/application.ex

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,86 +19,36 @@ defmodule Application do
1919
system. Instead, you start one or more applications, each with their own
2020
initialization and termination logic.
2121
22+
Applications also provide an "application environment", which provides one
23+
mechanism for configuring long running applications. We will learn more about
24+
the tooling, start and shutdown and the application environment in the next
25+
sections.
26+
27+
## Start and shutdown
28+
2229
Starting an application is done via the "application module callback", which
2330
is a module that defines the `start/2` function. The `start/2` function should
2431
then start a supervisor, which is often called as the top-level supervisor, since
2532
it sits at the root of a potentially long supervision tree. When the system is
2633
shutting down, all applications shut down their top-level supervisor, which
2734
terminates children in the opposite order they are started.
2835
29-
We have mentioned the Mix build tool is responsible for compiling applications,
30-
but it is also capable of running applications. For example, `mix test`
31-
automatically starts your application dependencies and your application itself
32-
before your test runs. `mix run --no-halt` also boots your current project and
33-
can be used to start a long running system. See `mix help run`.
34-
35-
Developers can also use tools like [Distillery](https://github.com/bitwalker/distillery)
36-
that build **releases**. Releases are able to package all of your source code
37-
as well as the Erlang VM into a single directory. Releases also give you explicit
38-
control over how each application is started and in which order. They also provide
39-
a more streamlined mechanism for starting and stopping systems, debugging, logging,
40-
as well as system monitoring.
41-
42-
Finally, Elixir provides tools such as escripts and archives, which are
43-
different mechanisms for packaging your application. Those are typically used
44-
when tools must be shared between developers and not as deployment options.
45-
See `mix help archive.build` and `mix help escript.build` for more detail.
46-
4736
Shutting down a live system cleanly can be done by calling `System.stop/1`.
4837
It will shut down all applications in the opposite order they are started.
4938
Each application will then shutdown its top-level supervisor, if one is
50-
available, which then shuts down its children.
39+
available, [which then shuts down its children](Supervisor.html#module-start-and-shutdown).
5140
5241
From Erlang/OTP 19.1, a SIGTERM from the operating system will automatically
5342
translate to `System.stop/0`. Erlang/OTP 20 gives user more explicit control
5443
over OS signals via the `:os.set_signal/2` function.
5544
56-
Applications also provide an "application environment", which is how
57-
applications are configured. The application environment can either be set
58-
statically, via a configuration file, or dynamically via `put_env/3` and
59-
friends.
60-
61-
Over the next sections, we will cover the "application environment" and
62-
the "application module callback" in more detail.
63-
64-
## Application environment
65-
66-
Once an application is started, OTP provides an application environment
67-
that can be used to configure the application.
68-
69-
Assuming you are inside a Mix project, you can edit the `application/0`
70-
function in the `mix.exs` file to the following:
71-
72-
def application do
73-
[env: [hello: :world]]
74-
end
75-
76-
In the application function, we can define the default environment values
77-
for our application. By starting your application with `iex -S mix`, you
78-
can access the default value:
79-
80-
Application.get_env(:APP_NAME, :hello)
81-
#=> :world
82-
83-
Applications and dependencies in Mix projects are typically configured
84-
via the `config/config.exs` file. For example, someone using your
85-
application can configure the `:hello` key as follows:
86-
87-
config :APP_NAME, hello: :brand_new_world
88-
89-
It is also possible to configure applications dynamically via `put_env/3`.
90-
91-
Keep in mind that each application is responsible for its environment.
92-
Do not use the functions in this module for directly accessing or modifying
93-
the environment of other applications (as it may lead to inconsistent
94-
data in the application environment).
45+
### Application module callback
9546
96-
## Application module callback
47+
An application may start and stop a supervision tree when it boots via
48+
the application module callback.
9749
98-
Often times, an application defines a supervision tree that must be started
99-
and stopped when the application starts and stops. For such, we need to
100-
define an application module callback. The first step is to define the
101-
module callback in the application definition in the `mix.exs` file:
50+
The first step is to pass the module callback in the application definition
51+
in the `mix.exs` file:
10252
10353
def application do
10454
[mod: {MyApp, []}]
@@ -139,6 +89,61 @@ defmodule Application do
13989
there is no module with application callbacks such as `start/2` and
14090
`stop/1`, the application can be started and stopped the same way as an
14191
application with a supervision tree.
92+
93+
## Tooling
94+
95+
The Mix build tool can also be used to start your applications. For example,
96+
`mix test` automatically starts your application dependencies and your application
97+
itself before your test runs. `mix run --no-halt` boots your current project and
98+
can be used to start a long running system. See `mix help run`.
99+
100+
Developers can also use tools like [Distillery](https://github.com/bitwalker/distillery)
101+
that build **releases**. Releases are able to package all of your source code
102+
as well as the Erlang VM into a single directory. Releases also give you explicit
103+
control over how each application is started and in which order. They also provide
104+
a more streamlined mechanism for starting and stopping systems, debugging, logging,
105+
as well as system monitoring.
106+
107+
Finally, Elixir provides tools such as escripts and archives, which are
108+
different mechanisms for packaging your application. Those are typically used
109+
when tools must be shared between developers and not as deployment options.
110+
See `mix help archive.build` and `mix help escript.build` for more detail.
111+
112+
## Application environment
113+
114+
Once an application is started, OTP provides an application environment
115+
that can be used to configure the application.
116+
117+
Assuming you are inside a Mix project, you can edit the `application/0`
118+
function in the `mix.exs` file to the following:
119+
120+
def application do
121+
[env: [hello: :world]]
122+
end
123+
124+
In the application function, we can define the default environment values
125+
for our application. By starting your application with `iex -S mix`, you
126+
can access the default value:
127+
128+
Application.get_env(:APP_NAME, :hello)
129+
#=> :world
130+
131+
Applications and dependencies in Mix projects are typically configured
132+
via the `config/config.exs` file. For example, someone using your
133+
application can configure the `:hello` key as follows:
134+
135+
config :APP_NAME, hello: :brand_new_world
136+
137+
Keep in mind configuration files are only useful to configure static
138+
values. For example, if you need to configure your applications based
139+
on the system environment, the file system or on database entries,
140+
then those configurations are better placed at runtime. For example,
141+
one may configure applications dynamically via `put_env/3`.
142+
143+
Keep in mind that each application is responsible for its environment.
144+
Do not use the functions in this module for directly accessing or modifying
145+
the environment of other applications (as it may lead to inconsistent
146+
data in the application environment).
142147
"""
143148

144149
@doc """

0 commit comments

Comments
 (0)