Cottontail
This library is a wrapper around the popular AMQP bunny gem. It is inspired by Sinatra to easily consume messages on different routing keys.
Installation
System wide:
gem install cottontail
Or in your Gemfile:
gem 'cottontail'
Usage
When using this gem, you should already be familiar with RabbitMQ and, idealy, Bunny as it's a wrapper around it. Given we place our code into worker.rb
, we can define a simple class like so:.
require 'cottontail'
class Worker
include Cottontail::Consumer
session ENV['RABBITMQ_URL'] do |worker, session|
channel = session.create_channel
queue = channel.queue('', durable: true)
worker.subscribe(queue, exclusive: true, ack: false)
end
consume do |delivery_info, properties, payload|
logger.info payload.inspect
end
end
# The following will start Cottontail right away. You need to be aware that it
# will enter the Bunny subscribe loop, so it will block the process.
Worker.start
To run it, you may start it like the following code example. However, you should use a more sophisticated solution for daemonizing your processes in a production environment. See http://www.ruby-toolbox.com/categories/daemonizing for futher inspiration.
ruby worker.rb &
Copyright © 2012-2015 Rudolf Schmidt, released under the MIT license