首页 文章

Event Dispatcher是一个合适的解决方案吗?

提问于
浏览
2

我正在开发一个API项目,需要在响应已经返回给客户端之后(或者在执行之前)发送电子邮件和存储统计信息 . 对于这两种情况,我正在考虑使用Symfony的EventDispatcher组件(我没有使用Symfony作为框架),因此每个控制器操作都会调度一个事件来向队列添加电子邮件或将数据插入统计数据库表 .

所以这件事看起来像这样

Controller 
    => Send Response to client
    => Dispatch Event email => EmailEventListener => Mail queue
    => Dispatch Event stats => StatsEventLister => Database

我正在考虑这个因为我希望这些内部动作尽可能地异步 . 这种情况适合的解决方案吗?

编辑:正如Jovan Perovic建议我'm adding more information. The API is a REST API that users communicate with it via web or mobile apps and I want to log, store stats and send notifications (emails primarily) without penalizing the performance of the API, the first idea was to use something that run after returning the response to the client but I don'知道是否可以使用EventDispatcher . 即使使用队列来处理统计信息或通知,我也需要一个集中的地方,所有控制器都可以发送信息,以便记录日志和存储统计信息 .

我希望我的目标现在更清楚了 . 抱歉 .

1 回答

  • 1

    我认为你可以使用Request filtersAfter 适合你),但是,我从未试图在 Symfony2 框架之外使用它们 .

    对于异步操作,一般来说,套接字是你的朋友 . 您可以通过将数据发送到某个套接字来外部化逻辑,该套接字将依次处理数据 . If that processing is non-essential (例如电子邮件和统计信息),即使您的外部机制失败,您的请求也可以完成 .

    我前段时间读过关于 Gearman here(只是一个例子)的内容,这可能有助于通过创建单独的作业来外化 .

    希望这有点明白:)

相关问题