3

我目前正在构建一个带有 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 域套接字,我将 JSON 推送到其中UNIXSocket并使用EventMachine.start_unix_domain_server. 但这只允许双向通信。我也考虑过使用 resque,但这更像是一个消息队列,而我需要总线。它依赖于redis。我很确定一定有一个 gem,它在 ruby​​ 中实现了一些消息总线,但是谷歌搜索并没有导致任何结果

4

1 回答 1

2

最后,我使用 Eventmachine Channels 破解了一个快速自己的解决方案。

这是我的服务器。基本上,客户端连接/tmp/messagebus.sock并发送数据。推入套接字的所有内容都会发送给所有其他客户端。

require 'rubygems'
require 'eventmachine'

module Messagebus
  class Server
    attr_accessor :connections
    attr_accessor :channel

    def initialize
      @connections = []
      @channel = EventMachine::Channel.new
    end

    def start
      @signature = EventMachine.start_unix_domain_server '/tmp/messagebus.sock', Connection do |conn|
        conn.server = self
      end
    end

    def stop
      EventMachine.stop_server(@signature)

      unless wait_for_connections_and_stop
        EventMachine.add_periodic_timer(1) { wait_for_connections_and_stop }
      end
    end

    def wait_for_connections_and_stop
      if @connections.empty?
        EventMachine.stop
        true
      else
        puts "Waiting for #{@connections.size} connection(s) to finish ..."
        false
      end
    end
  end

  class Connection < EventMachine::Connection
    attr_accessor :server

    def post_init
      log "Connection opened"
    end

    def server=(server)
      @server = server

      @subscription = server.channel.subscribe do |data|
        self.log "Sending #{data}"
        self.send_data data 
      end
    end

    def receive_data(data)
      log "Received #{data}"
      server.channel.push data
    end

    def unbind
      server.channel.unsubscribe @subscription
      server.connections.delete(self)
      log "Connection closed"
    end

    def log(msg)
      puts "[#{self.object_id}] #{msg}"
    end
  end
end

EventMachine::run {
  s = Messagebus::Server.new
  s.start
  puts "New server listening"
}
于 2012-07-11T21:00:15.333 回答