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 :(
This commit is contained in:
Michael Klishin 2019-04-25 19:36:20 +03:00
parent d04af19917
commit 802cf615bf
No known key found for this signature in database
GPG Key ID: 2C0DA45F4F944489

View File

@ -19,14 +19,18 @@ class FibonacciServer
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
# 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, payload|
queue.subscribe do |_delivery_info, properties, payload|
result = fibonacci(payload.to_i)
exchange.publish(
@ -49,6 +53,7 @@ begin
puts ' [x] Awaiting RPC requests'
server.start('rpc_queue')
server.loop_forever
rescue Interrupt => _
server.stop
end