Class: BunnyHop
- Inherits:
-
Object
- Object
- BunnyHop
- Defined in:
- lib/bunny_hop.rb,
lib/bunny_hop/cli.rb,
lib/bunny_hop/version.rb
Defined Under Namespace
Modules: ControllerHelper Classes: CLI
Constant Summary collapse
- VERSION =
"0.1.4"
Instance Method Summary collapse
-
#initialize ⇒ BunnyHop
constructor
A new instance of BunnyHop.
- #publish(queue, msg) ⇒ Object
- #subscribe(queue) ⇒ Object
Constructor Details
#initialize ⇒ BunnyHop
Returns a new instance of BunnyHop.
5 6 7 8 9 |
# File 'lib/bunny_hop.rb', line 5 def initialize @connection = ::Bunny.new(host: ENV.fetch('RABBITMQ_HOST', 'rabbitmq'), user: ENV.fetch('RABBITMQ_USER', 'rabbitmq'), password: ENV.fetch('RABBITMQ_PASSWORD', 'rabbitmq')) @connection.start @channel = @connection.create_channel end |
Instance Method Details
#publish(queue, msg) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bunny_hop.rb', line 11 def publish(queue, msg) lock = Mutex.new condition = ConditionVariable.new response = nil call_id = "#{rand}#{rand}#{rand}" exchange = @channel.default_exchange reply_queue = @channel.queue('', exclusive: true) reply_queue.subscribe do |_delivery_info, properties, payload| if properties[:correlation_id] == call_id response = payload lock.synchronize { condition.signal } end end exchange.publish(msg, routing_key: queue, correlation_id: call_id, reply_to: reply_queue.name) Timeout::timeout(5) { lock.synchronize { condition.wait(lock) } } @channel.close @connection.close response rescue Timeout::Error => e @channel.close @connection.close raise e end |
#subscribe(queue) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bunny_hop.rb', line 46 def subscribe(queue) queue = @channel.queue(queue) exchange = @channel.default_exchange queue.subscribe do |_delivery_info, properties, payload| result = yield(payload) exchange.publish( result.to_s, routing_key: properties.reply_to, correlation_id: properties.correlation_id ) end end |