elixir - 如何通过 Supervisor.init 启动命名代理?
问题描述
我有一个非常简单的混合应用程序(它是凤凰伞项目的一部分)。它甚至不包括业务逻辑流程。例如:
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
我Agent
打了两次电话。一次put
,第二次get
。所以我想Agent
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]}
我怎样才能开始Agent
“内联”?
解决方案
感谢@hauleth 在 Elixir-lang slack 中回答了这个问题。
正确的行是:
%{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_link
)格式。它没有得到执行。我的意思是GenServer.start(Impl, init_args, opts)
-vsAgent.start(init_fn, opts)
推荐阅读
- dataframe - 如何将输入数据流保存到 Spark 中用于 sql 查询的执行数据结构中?
- vhdl - 未定义符号错误,但符号似乎在代码 VHDL 中定义
- mongodb - 在 mongodb 的数组搜索中使用 upsert
- java - 我正在尝试使用 URL 参数登录到 Steam 并使用 Steam 发送的 RSA 密钥加密密码,给出不正确的密码错误
- git - Git日志仅显示初始提交?
- mysql - 与枢轴合成器作斗争
- spring-boot - Spring定时任务cron表达式验证
- javascript - React-Admin:如何处理对象列表中的输入源
- flutter - 颤振如何摆脱图标填充?
- javascript - 数据可用时,MaterialUI 组件不会自动完成