Skip to content

Instantly share code, notes, and snippets.

@michaeldever
Last active December 5, 2017 17:40
Show Gist options
  • Save michaeldever/3345831da12e290bae0ba85e48a8b7d1 to your computer and use it in GitHub Desktop.
Save michaeldever/3345831da12e290bae0ba85e48a8b7d1 to your computer and use it in GitHub Desktop.
Rabbit MQ Helper
module RabbitMqHelper
def handle_subscription
connection = Bunny.new(ENV.fetch('MQ_URL'))
connection.start
channel = connection.create_channel
exchange = channel.topic(ENV.fetch('MQ_EXCHANGE'), durable: true)
queue = channel.queue(ENV.fetch('MQ_QUEUE'), durable: true)
.bind(exchange, routing_key: ENV.fetch('MQ_ROUTING_KEY'))
subscribe_to_queue(channel, queue)
end
def subscribe_to_queue(channel, queue)
queue.subscribe(block: true, manual_ack: true) do |delivery_info, _properties, payload|
channel.ack(delivery_info.delivery_tag) unless Rails.env.development?
begin
payload = JSON.parse(payload)
handle_payload(payload)
rescue StandardError => e
puts "Error processing: #{e.message}"
puts e.backtrace
end
end
end
def handle_payload(payload)
# handle payload
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment