Class: Huck::Receivers::RabbitMQReceiver

Inherits:
Huck::Receiver show all
Defined in:
lib/huck/receivers/rabbitmq.rb

Overview

A receiver that talks to RabbitMQ

Instance Attribute Summary

Attributes inherited from Huck::Receiver

#config

Instance Method Summary collapse

Methods inherited from Huck::Receiver

#accept, factory

Constructor Details

#initializeRabbitMQReceiver

Includes all required modules for the RabbitMQ receiver



9
10
11
# File 'lib/huck/receivers/rabbitmq.rb', line 9

def initialize
  Huck::must_load 'bunny'
end

Instance Method Details

#receiveObject

A long-running poller process which reads messages out of the remote queue and yields them to higher-order logic.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/huck/receivers/rabbitmq.rb', line 26

def receive
  verify_config

  options = Hash.new
  [:host, :port, :user, :pass, :vhost].each do |arg|
    if @config['rabbitmq'].has_key? arg.to_s
      options[arg] = @config['rabbitmq'][arg.to_s]
    end
  end

  conn = Bunny.new options
  conn.start

  ch = conn.create_channel
  queue = ch.queue(@config['rabbitmq']['queue_name'])

  queue.subscribe :block => true do |_, _, msg|
    yield msg
  end
end

#verify_configObject

Ensures that configuration is set properly before trying to use the connection data to talk to RabbitMQ



15
16
17
18
19
20
21
22
# File 'lib/huck/receivers/rabbitmq.rb', line 15

def verify_config
  if !@config.has_key? 'rabbitmq'
    raise Huck::Error, 'missing rabbitmq config'
  end
  if !@config['rabbitmq'].has_key? 'queue_name'
    raise Huck::Error, 'missing rabbitmq receiver config: queue_name'
  end
end