Class: BmcDaemonLib::MqConsumer

Inherits:
MqEndpoint show all
Includes:
LoggerHelper
Defined in:
lib/bmc-daemon-lib/mq_consumer.rb

Instance Attribute Summary

Attributes included from LoggerHelper

#logger

Attributes inherited from MqEndpoint

#logger

Instance Method Summary collapse

Methods inherited from MqEndpoint

#initialize

Constructor Details

This class inherits a constructor from BmcDaemonLib::MqEndpoint

Instance Method Details

#listen_to(topic, rkey) ⇒ Object



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
55
# File 'lib/bmc-daemon-lib/mq_consumer.rb', line 30

def listen_to topic, rkey
  log_info "listen_to [#{topic}] [#{rkey}] > [#{@queue.name}]"

  # Ensure a queue has been successfully subscribed first
  unless @queue
    error = "listen_to: no active queue (call subscribe_to_queue beforehand)"
    log_error error
    raise MqConsumerException, error
  end

  # Bind on topic exchange, if exists
  @queue.bind topic, routing_key: rkey

  # Handle errors
  rescue Bunny::NotFound => e
    log_debug "missing exchange [#{topic}]"
    #@channel.topic topic, durable: false, auto_delete: true
    raise MqConsumerTopicNotFound, e.message

  rescue StandardError => e
    log_error "UNEXPECTED: #{e.inspect}"
    raise MqConsumerException, e.message

  else
  #   log_debug "bound queue [#{@queue.name}] on topic [#{topic}]"
end

#subscribe_to_queue(name, context = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bmc-daemon-lib/mq_consumer.rb', line 11

def subscribe_to_queue name, context = nil
  log_debug "subscribe_to_queue [#{name}]"

  # Ensure a channel has been successfully opened first
  unless @channel
    error = "subscribe_to_queue: no active channel (call subscribe_to_queue beforehand)"
    log_error error
    raise MqConsumerException, error
  end

  # Queue for this rule
  @queue = @channel.queue(name, auto_delete: false, durable: true)

  # Create consumer on this queue
  @queue.subscribe(manual_ack: AMQP_MANUAL_ACK, on_cancellation: :consumer_cancelled) do |delivery_info, , payload|
    handle_receive context, delivery_info, , payload
  end
end