Elixir 如何通过Supervisor.init启动命名代理?

Elixir 如何通过Supervisor.init启动命名代理?,elixir,Elixir,我有一个非常简单的混合应用程序(它是凤凰伞项目的一部分)。它甚至不包括业务逻辑的流程。例如: defmodule BGAdapter.Application do use Application def start(_type, _args) do children = [ BGAdapter.LifeCycle # Agent ] Supervisor.start_link(children, strategy: :one_for_one, na

我有一个非常简单的混合应用程序(它是凤凰伞项目的一部分)。它甚至不包括业务逻辑的流程。例如:

defmodule BGAdapter.Application do
  use Application

  def start(_type, _args) do
    children = [
      BGAdapter.LifeCycle # Agent
    ]

    Supervisor.start_link(children, strategy: :one_for_one, name: BGAdapter.Supervisor)
  end
end
我的
代理
打了两次电话。一次
put
,第二次
get
。 因此,我想将单独的
代理
BGAdapter.LifeCycle
模块替换为smth,如:

defmodule BGAdapter.Application do
  use Application

  def start(_type, _args) do
    children = [
      { Agent, fn -> %{} end, name: BGAdapter.LifeCycle } # This does not work
    ]
...
错误是:

** (Mix) Could not start application bg_adapter: exited in: BGAdapter.Application.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (ArgumentError) supervisors expect each child to be one of the following:

  * a module
  * a {module, arg} tuple
  * a child specification as a map with at least the :id and :start fields
  * or a tuple with 6 elements generated by Supervisor.Spec (deprecated)

Got: {Agent, #Function<0.33439399/0 in BGAdapter.Application.start/2>, [name: BGAdapter.LifeCycle]}
**(Mix)无法启动应用程序bg\U适配器:在:BGAdapter.application.start(:normal,[])中退出
**(退出)引发了一个异常:
**(错误)监管者希望每个孩子都是以下其中之一:
*模块
*一个{模,arg}元组
*作为映射的子规范,至少包含:id和:start字段
*或者由Supervisor.Spec生成的包含6个元素的元组(已弃用)
获取:{Agent,#Function,[name:BGAdapter.LifeCycle]}

我如何启动
代理
“内联”

多亏@hauleth用长生不老药来回答这个问题

正确的路线是:

%{id: BGAdapter.LifeCycle, start: {Agent, :start_link, [fn -> %{} end, [name: BGAdapter.LifeCycle]]}}
应用程序文件:

defmodule BGAdapter.Application do
  use Application

  def start(_type, _args) do
    children = [
      %{
        id: BGAdapter.LifeCycle,
        start: {Agent, :start_link, [fn -> %{} end, [name: BGAdapter.LifeCycle]]}
      }
    ]

    Supervisor.start_link(children, strategy: :one_for_one, name: BGAdapter.Supervisor)
  end
end
我不确定,但我认为问题在于
Agent
有自己的
start
(和
start\u link
)格式。它没有得到执行。我的意思是-
GenServer.start(Impl,init_args,opts)
vs
Agent.start(init_fn,opts)