Class: BunnyBurrow::Server

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

Instance Attribute Summary

Attributes inherited from Base

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from BunnyBurrow::Base

Class Method Details

.create_responseObject



5
6
7
8
9
10
11
# File 'lib/bunny_burrow/server.rb', line 5

def self.create_response
  {
    status: STATUS_OK,
    error_message: nil,
    data: {}
  }
end

Instance Method Details

#shutdownObject



67
68
69
70
# File 'lib/bunny_burrow/server.rb', line 67

def shutdown
  stop_waiting
  super
end

#stop_waitingObject



61
62
63
64
65
# File 'lib/bunny_burrow/server.rb', line 61

def stop_waiting
  return unless @waiting
  process_lock.synchronize { process_condition.signal }
  @waiting = false
end

#subscribe(routing_key, queue_name = routing_key, &block) ⇒ Object



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
45
46
47
48
49
50
51
52
53
54
# File 'lib/bunny_burrow/server.rb', line 13

def subscribe(routing_key, queue_name = routing_key, &block)
  queue = channel.queue(queue_name, exclusive: false, auto_delete: true)
  queue.bind(topic_exchange, routing_key: routing_key)

  details = {
    routing_key: routing_key,
    queue: queue.name,
    exchange: topic_exchange.name
  }

  log "Subscribing #{details}"
  queue.subscribe(manual_ack: true) do |delivery_info, properties, payload|
    begin
      details = {
        delivery_info: delivery_info,
        properties: properties
      }

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

      response = block.call(payload)

      details[:response] = response if log_response?

      log "Replying #{details}"
      default_exchange.publish(response.to_json, :routing_key => properties.reply_to, persistence: false)

      log "Acknowledging #{details}"
      channel.ack delivery_info.delivery_tag
    rescue => e
      log e.message, level: :error
      response = {
        status: STATUS_SERVER_ERROR,
        error_message: e.message
      }
      default_exchange.publish(response.to_json, :routing_key => properties.reply_to, persistence: false)
    end
  end
rescue => e
  log e.message, level: :error
end

#waitObject



56
57
58
59
# File 'lib/bunny_burrow/server.rb', line 56

def wait
  @waiting = true
  process_lock.synchronize { process_condition.wait(process_lock) }
end