Module: HireFire::Macro::Bunny

Extended by:
Bunny
Included in:
Bunny
Defined in:
lib/hirefire/macro/bunny.rb

Instance Method Summary collapse

Instance Method Details

#count_messages(channel, queue_names, options) ⇒ Object



71
72
73
74
75
76
# File 'lib/hirefire/macro/bunny.rb', line 71

def count_messages(channel, queue_names, options)
  queue_names.inject(0) do |sum, queue_name|
    queue = channel.queue(queue_name, :durable => options[:durable])
    sum + queue.message_count
  end
end

#queue(*queues) ⇒ Integer

Returns the job quantity for the provided queue(s).

Examples:

Bunny Macro Usage


# all queues using existing RabbitMQ connection.
HireFire::Macro::Bunny.queue("queue1", "queue2", :connection => connection)

# all queues using new RabbitMQ connection.
HireFire::Macro::Bunny.queue("queue1", "queue2", :amqp_url => url)

# all non-durable queues using new RabbitMQ connection.
HireFire::Macro::Bunny.queue("queue1", "queue2", :amqp_url => url, :durable => false)

Parameters:

  • queues (Array)

    provide one or more queue names, or none for “all”. Last argument can pass in a Hash containing :connection => rabbitmq_connection or :amqp => :rabbitmq_url

Returns:

  • (Integer)

    the number of jobs in the queue(s).



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hirefire/macro/bunny.rb', line 25

def queue(*queues)
  require "bunny"

  queues.flatten!

  if queues.last.is_a?(Hash)
    options = queues.pop
  else
    options = {}
  end

  if options[:durable].nil?
    options[:durable] = true
  end

  if options[:connection]
    connection = options[:connection]

    channel = nil
    begin
      channel = connection.create_channel
      return count_messages(channel, queues, options)
    ensure
      if channel
        channel.close
      end
    end
  elsif options[:amqp_url]
    connection = ::Bunny.new(options[:amqp_url])
    begin
      connection.start
      channel = connection.create_channel
      return count_messages(channel, queues, options)
    ensure
      if channel
        channel.close
      end

      connection.close
    end
  else
    raise %{Must pass in :connection => rabbitmq_connection or :amqp_url => url\n} +
            %{For example: HireFire::Macro::Bunny.queue("queue1", :connection => rabbitmq_connection}
  end
end