1

我们有一个 Rails 3.2 应用程序,将使用 Bunny 监听 RabbitMQ 队列。侦听器的代码将如下所示:

require 'bunny'

class TestBunny
  def self.do 

    connection = Bunny.new(host: RABBITMQ_HOST, user: RABBITMQ_USERNAME, pass: RABBITMQ_PASSWORD, automatically_recover: false)
    connection.start

    channel = connection.create_channel
    queue = channel.queue('custom_reports_queue', durable: false)

    channel.prefetch(1)
    puts ' [*] Waiting for messages. To exit press CTRL+C'
    begin
      queue.subscribe(manual_ack: true, block: true) do |delivery_info, _properties, body|
        puts " [x] Received '#{body}'"

        # imitate some work
        sleep body.count('.').to_i
        puts ' [x] Done'
        OfflineReportMailer.success.deliver
        channel.ack(delivery_info.delivery_tag)
      end
    rescue Interrupt => _
      connection.close
    end
  end  

我已将上述内容转换为 rake 任务:

RAILS_ENV=local rake report_queue:listen

它需要一直运行。我怎么称呼这个?可能作为 rake 后台任务?我们使用 nginx / unicorn 作为我们的网络服务器。

4

0 回答 0