Skip to content

Commit fb1253d

Browse files
committed
Merge pull request elixir-lang#516 from amarandon/fix-466
Update section on starting applications
2 parents 6aaa0e6 + c475edd commit fb1253d

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

getting-started/mix-otp/supervisor-and-application.markdown

+18-5
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,36 @@ iex> Application.start(:kv)
102102
{:error, {:already_started, :kv}}
103103
```
104104

105-
Oops, it's already started.
105+
Oops, it's already started. Mix normally starts the whole hierarchy of applications defined in our project's `mix.exs` file and it does the same for all dependencies if they depend on other applications.
106106

107107
We can pass an option to mix to ask it to not start our application. Let's give it a try by running `iex -S mix run --no-start`:
108108

109+
```elixir
110+
iex> Application.start(:kv)
111+
:ok
112+
```
113+
114+
We can stop our `:kv` application as well as the `:logger` application, which is stared by default with Elixir:
115+
116+
```elixir
117+
iex> Application.stop(:kv)
118+
:ok
119+
iex> Application.stop(:logger)
120+
:ok
121+
```
122+
123+
And let's try to start our application again:
124+
109125
```elixir
110126
iex> Application.start(:kv)
111127
{:error, {:not_started, :logger}}
112128
```
113129

114-
Now we get an error because an application that `:kv` depends on (`:logger` in this case) hasn't been started. Mix normally starts the whole hierarchy of applications defined in our project's `mix.exs` file and it does the same for all dependencies if they depend on other applications. But since we passed the `--no-start` flag, we need to either start each application manually in the correct order or call `Application.ensure_all_started` as follows:
130+
Now we get an error because an application that `:kv` depends on (`:logger` in this case) isn't started. We need to either start each application manually in the correct order or call `Application.ensure_all_started` as follows:
115131

116132
```elixir
117133
iex> Application.ensure_all_started(:kv)
118134
{:ok, [:logger, :kv]}
119-
iex> Application.stop(:kv)
120-
18:12:10.698 [info] Application kv exited :stopped
121-
:ok
122135
```
123136

124137
Nothing really exciting happens but it shows how we can control our application.

0 commit comments

Comments
 (0)