Module: HireFire::Macro::Deprecated::Bunny

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

Overview

Provides backward compatibility with the deprecated Bunny macro. For new implementations, refer to Bunny.

Instance Method Summary collapse

Instance Method Details

#queue(*queues) ⇒ Integer

Retrieves the total number of jobs in the specified queue(s).

This method allows querying multiple queues and supports both existing and new RabbitMQ connections. By default, queues are considered durable unless specified otherwise.

Examples:

Querying the default queue using an existing RabbitMQ connection

HireFire::Macro::Bunny.queue("default", connection: connection)

Querying the default queue using a new RabbitMQ connection

HireFire::Macro::Bunny.queue("default", amqp_url: url)

Querying the “default” and “critical” non-durable queues

HireFire::Macro::Bunny.queue("default", "critical", amqp_url: url, durable: false)

Querying a priority queue

HireFire::Macro::Bunny.queue("priority_queue", connection: connection, "x-max-priority": 10)

Parameters:

  • queues (Array<String, Symbol>)

    Queue names to query. The last argument can be a Hash with either :connection or :amqp_url.

Options Hash (*queues):

  • :connection (Bunny::Session, nil)

    An existing RabbitMQ connection.

  • :amqp_url (String, nil)

    RabbitMQ URL for initializing a new connection.

  • :durable (Boolean) — default: true

    Set to false for non-durable queues.

  • :"x-max-priority" (Integer, nil) — default: nil

    The maximum priority level for the queue. If specified, it overrides the default priority settings for the queue.

Returns:

  • (Integer)

    Total number of jobs in the specified queues.

Raises:

  • (ArgumentError)

    Raises an error if neither :connection nor :amqp_url are provided.



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
# File 'lib/hirefire/macro/deprecated/bunny.rb', line 31

def queue(*queues)
  require "bunny"

  queues.flatten!
  options = queues.last.is_a?(Hash) ? queues.pop : {}
  options[:durable] = true if options[:durable].nil?

  if options[:connection]
    connection = options[:connection]
    channel = nil
    begin
      channel = connection.create_channel
      Private.count_messages(channel, queues, options)
    ensure
      channel&.close
    end
  elsif options[:amqp_url]
    connection = ::Bunny.new(options[:amqp_url])
    begin
      connection.start
      channel = connection.create_channel
      Private.count_messages(channel, queues, options)
    ensure
      channel&.close
      connection.close
    end
  else
    raise ArgumentError, "Must pass either :connection => rabbitmq_connection or :amqp_url => url." \
                         "For example: HireFire::Macro::Bunny.queue(\"queue1\", connection: rabbitmq_connection)"
  end
end