我正在尝试为调度程序应用程序(使用Elixir 1.5语法注释)设置监督树。该应用程序应该工作,使:如何将参数传递给:simple_one_for_one GenServer以初始化状态
- 应用程序启动使用注册表&调度监督员
- 的SchedulerSupervisor靴,并允许通过
start_child
回调动态添加的儿童。这需要用于构建调度状态的初始化参数。 - 调度程序在初始化时将其名称注册到注册中心,并使用一个包含状态的结构进行初始化(调度程序具有操纵调度的功能,但与我遇到的问题无关)。
如果我不传递任何参数,我能得到这个工作 - 创建没有登记表,以及我在创建后修改状态。一旦我尝试添加参数,系统出错 - 我知道这只是我的一个语法误解,但我不能为我的生活弄清楚我做错了什么。我还没有在这里找到这些文档非常有用,我试着从GH,GH Gists和在线文章中复制和修改示例,但是我无法使其工作。
当前设置 - 理想我想要通过id
,period
和targets
作为参数传递给start_child
,但可以甚至可以将其与一个参数不工作,所以只用一个坚持,直到我能得到它正在运行:
的应用:
defmodule Assay.Application do
use Application
def start(_type, _args) do
children = [
{Assay.SchedulerSupervisor, []},
{Registry, keys: :unique, name: Assay.Scheduler.Registry}
]
opts = [strategy: :one_for_all, name: Assay.Supervisor]
Supervisor.start_link(children, opts)
end
end
主管:
defmodule Assay.SchedulerSupervisor do
use Supervisor
@name Assay.SchedulerSupervisor
def start_link(_args) do
Supervisor.start_link(__MODULE__, :ok, name: @name)
end
def start_schedule(id) do
Supervisor.start_child(@name, [id])
end
def init(_) do
Supervisor.init([Assay.Scheduler], [strategy: :simple_one_for_one, name: @name])
end
end
钍ËGenServer(只显示相关的初始化函数)
defmodule Assay.Scheduler do
use GenServer
alias Assay.Scheduler
require Logger
defstruct targets: [], period: 60_000, id: nil, active: false
def start_link(id) do
GenServer.start_link(__MODULE__, [id], [name: via_tuple(id)])
end
def init([id]) do
Logger.info "starting a new #{__MODULE__} with id #{id}"
{:ok, %Scheduler{id: id}}
end
end
编辑:实际的错误可能会帮助 - 我可以看到ARGS是错误的,我只是不明白,为什么:
{:error,
{:EXIT,
{:undef,
[{Assay.Scheduler, :start_link, [[], 1], []},
{:supervisor, :do_start_child_i, 3, [file: 'supervisor.erl', line: 381]},
{:supervisor, :handle_call, 3, [file: 'supervisor.erl', line: 406]},
{:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 636]},
{:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 665]},
{:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 247]}]}}}
什么是你得到确切的错误? – Dogbert
ah,对不起,添加了日志错误信息@Dogbert – DanCouper
你确定这是问题中代码的错误信息吗?您没有将60000传递给start_child,但它在错误消息中。 – Dogbert