Last active
August 29, 2015 14:00
-
-
Save Zhomart/11389716 to your computer and use it in GitHub Desktop.
Consumer#cancel doesn't work inside subscribe block
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
| require 'bunny' | |
| require 'json' | |
| puts Bunny::VERSION # => 1.2.1 | |
| bunny_config = { | |
| log_level: :debug | |
| } | |
| connection = Bunny.new bunny_config | |
| connection.start | |
| request_id = rand(36**32).to_s(36) | |
| request = { | |
| id: request_id, | |
| data: { | |
| words: ['goat', 'hoax', 'jelly', 'kitty', 'post'], | |
| pair: ['jelly', 'kitty'] | |
| } | |
| } | |
| routing_key = 'pair_it' | |
| exchane_name = 'gothic.exchanges.direct.ci' | |
| result_channel = connection.create_channel | |
| request_channel = connection.create_channel | |
| result_exchange = result_channel.direct(exchane_name) | |
| request_exchange = request_channel.direct(exchane_name) | |
| # === Sending request | |
| puts "Trying to send to '#{exchane_name}': #{request.inspect[0..120]} by #{routing_key}" | |
| request_exchange.publish(JSON.generate(request), :key => routing_key) | |
| puts "Sent to #{routing_key} | #{request_exchange.name}" | |
| # === Handling result | |
| finished = false | |
| queue_result = result_channel.queue("", auto_delete: true, exclusive: true, durable: false) | |
| queue_result.bind(result_exchange, routing_key: request_id) | |
| result_consumer = queue_result.subscribe do |info, meta, payload| | |
| response = JSON.parse(payload) | |
| puts "Got: #{payload}\nfor request: #{request_id}" | |
| if response['request_id'].to_s != request_id.to_s | |
| raise "NOOO #{request[:id]} != #{response['request_id']}" | |
| end | |
| puts "canceling result consumer: #{result_consumer}" # => is shown | |
| puts result_consumer.cancel.to_s | |
| puts "finished" # => UNATTAINABLE!!! | |
| finished = true | |
| end | |
| result_consumer.on_cancellation do | |
| puts "on_cancellation has been called" | |
| end | |
| puts "sent: #{result_consumer}" # => is shown | |
| # infite loop | |
| sleep 100 while not finished | |
| puts "Finished" | |
| result_channel.close | |
| request_channel.close | |
| connection.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment