rabbitmq-tutorials/ruby/receive_logs_topic.rb
Michael Klishin 6be7cfebf9
Bunny: warn against subscribe(block: true)
It was never meant to be used outside of tutorials
but unfortunately some do just that.
2019-02-27 04:38:02 +03:00

31 lines
736 B
Ruby

#!/usr/bin/env ruby
require 'bunny'
abort "Usage: #{$PROGRAM_NAME} [binding key]" if ARGV.empty?
connection = Bunny.new(automatically_recover: false)
connection.start
channel = connection.create_channel
exchange = channel.topic('topic_logs')
queue = channel.queue('', exclusive: true)
ARGV.each do |severity|
queue.bind(exchange, routing_key: severity)
end
puts ' [*] Waiting for logs. To exit press CTRL+C'
begin
# block: true is only used to keep the main thread
# alive. Please avoid using it in real world applications.
queue.subscribe(block: true) do |delivery_info, _properties, body|
puts " [x] #{delivery_info.routing_key}:#{body}"
end
rescue Interrupt => _
channel.close
connection.close
exit(0)
end