Last active
December 5, 2017 17:40
-
-
Save michaeldever/3345831da12e290bae0ba85e48a8b7d1 to your computer and use it in GitHub Desktop.
Rabbit MQ Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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