Raq

Raq makes it easy to create durable message queue consumers. It tries to learn from Thin and Rack in order to provide a reasonably familiar way of creating and running AMQP consumers.

Raq expresses the opinion that, like database configuration, queue configuration should handled by the environment. Raq offers the ability to specify message broker connection information and queue names as command line flags, or in a separate configuration file.

Installation

Add this to your Gemfile:

gem 'raq'

Then install it by running Bundler:

$ bundle

Usage

Raq provides a Rack-like api for creating consumers and message middleware by implementing run and use.

However, unlike Rack, Raq does not infer anything about the payload. It exposes the protocol level meta information, and the unmodified payload body directly as arguments to your consumers. Though, you can quickly chain together middleware to satisfy your application.

Consider this example.rb:

require 'raq'

class Echo < Struct.new(:app)
  def call(meta, payload)
    puts "Echo: #{payload}"
    app.call(meta,payload)
  end
end

runner = Raq::Runner.new(ARGV)
server = Raq::Server.new(
  connection: runner.connection_options,
  queues: runner.options[:queue]) do

  use Echo

  run do |meta, payload|
    puts "Acknowledging #{payload}"
    meta.ack

    # Not for long lived processes...
    server.connection.close { EM.stop }
  end
end

server.run

When run like so:

$ ruby example.rb --queue a.queue.with.messages

It will print the payload to stdout a couple times, acknowledge the message, and exit.

Configuration

You can choose to configure Raq with either commandline flags or a configuration file. The configuration file is a simple YAML file. The keys should be the same as the long form commandline flags.

For example, consider this sample.yml:

--
queue: RabbitEmergencyRoom

When you run the above example.rb like so:

$ ruby example.rb --config sample.yml

It will connect to the RabbitEmergencyRoom queue!

Contributing to Raq

Copyright © 2013 Caleb Buxton. See LICENSE.txt for further details.