不处理强制转换的事件处理程序的 GenServer 实现
GenServer implementation of an event handler not handling casts
我正在尝试在我的 Phoenix 应用程序中使用 GenServer
as an event handler for EventBus
,但出于某种原因,我似乎无法弄清楚为什么没有调用句柄转换函数。我用 :observer.start()
.
检查了进程是否还活着
为了让 GenServer
正确处理 cast 调用,我缺少什么吗?
本质上,流程函数应该处理传入事件并将其转换为 GenServer
,其中 GenServer
将处理转换并对该事件执行域逻辑。
----生成服务器模块----
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
关于 GenServer.cast/2
states, that the first parameter in call to GenServer.cast/2
must be of a type server()
的文档是:
any of the values described in the “Name registration” section of the documentation for this module.
在您的代码中,您启动 link 未命名:
GenServer.start_link(__MODULE__, [], opts)
但随后您投射到指定的 GenServer
:
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓
GenServer.cast(__MODULE__, {topic_id, event_id})
修复它的最简单方法,启动名为的服务器:
GenServer.start_link(__MODULE__, [], name: __MODULE__)
我正在尝试在我的 Phoenix 应用程序中使用 GenServer
as an event handler for EventBus
,但出于某种原因,我似乎无法弄清楚为什么没有调用句柄转换函数。我用 :observer.start()
.
为了让 GenServer
正确处理 cast 调用,我缺少什么吗?
本质上,流程函数应该处理传入事件并将其转换为 GenServer
,其中 GenServer
将处理转换并对该事件执行域逻辑。
----生成服务器模块----
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
关于 GenServer.cast/2
states, that the first parameter in call to GenServer.cast/2
must be of a type server()
的文档是:
any of the values described in the “Name registration” section of the documentation for this module.
在您的代码中,您启动 link 未命名:
GenServer.start_link(__MODULE__, [], opts)
但随后您投射到指定的 GenServer
:
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓
GenServer.cast(__MODULE__, {topic_id, event_id})
修复它的最简单方法,启动名为的服务器:
GenServer.start_link(__MODULE__, [], name: __MODULE__)