Class: BunnyBurrow::Client

Inherits:
Base
  • Object
show all
Defined in:
lib/bunny_burrow/client.rb

Constant Summary collapse

DIRECT_REPLY_TO =
'amq.rabbitmq.reply-to'

Instance Attribute Summary

Attributes inherited from Base

#log_prefix, #log_request, #log_response, #logger, #rabbitmq_connection, #rabbitmq_exchange, #rabbitmq_url, #timeout, #verify_peer

Instance Method Summary collapse

Methods inherited from Base

#initialize, #shutdown

Constructor Details

This class inherits a constructor from BunnyBurrow::Base

Instance Method Details

#handle_delivery(details, payload) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/bunny_burrow/client.rb', line 46

def handle_delivery(details, payload)
  details[:response] = payload if log_response?
  log "Receiving #{details}"
  result = payload
  lock.synchronize {condition.signal}
  result
end

#publish(payload, routing_key) ⇒ Object



10
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_burrow/client.rb', line 10

def publish(payload, routing_key)
  result = nil

  details = {
    routing_key: routing_key,
    reply_to: DIRECT_REPLY_TO
  }

  details[:request] = payload if log_request?
  log "Publishing #{details}"

  options = {
    routing_key: routing_key,
    reply_to: DIRECT_REPLY_TO,
    persistence: false
  }

  consumer = Bunny::Consumer.new(channel, DIRECT_REPLY_TO, SecureRandom.uuid)
  consumer.on_delivery do |_, _, received_payload|
    result = handle_delivery(details, received_payload)
  end


  begin
    channel.basic_consume_with consumer
    topic_exchange.publish(payload.to_json, options)

    Timeout.timeout(timeout) do
      lock.synchronize {condition.wait(lock)}
    end
  ensure
    consumer.cancel
  end
  result
end