Module: HireFire::Macro::Bunny

Extended by:
Errors::JobQueueLatencyUnsupported, Bunny, Deprecated::Bunny, Utility
Included in:
Bunny
Defined in:
lib/hirefire/macro/bunny.rb

Defined Under Namespace

Classes: ConnectionError

Instance Method Summary collapse

Methods included from Deprecated::Bunny

queue

Methods included from Errors::JobQueueLatencyUnsupported

job_queue_latency

Instance Method Details

#job_queue_size(*queues, amqp_url: nil) ⇒ Integer

Note:

It’s important to separate jobs scheduled for future execution into a different queue from the regular queue. This is because including them in the regular queue can interfere with the accurate counting of jobs that are currently scheduled to run, leading to premature upscaling. If you want to be able to schedule jobs to run in the future, consider using the Delayed Message Plugin for RabbitMQ.

Note:

The method relies on the ‘message_count` metric to determine the number of “Ready” messages in the queue. When using auto-acknowledgment, messages are acknowledged immediately upon delivery, causing the `message_count` to drop to zero, even if the consumer is processing messages. To ensure accurate metrics:

  • Enable manual acknowledgment (‘manual_ack: true`) so that RabbitMQ tracks unacknowledged messages.

  • Set a reasonable prefetch limit (‘channel.prefetch(x)`) to control the number of messages delivered to the consumer, allowing a measurable backlog to remain in the “Ready” state.

This configuration ensures accurate scaling metrics and prevents premature depletion of the queue.

Calculates the total job queue size using Bunny.

If an ‘amqp_url` is not provided, the method attempts to establish a connection using a hierarchy of environment variables for the RabbitMQ URL. It checks the following environment variables in order: `AMQP_URL`, `RABBITMQ_URL`, `RABBITMQ_BIGWIG_URL`, `CLOUDAMQP_URL`. If none of these variables are set, it defaults to a local RabbitMQ instance at “amqp://guest:guest@localhost:5672”.

Examples:

Retrieve job queue size for the “default” queue

HireFire::Macro::Bunny.job_queue_size(:default)

Retrieve job queue size across “default” and “mailer” queues

HireFire::Macro::Bunny.job_queue_size(:default, :mailer)

Use a new connection on each call using a AMQP URL

HireFire::Macro::Bunny.job_queue_size(:default, amqp_url: url)

Parameters:

  • queues (Array<String, Symbol>)

    Names of the queues for size measurement.

  • amqp_url (String, nil) (defaults to: nil)

    (optional) RabbitMQ URL for establishing a new connection.

Returns:

  • (Integer)

    Total job queue size.

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hirefire/macro/bunny.rb', line 48

def job_queue_size(*queues, amqp_url: nil)
  require "bunny"

  queues = normalize_queues(queues, allow_empty: false)
  channel, connection = setup_channel(amqp_url)

  begin
    queues.sum { |name| channel.queue(name, passive: true).message_count }
  ensure
    channel&.close
    connection&.close
  end
end