我一直在尝试实例化一个genserver进程,它将在Phoenix框架中订阅PubSub,以下是我的文件和错误:
config.ex:
config :excalibur, Excalibur.Endpoint,
pubsub: [
adapter: Phoenix.PubSub.PG2,
name: Excalibur.PubSub
]使用genserver的模块:
defmodule RulesEngine.Router do
import RulesEngine.Evaluator
use GenServer
alias Excalibur.PubSub
def start_link(_) do
GenServer.start_link(__MODULE__, name: __MODULE__)
end
def init(_) do
{:ok, {Phoenix.PubSub.subscribe(PubSub, :evaluator)}}
IO.puts("subscribed")
end
# Callbacks
def handle_info(%{}) do
IO.puts("received")
end
def handle_call({:get, key}, _from, state) do
{:reply, Map.fetch!(state, key), state}
end
end当我做一个iex -S混搭的时候,这个错误发生了:
** (Mix) Could not start application excalibur: Excalibur.Application.start(:normal, []) returned an error: shutdown: failed to start child: RulesEngine.Router
** (EXIT) an exception was raised:
** (FunctionClauseError) no function clause matching in Phoenix.PubSub.subscribe/2
(phoenix_pubsub 1.1.2) lib/phoenix/pubsub.ex:151: Phoenix.PubSub.subscribe(Excalibur.PubSub, :evaluator)
(excalibur 0.1.0) lib/rules_engine/router.ex:11: RulesEngine.Router.init/1
(stdlib 3.12.1) gen_server.erl:374: :gen_server.init_it/2
(stdlib 3.12.1) gen_server.erl:342: :gen_server.init_it/6
(stdlib 3.12.1) proc_lib.erl:249: :proc_lib.init_p_do_apply/3那么,哪种方法才是启动订阅PubSub主题的Genserver的正确方法呢?
发布于 2020-05-06 12:32:45
根据文档,Phoenix.PubSub.subscribe/3具有以下规范:
@spec subscribe(t(), topic(), keyword()) :: :ok | {:error, term()}@type topic :: binary()在哪里。也就是说,您的init/1应该如下所示
def init(_) do
{:ok, {Phoenix.PubSub.subscribe(PubSub, "evaluator")}}
|> IO.inspect(label: "subscribed")
end请注意,我还将IO.puts/1更改为IO.inspect/2以保留返回值。
https://stackoverflow.com/questions/61624376
复制相似问题