Project

smart-que

0.0
No commit activity in last 3 years
No release in over 3 years
Publish messages to RabbitMq broker and Consume with independent workers.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 5.11
~> 12.3

Runtime

 Project Readme

SmartQue

Welcome to SmartQue gem! This gem uses bunny library to connect with RabbitMq message broker to publish and consume messages with defined queues.

Installation

Add this line to your application's Gemfile:

gem 'smart-que', '~> 0.2.6'

And then execute:

$ bundle

Or install it yourself as:

$ gem install smart-que -v 0.2.6

Usage

  1. Setup RabbitMq
  2. Add smart-que gem to your Gemfile and perform bundle install
  3. Require smart_que in the application.rb file
  4. Create Publisher/Consumer classes & start publish/consume messages

SmartQue Publisher

SmartQue publisher is used to publish messages to the previously setup RabbitMq server. All messages are converted to JSON format before publishing to the queue. RabbitMq server details and queue lists can be configured as follows

# File: config/initializers/smart_que.rb
require 'smart_que'

SmartQue.configure do |f|
  f.host = ENV[:rabbit_mq][:host']
  f.port = ENV[:rabbit_mq][:port']
  f.vhost = ENV[:rabbit_mq][:vhost']
  f.username = ENV[:rabbit_mq][:username']
  f.password = ENV[:rabbit_mq][:password']
  f.logger = Rails.logger
  f.queues = [
      'default',
      'sms_otp',
      'fcm_push'
  ]
end

$publisher = SmartQue::Publisher.new

# Add this below mentioned line to your application.rb
# File : config/application.rb

require 'smart_que'

After initializing SmartQue publisher, it can be accessed anywhere in the rails application to publish messages to the RabbitMq server.

$publisher.publish('sms_otp', { number: '+919898123123', message: 'Test Message' })

SmartQue unicast

$publisher.unicast(queue_name, payload)

The unicast method will provide an option to publish message to any queue which are not predefined with initialization configuration. The queue_name parameter can be any queue name, which will be created and bound to direct exchange instantly if doesn't exist.

$publisher.unicast('sms_alert', { number: '+919898123123', message: 'Test Message' })

SmartQue multicast

$publisher.multicast(topic_name, payload)

The multicast method will provide an option to multicast message to queues which are not predefined with initialization configuration. The topic_name parameter can be any topic name and message will be consumable by any queues which subscribe to this topic name.

$publisher.multicast('weather', { message: 'Test Message' })

SmartQue broadcast

$publisher.broadcast(payload)

The broadcast method will provide an option to broadcast message to queues which are not predefined with initialization configuration. The broadcst message will be consumable by all queues which are bound to rabbitmq fanout exchange.

$publisher.broadcast({ message: 'Broadcast Test Message' })

SmartQue Consumer

The consumer class should be inherited from SmartQue::Consumer, and each consumer will be listening to a defined Queue. The queue name and run method should be defined properly in each consumer class.

class OtpSmsConsumer < SmartQue::Consumer
  QUEUE_NAME = 'sms.otp'

  def run(payload)
    # Payload: { number: '+919898123123', message: 'Test Message' }
    # Method implementation goes here
    puts payload
  end
end

Note that, queue name words are separated by . while defining it with consumer class. Consumer can be started by calling start method available for consumer instance as follows

c = OtpSmsConsumer.new
c.start

All consumer processes can be placed in a rake file, so that it can be started individually with rake tasks.

File : lib/tasks/consumer.rake

# Tasks which related to rabbitmq broker.
namespace :rabbitmq do

  desc "Run OTP sms worker"
  task :send_otp_sms => [:environment] do
    c = OtpSmsConsumer.new
    c.start
  end
end

# Task can be initiated by rake
RAILS_ENV=staging bundle exec rake rabbitmq:send_otp_sms

SmartQue Exceptions

SmartQue will not retry automatic connection recovery after rabbitmq connection failure. It raises SmartQue::ConnectionError when tcp connection failure happens. You can catch this exception to identify connection failures and can perform necessary actions upon it.

RabbitMq Web Interface

You can enable rabbitmq_management plugin so that rabbitmq server informations are viewed and managed via web console interface. Management plugin can be enabled by running this command and can be accessed through web browser

rabbitmq-plugins enable rabbitmq_management

More informations are available on RabbitMq management plugin documentations.

Development

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

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/smart_que. 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.

Code of Conduct

Everyone interacting in the SmartQue project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.