Elixir的Library与OTP Application的关系

所有的library必须是独立的OTP application.

1
2
3
4
5
6
7
8
9
10
11
12
defp deps do
[{:phoenix, "~> 1.2.1"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.0"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.6"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:comeonin, "~> 2.5"},
{:guardian, "~> 0.14"},
{:cowboy, "~> 1.0"}]
end

application的依赖写在dpes中,如果该依赖只存在编译的时候,那往往就足够了,如果运行时需要此依赖,则必须在 application 函数中列出。

1
2
3
4
5
def application do
[mod: {Til, []},
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
:phoenix_ecto, :postgrex, :comeonin]]
end

application 函数中, mod 是一个callback。这个callback需要我们在指定的module中给出实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
defmodule Til do
use Application

# See https://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec

# Define workers and child supervisors to be supervised
children = [
# Start the Ecto repository
supervisor(Til.Repo, []),
# Start the endpoint when the application starts
supervisor(Til.Endpoint, []),
# Start your own worker by calling: Til.Worker.start_link(arg1, arg2, arg3)
# worker(Til.Worker, [arg1, arg2, arg3]),
]

# See https://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Til.Supervisor]
Supervisor.start_link(children, opts)
end

# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
Til.Endpoint.config_change(changed, removed)
:ok
end
end

这里主要就是建立本应用的supervisor tree。

如果你觉得本文对你有帮助,请给我点赞助。