【发布时间】:2012-05-02 03:23:29
【问题描述】:
我目前正在构建一个包含 Rails 进程和多个工作进程的架构,这些进程需要通知某些事件(例如创建对象)。
Rails
| API Worker
+----------o------o--------o------ - - -
Some other
daemon
我想做以下事情
class Article
after_creation do
MessageBus.send type: "article-created", id: self.id
end
end
虽然进程(API、Worker、Daemons,...)只是订阅消息总线,并在消息进入时调用一个块。
MessageBus.subscribe do |msg|
if msg['type'] == 'article-created'
# if this is my websocket daemon, I would push the article to the browser
# if this is my indexing daemon, I would reindex the full-text search
# if this is ... you get the deal.
end
end
目前我正在使用本地 unix 域套接字,在其中我使用 UNIXSocket 推送 JSON 并使用 EventMachine.start_unix_domain_server 获取它。但这只允许双向通信。我也考虑过使用 resque,但这更像是一个消息队列,而我需要总线。它依赖于redis。我很确定一定有一个 gem,它在 ruby 中实现了一些消息总线,但是谷歌搜索并没有导致任何结果
【问题讨论】:
-
您尝试过 EventMachine 频道吗? eventmachine.rubyforge.org/EventMachine/Channel.html
-
+1 用于 EventMachine 通道。要考虑的另一件事(如果您的工作人员与主服务器在同一进程空间中运行)是 Ruby 对观察者模式具有原生支持 Observable
标签: ruby-on-rails ruby message-bus