忽略没有 Mix 配置文件,我写了以下内容:
defmodule Test.Supervisor do
use Supervisor
def start_link do
#"name:" will show up in :observer...
Supervisor.start_link(__MODULE__, [], [name: :"root_supervisor"])
end
def init(args) do
children = [
worker(Test.Method, [], [function: :start, id: "my_root_process"]),
]
supervise(children, [strategy: :one_for_one, name: :root])
end
end
defmodule Test do
def start(_type, _args) do
Test.Supervisor.start_link()
end
end
defmodule Test.Method do
def start do
IO.puts("Expect to see me often... #{self}")
end
end
在第一次运行(iex -S mix)后没有重新启动应用程序就崩溃了。错误信息是:
=INFO REPORT==== 14-Jan-2016::22:34:04 ===
application: logger
exited: stopped
type: temporary
** (Mix) Could not start application mememe: Test.start(:normal, {}) returned
an error: shutdown: failed to start child: "my_root_process"
** (EXIT) :ok
但是,如果我改为直接Test.start()
调用Test.Method.start()
,如下所示:
defmodule Test do
def start(_type, _args) do
Test.Method.start()
end
end
然后它运行良好,但代码将不受监督。我很确定我在实施或理解方面犯了一个基本错误,但这个错误到底是什么?