how to run a bunny job as a background process in Rails 3.2

195 views Asked by At

We have a Rails 3.2 app and will listening on a RabbitMQ queue using Bunny. The code for the listener will be like:

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  

I have converted the above to a rake task:

RAILS_ENV=local rake report_queue:listen

It needs to be always running. How would I call this? Possibly as a rake background task? We are using nginx / unicorn as our webserver.

0

There are 0 answers