我有一个简单的GenServer,它在这里:
defmodule MyApp.ScoreTableQueue do
use GenServer
@impl true
def init(stack) do
{:ok, stack}
end
@impl true
def handle_call(:pop, _from, state) do
{:reply, state, []}
end
@impl true
def handle_cast({:push, item}, state) do
{:noreply, [item | state]}
end
end
我想在此模块中使用此GenServer:
defp order_score(question, season) do
for team <- season.teams do
state = score_table_map(question, team)
# Push state on GenServer queue
end
create_score_table(question, season)
end
defp score_table_map(question, team) do
p_score = Enum.find(team.prediction_scores, &(&1.question_id == question.id))
%{team_score: p_score.score, team_id: p_score.team_id}
end
defp create_score_table(question, season) do
changeset = ScoreTable.changeset(%ScoreTable{
question_id: question.id,
season_id: season.id,
table_details: %{
information: # Pop state of genserver
}
})
Repo.insert(changeset)
end
如该代码示例中所指出的,我想在GenServer上的循环过程中以及在下面的变更集上弹出状态后,将某些状态推入。
我的主要问题是如何在其他模块中初始化genserver,这是最佳实践吗?
答案 0 :(得分:4)
您将不想在另一个模块中初始化GenServer。您将需要将其添加到您的监督树中。
您可能还想考虑将“ API”功能添加到GenServer模块,以便其用户不需要知道它是GenServer。像
# This assumes you have named your GenServer the same as the module name
def push(item) do
GenServer.call(__MODULE__, {:push, item})
end
def pop() do
GenServer.call(__MODULE__, :pop)
end
然后,您可以像需要的普通函数一样调用它。
MyApp.ScoreTableQueue.push(:foo)
stack = MyApp.ScoreTableQueue.pop()
...