Class: Warren::Queue::BunnyAdapter

Inherits:
Warren::Queue show all
Defined in:
lib/warren/adapters/bunny_adapter.rb

Constant Summary

Constants inherited from Warren::Queue

InvalidAdapter, NoAdapterSet, NoBlockGiven, NoConnectionDetails

Class Method Summary collapse

Methods inherited from Warren::Queue

adapter, adapter=, connection, connection=, inherited, logger, logger=

Class Method Details

.check_connection_details(opts) ⇒ Object

Checks the connection details are correct for this adapter



12
13
14
15
16
17
18
19
20
# File 'lib/warren/adapters/bunny_adapter.rb', line 12

def self.check_connection_details opts
  # Check they've passed in the stuff without a default on it
  [:user, :pass, :vhost].each do | required_arg |
    unless opts.has_key?(required_arg)
      raise Warren::Connection::InvalidConnectionDetails, "#{required_arg.to_s.capitalize} not specified"
    end
  end
  true
end

.publish(queue_name, payload, &blk) ⇒ Object

Sends a message to a queue. If successfully sent it returns true, unless callback block is passed (see below)

Warren::Queue.publish(:queue_name, {:foo => "name"})

Can also pass a block which is fired after the message is sent. If a block is passed, then the return value of the block is returned from this method.

Warren::Queue.publish(:queue_name, {:foo => "name"}) { puts "foo" }


45
46
47
48
49
50
51
52
53
54
# File 'lib/warren/adapters/bunny_adapter.rb', line 45

def self.publish queue_name, payload, &blk
  queue_name = self.queue_name if queue_name == :default
  # Create a message object if it isn't one already
  msg = Warren::MessageFilter.pack(payload)

  do_connect(queue_name, blk) do |queue|
    queue.publish msg.to_s
  end

end

.queue_nameObject

Returns the default queue name or returns InvalidConnectionDetails if no default queue is defined



26
27
28
29
30
31
# File 'lib/warren/adapters/bunny_adapter.rb', line 26

def self.queue_name
  unless self.connection.options.has_key?(:default_queue)
    raise Warren::Connection::InvalidConnectionDetails, "Missing a default queue name."
  end
  self.connection.options[:default_queue]
end

.subscribe(queue_name, &block) ⇒ Object

Subscribes to a queue and runs the block for each message received

Warren::Queue.subscribe("example") {|msg| puts msg }

Expects a block and raises NoBlockGiven if no block is given.

The block is passed up to two arguments, depending on how many you ask for. The first one is always required, which is the message passed over the queue (after being unpacked by filters.) The (optional) second argument is a hash of headers bunny gives us containing extra data about the message.

Warren::Queue.subscribe(:default) {|msg, payload| puts msg, payload }

Raises:



72
73
74
75
76
77
78
79
# File 'lib/warren/adapters/bunny_adapter.rb', line 72

def self.subscribe queue_name, &block
  raise NoBlockGiven unless block_given?
  queue_name = self.queue_name if queue_name == :default
  # todo: check if its a valid queue?
  do_connect(queue_name) do |queue|
    handle_bunny_message(queue.pop, &block)
  end
end