[英]Understanding Genserver - start_link/0 conflicts with default
I'm trying implement a GenServer
to monitor one of my functions and then restart the process after 1 hour. 我正在尝试实现
GenServer
来监视我的功能之一,然后在1小时后重新启动该过程。 Forgive the lack of knowledge as this if my first experience with GenServer
. 如果我第一次接触
GenServer
原谅缺乏知识。
defmodule Statcasters.Scheduler do
use GenServer
use Quantum.Scheduler,
otp_app: :statcasters
alias Statcasters.{Repo, Question, SportRadar.ActiveQuestion, SportRadar.Nba, SportRadar.Questions}
require IEx
def start_link do
GenServer.start_link(__MODULE__, %{})
end
def init(state) do
check_question()
{:ok, state}
end
def handle_info(:update, state) do
check_question()
{:noreply, state}
end
def check_question do
case question = Repo.get_by(Question, active: true, closed: true) do
question when not(is_nil(question)) ->
case ActiveQuestion.ready_for_answer_status(question) do
n when n in ["complete", "closed"] ->
question
|> Question.changeset(%{ready_for_answer: true, closed: true})
|> Repo.update()
end
_ ->
Process.send_after(self(), :update, 60*60*1000)
end
end
end
I want this code to restart my check_question/0
function after one hour if if fails to find the table row in the database. 如果未能在数据库中找到表行,我希望此代码在一小时后重新启动我的
check_question/0
函数。 I've received some help for this answer here . 在这里,我已经获得了一些有关此答案的帮助。 I think this is very close to what I want but I'm getting this error at compile time:
我认为这与我想要的非常接近,但是我在编译时遇到此错误:
** (CompileError) lib/statcasters/scheduler.ex:13: def start_link/0 conflicts with defaults from start_link/1
lib/statcasters/scheduler.ex:13: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
children = [
# Start the Ecto repository
supervisor(Statcasters.Repo, []),
# Start the endpoint when the application starts
supervisor(StatcastersWeb.Endpoint, []),
supervisor(Statcasters.Scheduler, [], restart: :transient),
# Start your own worker by calling: Statcasters.Worker.start_link(arg1, arg2, arg3)
# worker(Statcasters.Worker, [arg1, arg2, arg3]),
worker(Statcasters.Scheduler, [])
]
I need to restart the check_question
function if it doesn't find a question from the database. 如果找不到数据库中的问题,我需要重新启动
check_question
函数。 Thanks for the help. 谢谢您的帮助。
use Quantum.Scheduler
is already defining a start_link/0
and start_link/1
for you. use Quantum.Scheduler
已经为您定义了一个start_link/0
和start_link/1
。 You cannot use Quantum.Scheduler
and use GenServer
in the same module, define them in different ones. 您不能在同一模块中
use Quantum.Scheduler
和use GenServer
,也不能在不同模块中定义它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.