Code Monkey home page Code Monkey logo

bunny-publisher's Introduction

BunnyPublisher

Build Status Gem Version

Ruby publisher(producer) for RabbitMQ based on bunny.

Why?

Bunny is a great RabbitMQ client that allows to implement both consumers & producers. In order to publish a message it requires only few lines of code:

conn = Bunny.new
conn.start
channel = conn.create_channel
exchange = channel.exchange('my_exchange')
exchange.publish('message', routing_key: 'some.key')

But usually more features are requested. Such as:

  1. Multi-thread environment should re-use AMQP-connection.
  2. No message should be lost if there is no sutable routing at the moment of publishing.
  3. Callbacks support (e.g. for logging or metrics)

So publisher implementation becomes more complex and hard to maintain. This project aims to reduce amount of boiler-plate code to write. Just use basic publisher with modules for your needs:

  1. BunnyPublisher::Base - basic publisher with callbacks support. Based on publisher of Sneakers.
  2. BunnyPublisher::Mandatory - module for publisher that uses mandatory option to handle unrouted messages
  3. BunnyPublisher::RPC - module for publisher to support RPC

Installation

Required ruby version is 2.5. Add this line to your application's Gemfile:

gem 'bunny-publisher'

And then execute:

$ bundle

Or install it yourself as:

$ gem install bunny-publisher

Basic usage

Publisher is ready to be used out of the box

# publishes to default exchange, expects "such.easy" queue to exist
BunnyPublisher.publish 'wow', routing_key: 'such.easy'

Set RABBITMQ_URL environment variable to connect to custom host/vhost.

Publisher can also be configured

BunnyPublisher.configure do |c|
  # custom exchange options
  c.exchange = 'custom'
  c.exchange_options = { type: 'fanout' }

  # custom connection (e.g. with ssl configured)
  c.connection = Bunny.new(something: 'custom')

  # or just custom options for connection
  c.amqp = ENV['CLOUDAMQP_URL']
  c.heartbeat = 10
end

Mandatory publishing

The publisher also supports :mandatory option to handle unrouted messages. In case of unrouted message publisher:

  1. Will create a queue by the name of routing key
  2. Will bind queue to the exchange
  3. Publish message again

Configure publisher to use mandatory option

BunnyPublisher.configure do |c|
  c.mandatory = true

  # ...
end

Publish message with new routing key

BunnyPublisher.publish 'wow', routing_key: 'such.reliable' # this will create "such.reliable" queue

You also can set custom settings for queue definition

BunnyPublisher.configure do |c|
  c.mandatory = true

  c.queue = 'funnel' # if not set, routing_key is used for the name
  c.queue_options = { durable: true }

  # ...
end

Remote Procedure Call RPC

Not implemented yet

Test mode

BunnyPublisher also has a test mode which replaces exchange and prevents real connection to RabbitMQ

BunnyPublisher.configure do |c|
  c.test = true # or Rails.env.test?

  # ...
end

Now publisher has #messages, #flush! methods:

describe SomeServiceThatPublishesToRabbitMQ, '#call' do
  subject { described_class.new(some: 'attributes').call }

  before { BunnyPublisher.flush! }

  it 'publishes a message' do
    expect { subject }.to change(BunnyPublisher.messages).by(1)
  end
end

Testing

Run rabbitmq server (in separate console):

docker run --rm -p 5672:5672 -p 15672:15672 rabbitmq:3.8-management-alpine

Run tests with:

bundle
rake

Use wwtd to run test matrix:

gem install wwtd
wwtd

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/veeqo/bunny-publisher.

License

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

Sponsored by Veeqo

bunny-publisher's People

Contributors

main24 avatar sharshenov avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.