如何在Rails 3.2中将兔子作业作为后台进程运行

时间:2018-10-01 21:14:06

标签: ruby-on-rails bunny

我们有一个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  

我已将以上内容转换为耙任务:

RAILS_ENV=local rake report_queue:listen

它必须一直运行。我怎么称呼它?可能作为耙后台任务?我们正在使用nginx / unicorn作为我们的网络服务器。

0 个答案:

没有答案