rabbitmq-tutorials/ruby/worker.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

26 lines
680 B
Ruby

#!/usr/bin/env ruby
require 'bunny'
connection = Bunny.new(automatically_recover: false)
connection.start
channel = connection.create_channel
queue = channel.queue('task_queue', durable: true)
channel.prefetch(1)
puts ' [*] Waiting for messages. 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(manual_ack: true, block: true) do |delivery_info, _properties, body|
puts " [x] Received '#{body}'"
# imitate some work
sleep body.count('.')
puts ' [x] Done'
channel.ack(delivery_info.delivery_tag)
end
rescue Interrupt => _
connection.close
end