Elixir 为什么你的孩子不工作
我是长生不老药的初学者 我有一个应用程序在application.ex中启动一个自定义主管。代码:Elixir 为什么你的孩子不工作,elixir,otp,Elixir,Otp,我是长生不老药的初学者 我有一个应用程序在application.ex中启动一个自定义主管。代码: defmodule MyApp do use Application def start(_type, _args) do import Supervisor.Spec children = [ supervisor(MyApp.Web.Endpoint, []), supervisor(MyApp.Repo, []), #my n
defmodule MyApp do
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
supervisor(MyApp.Web.Endpoint, []),
supervisor(MyApp.Repo, []),
#my notifier
MyApp.MyNotifier.Supervisor
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
end
主管的代码是这样的:
defmodule MyApp.MyNotifier.Supervisor do
use Supervisor
def start_link(_options), do:
Supervisor.start_link(__MODULE__, :ok, name: __MODULE__)
def start_my_notifier(state) do
Supervisor.start_child(__MODULE__, state)
end
def init(:ok) do
Supervisor.init([], strategy: :one_for_one)
end
end
defmodule MyApp.MyNotifier do
use GenServer
# Client
def start_link(state) do
GenServer.start_link(__MODULE__, state)
end
# Server
def init(state) do
# Reschedule
reschedule(state)
# Reply
{:ok, state}
end
def handle_info(:reschedule, state) do
case state["count"] < 9 do
true ->
# Send notification
MyNotifier.Helper.notify_past_delivery_time(sate["id"])
# Reschedule once more
reschedule(state)
false ->
# End process
Process.exit(self(), :normal)
end
{:noreply, state}
end
defp reschedule(state) do
Process.send_after(self(), :reschedule, state["time"] * 60 * 1000)
end
end
工人的代码是这样的:
defmodule MyApp.MyNotifier.Supervisor do
use Supervisor
def start_link(_options), do:
Supervisor.start_link(__MODULE__, :ok, name: __MODULE__)
def start_my_notifier(state) do
Supervisor.start_child(__MODULE__, state)
end
def init(:ok) do
Supervisor.init([], strategy: :one_for_one)
end
end
defmodule MyApp.MyNotifier do
use GenServer
# Client
def start_link(state) do
GenServer.start_link(__MODULE__, state)
end
# Server
def init(state) do
# Reschedule
reschedule(state)
# Reply
{:ok, state}
end
def handle_info(:reschedule, state) do
case state["count"] < 9 do
true ->
# Send notification
MyNotifier.Helper.notify_past_delivery_time(sate["id"])
# Reschedule once more
reschedule(state)
false ->
# End process
Process.exit(self(), :normal)
end
{:noreply, state}
end
defp reschedule(state) do
Process.send_after(self(), :reschedule, state["time"] * 60 * 1000)
end
end
当我在调试模式下运行应用程序(iex-S mix phx.server)并在worker的init函数中放入一个iex.pry时(然后我们强制应用程序进入启动子应用程序的状态)。为什么应用程序从不停止?调用的第二个参数应该是子规范,而不是:
Supervisor.start_child(__MODULE__, state)
它应该有点像:
Supervisor.start_child(__MODULE__, [self(), MyApp.MyNotifier, [state]])
我在supervisor代码中将supervisor策略更改为simple_one_for_one
Supervisor.init([], strategy: :simple_one_for_one)
这对我很有用。我更改了代码并返回此错误:(退出)退出:GenServer.call(MyApp.MyNotifier,{:start\u child,%%{“count”=>0,“id”=>1311,“name”=>“MyNotifier1311”,“time”=>15}},:infinity)**(退出)无进程:进程不活动或当前没有与给定名称关联的进程,可能是因为它的应用程序没有在我的单元测试中启动,我用以下代码启动了主管:MyApp.MyNotifier.supervisor.start\u link(:ok)哦,非常抱歉,我误读了这个问题,提出了一个绝对的废话。我已经用正确的版本更新了答案。谢谢你的回答。但是问题还在继续。。。当把一个IEx.pry放入工人的初始功能中时,工人的功能永远不会停止,你知道吗?你不能解决我的问题吗?你能试着把
主管.启动孩子(\uuu模块,状态)
改成主管.启动孩子(\uu模块,%%{id:MyApp.MyNotifier,start:{MyApp.MyNotifier,:启动孩子,[state]})
?@Dogbert谢谢你的回答。但是问题还在继续。。。当将一个IEx.pry放入init时,工人的功能永远不会停止there@Dogbert你知道我的上司吗?你不能解决我的问题吗?