【发布时间】:2017-09-07 02:32:55
【问题描述】:
我有一个 Elixir/Mix 应用程序(也是 Phoenix,但里面也有很多非 Phoenix 的东西),我想知道在“启动”代码中放入什么最佳实践,比如动态添加子代码到主管,开火“我还活着!” ping 或其他您希望在启动后立即发生的事情。
一个明显的地方是Application 文件,但那里的预期回报是来自Supervisor.start_link(children, opts) 的回报。因此,例如,在 Phoenix 应用程序中,我可以这样做:
defmodule MyApp do
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
supervisor(MyApp.Repo, []),
supervisor(MyApp.Endpoint, []),
supervisor(MyApp.DynamicSupervisorThingy, [])
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
start_val = Supervisor.start_link(children, opts)
# --- Put things right here that I want to start ----
MyApp.DynamicSupervisorThingy.add_children_dynamically()
MyApp.SomeModule.do_some_thingy()
MyApp.OtherModule.send_some_pings()
if MIX_ENV == :prod do
MyApp.YetAnother.prod_stuff_i_dont_want_in_dev()
end
start_val
end
end
这似乎是错误的,但我不知道我应该把这段代码放在哪里。
【问题讨论】: