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



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

def self.check_connection_details opts
  # Check they've passed in the stuff without a default on it
  unless opts.has_key?(:user) && opts.has_key?(:pass) && opts.has_key?(:vhost)
    raise Warren::Connection::InvalidConnectionDetails, "Missing a username, password or vhost."
  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" }


42
43
44
45
46
47
48
49
50
51
# File 'lib/warren/adapters/bunny_adapter.rb', line 42

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



23
24
25
26
27
28
# File 'lib/warren/adapters/bunny_adapter.rb', line 23

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.

Raises:



61
62
63
64
65
66
67
68
69
70
# File 'lib/warren/adapters/bunny_adapter.rb', line 61

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|
    msg = queue.pop
    return if msg == :queue_empty
    block.call(Warren::MessageFilter.unpack(msg))
  end
end