in src/alerter/rabbitmq_admin.py [0:0]
def get_queued_message_count(queue_name: str, config: RabbitMQConfig, retry_limit=10, retry_counter=0) -> int:
"""
requests the current message count for the given queue from the server.
:param queue_name: name of the queue to request
:param config: RabbitMQConfig
:param retry_limit: maximum number of times to retry, defaults to 10
:param retry_counter: current retry counter. Don't specify this when calling
:return: the queued message count. Throws an exception on failure and will retry on transient errors
"""
if retry_counter > retry_limit:
logger.error("Could not succeed after {0} attempts, giving up".format(retry_counter))
raise RuntimeError("Could not contact RabbitMQ and exhausted retries")
dest_url = config.make_url("queues", queue_name)
logger.debug("Making request to {0}".format(dest_url))
response = requests.get(dest_url, auth=config.credentials)
if response.status_code == 200:
content = response.json()
return content["messages_ready"]
elif response.status_code == 503 or response.status_code == 504:
logger.warning("Rabbit MQ is not available at the moment ({0} returned 503). Retrying in a few seconds...")
time.sleep(3)
return get_queued_message_count(queue_name, config, retry_limit, retry_counter+1)
else:
logger.error("Could not contact RabbitMQ at {0}, server returned {1} {2}".format(dest_url, response.status_code, response.text))
raise RuntimeError("RabbitMQ returned an error, see the logs for details")