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.

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:



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hirefire/macro/bunny.rb', line 38

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