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
-
#job_queue_size(*queues, amqp_url: nil) ⇒ Integer
Calculates the total job queue size using Bunny.
Methods included from Deprecated::Bunny
Methods included from Errors::JobQueueLatencyUnsupported
Instance Method Details
#job_queue_size(*queues, amqp_url: nil) ⇒ Integer
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.
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”.
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). } ensure channel&.close connection&.close end end |