rabbitmq-tutorials/ruby/rpc_server.rb
Michael Klishin 802cf615bf
Ruby tutorial 6: avoid using block: true
It makes reasoning about connection recovery
pretty difficult. The option only exists to make
tutorials shorter but sadly some users go straight
into production with copy-pasted code :(
2019-04-25 19:36:20 +03:00

60 lines
1.1 KiB
Ruby

#!/usr/bin/env ruby
require 'bunny'
class FibonacciServer
def initialize
@connection = Bunny.new
@connection.start
@channel = @connection.create_channel
end
def start(queue_name)
@queue = channel.queue(queue_name)
@exchange = channel.default_exchange
subscribe_to_queue
end
def stop
channel.close
connection.close
end
def loop_forever
# This loop only exists to keep the main thread
# alive. Many real world apps won't need this.
loop { sleep 5 }
end
private
attr_reader :channel, :exchange, :queue, :connection
def subscribe_to_queue
queue.subscribe do |_delivery_info, properties, payload|
result = fibonacci(payload.to_i)
exchange.publish(
result.to_s,
routing_key: properties.reply_to,
correlation_id: properties.correlation_id
)
end
end
def fibonacci(value)
return value if value.zero? || value == 1
fibonacci(value - 1) + fibonacci(value - 2)
end
end
begin
server = FibonacciServer.new
puts ' [x] Awaiting RPC requests'
server.start('rpc_queue')
server.loop_forever
rescue Interrupt => _
server.stop
end