在Elixir上工作时遇到了一些问题。
这是我的mix.exs
...
defmodule FlockTest.Mixfile do
use Mix.Project
def project do
[app: :flock_test,
version: "0.1.0",
elixir: "~> 1.4",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]
end
# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
# Specify extra applications you'll use from Erlang/Elixir
[
extra_applications: [:logger],
mod: {FlockTest, []}
]
end
...
...
这是FlockTest
的代码。
defmodule FlockTest do
@moduledoc """
Documentation for FlockTest.
"""
def start(_type, _args) do
IO.puts "Start Flock"
end
end
我使用mix run --no-halt
运行此代码,但这会导致这样的错误。
=INFO REPORT==== 12-Nov-2017::17:47:39 ===
application: logger
exited: stopped
type: temporary
** (Mix) Could not start application flock_test: FlockTest.start(:normal,
[]) returned a bad value: :ok
我做错了吗?
答案 0 :(得分:7)
您的应用程序的start/2
函数应该启动并返回应用程序的顶级进程,通常是一个超级用户实例。现在,您返回IO.puts
返回的值,成功时为:ok
。有效的返回值为{:ok, pid} | {:ok, pid, state} | {:error, reason :: term}
,如文档here所示。有关此示例,您可以使用mix new foo --sup
创建新应用程序并查看lib/foo/application.ex
。以下是start/2
的外观:
def start(_type, _args) do
# List all child processes to be supervised
children = [
# Starts a worker by calling: A.Worker.start_link(arg)
# {A.Worker, arg},
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: A.Supervisor]
Supervisor.start_link(children, opts)
end