Project

queight

0.0
No commit activity in last 3 years
No release in over 3 years
a lightweight wrapper around bunny
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
= 2.1.1
~> 10.0
>= 0
~> 0.42.0

Runtime

>= 0
~> 0.0.11
 Project Readme

Queight

Build Status

This is a lightweight wrapper around the bunny gem. It tries to handle caching the rabbitmq connection and channels in a sensible way:

  • The main connection is cached globally.
  • Channels are allocated from a pool since they are not thread safe.
  • Channels for subscribers are allocated one-off and specify a prefetch.

This matches our normal usage of rabbitmq, with publishing happening constantly throughout usage, while subscription is usually a dedicated process.

Installation

Add this line to your application's Gemfile:

gem 'queight'

And then execute:

$ bundle

Or install it yourself as:

$ gem install queight

Usage

First configure your environment:

RABBITMQ_URL=amqp://username:password@hostname:port

You can use the following query parameters to further customize behaviour:

parameter effect
size connection pool size
id client identification

Then you can get a Queight client:

client = Queight.current

In order to publish messages you'll need an exchange:

topic_exchange = Queight.topic "exchange.topic"
direct_exchange = Queight.direct "exchange.direct"

And in order to subscribe to queues you'll need a queue:

queue = Queight.queue("queue.name")

Then you can just publish messages using the client and the exchange:

client.publish(topic_exchange, "message", "routing.key")

Note that publishing uses transactions by default (slow!), so if you're ok with messages hitting the floor and not knowing about it, try:

client.publish_without_transaction(topic_exchange, "message", "routing.key")
client.publish!(topic_exchange, "message", "routing.key")

You can also publish to a queue directly (using the default exchange). This will ensure the queue is declared before publishing:

client.publish_to_queue(message, queue)
client.publish_to_queue_without_transaction(message, queue)
client.publish_to_queue!(message, queue)

Queue size can be queried:

client.message_count(queue)

Publishing to a topic exchange

exchange = Queight.topic("test.exchange.topic")
message = JSON.dump(id: 1, message: "hello")
routing_key = "message.1"
client.publish(exchange, message, routing_key)
queue = Queight.queue("test.queue1")

client = Queight.current

Binding queues

exchange = Queight.topic("exchange.name")
# Declare a queue and routing patterns
queue = Queight.queue("queue.name", routing: ["message.#", "reply.#"])
client.bind(exchange, queue)

Subscribing to messages from a queue

Subscribing by default will block and require manual ack.

client.subscribe(queue) do |channel, delivery_info, properties, payload|
  do_something(payload)
  channel.acknowledge(delivery_info.delivery_tag)
end

Development

The .env file assumes a rabbitmq running on localhost with a username and password of guest. The provided docker-compose.yml let's you run one with:

docker-compose up -d

If your docker does not expose ports on localhost you may need to override this, e.g. dinghy on OS X should override in .env.local with:

RABBITMQ_URL=amqp://guest:guest@queight-rabbitmq-1.docker

Alternatively, you can use the provided ruby18 and ruby23 services to run tests:

docker-compose run --rm ruby18 bundle
docker-compose run --rm ruby18

docker-compose run --rm ruby23 bundle
docker-compose run --rm ruby23

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/JonathonMA/queight. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.