GenServer实现的事件处理程序不处理强制转换

约翰

我试图在Phoenix应用GenServer程序中用作事件处理EventBus程序,但由于某些原因,我似乎无法弄清楚为什么未调用handle cast函数。我检查了过程是否还存在:observer.start()

我缺少GenServer正确处理转换呼叫的功能吗?

本质上,流程函数应该处理传入的事件并将其强制转换GenServerGenServer将处理强制转换并对该事件执行域逻辑的位置。

---- Gen服务器模块----

defmodule App.Notifications.EventHandler do
  use GenServer
  require Logger


  def start_link(opts \\ []) do
    {:ok, pid} = GenServer.start_link(__MODULE__, [], opts)
  end

  def init([]) do
    {:ok, []}
  end

  def process({topic_id, event_id}) do
    Logger.info("event notification process recieved!!") <---- THIS IS GETTING PRINTED!

    GenServer.cast(__MODULE__, {topic_id, event_id})
  end



def handle_cast({topic_id, event_id}, state) do
  Logger.info("event  notification data Recieved!!") <----- THIS IS NOT

  # do stuff

  {:noreply, state}
end


end

----应用模块----

defmodule App.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application


  def start(_type, _args) do

    EventBus.subscribe({App.Notifications.EventHandler, ["^event_notification_created$"]})

    # List all child processes to be supervised
    children = [
      # Start the Ecto repository
      App.Repo,
      # Start the endpoint when the application starts
      AppWeb.Endpoint,
      # Starts a worker by calling: App.Worker.start_link(arg)
      # {App.Worker, arg},,
      App.Notifications.EventHandler <--- STARTING THE GEN SERVER HERE
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: App.Supervisor]
    Supervisor.start_link(children, opts)
  end

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  def config_change(changed, _new, removed) do
    App.Endpoint.config_change(changed, removed)
    :ok
  end
end
阿列克谢·马蒂什金(Alexei Matiushkin)

有关GenServer.cast/2状态的文档,调用中的第一个参数GenServer.cast/2必须为以下类型server()

此模块的文档名称注册”部分中描述的任何值

在您的代码中,启动未命名的链接:

GenServer.start_link(__MODULE__, [], opts)

但随后您将其强制转换为GenServer

#              ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓
GenServer.cast(__MODULE__, {topic_id, event_id})

修复它的最简单方法是,启动名为:

GenServer.start_link(__MODULE__, [], name: __MODULE__)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章