【发布时间】:2019-11-07 22:18:22
【问题描述】:
我试图在我的 Phoenix 应用程序中使用 GenServer 作为 EventBus 的事件处理程序,但由于某种原因,我似乎无法弄清楚为什么没有调用句柄转换函数。我使用:observer.start() 检查了该进程是否存在。
GenServer 是否缺少正确处理演员调用的东西?
本质上,流程函数应该处理传入事件并将其转换为GenServer,其中GenServer 将处理转换并对该事件执行域逻辑。
----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
【问题讨论】:
标签: elixir phoenix-framework gen-server