ruby进程间通信

时间:2011-05-23 19:55:28

标签: ruby-on-rails ruby

我有一个Rails项目和两个在后台运行的ruby迷你守护进程。什么是他们之间沟通的最佳方式?

下面的沟通应该是可能的: Rails - >过程1 - >过程2 - >轨道

有些请求会同步,其他请求会同步。

队列(类似于AMQ,还是基于Redis的自定义)或RPC HTTP调用?

2 个答案:

答案 0 :(得分:3)

同时检查DRb

答案 1 :(得分:1)

我通过RabbitMq +兔子宝石实现了一个系统。

更新

阅读http://blog.brightbox.co.uk/posts/queues-and-callbacks后,我决定试用RabbitMQ。有两个gems amqp(async,eventmachine based)或bunny(sync)。 amqp很棒,但如果你和乘客一起使用Rails,它可以做一些奇怪的事情。

系统像这样工作,守护进程在队列中侦听消息:

# The incoming data should be a JSON encoded hash that looks like:
# { "method" => method_to_call, "opts" => [ Array of opts for method ],
#   "output" => "a queue where to send the result (optional)" }
# If output is specified it will publish the JSON encoded response there.
def listen_on(queue_name, class)
  BUNNY.start
  bunny = BUNNY.queue(queue_name)

  bunny.subscribe do |msg|
    msg = JSON.parse(msg[:payload])

    result = class.new.send(msg["method"], *msg["opts"])

    if msg["output"]
      BUNNY.queue(msg["output"]).publish(result.to_json)
    end
  end

因此,一旦收到消息,它就会从类中调用一个方法。需要注意的一点是,在守护进程中使用Bunny进行Rails和amqp是理想的。但我喜欢使用一个gem pe服务。

相关问题