Picking celery

I am using python celery + rabbitmq. I cannot find a way to get the task score in some queue. Something like that:

celery.queue('myqueue').count()

Can I get a task score from a specific queue?

One solution is to run an external command from my python script:

"rabbitmqctl list_queues -p my_vhost"

and analyze the results, is this a good way to do this?

+5
source share
1 answer

I believe that using the rabbitmqctl command is not a good solution, especially on my ubuntu server, where rabbitmqctl can only be run with root privileges.

Playing with pika objects, I found a working solution:

import pika
from django.conf import settings 

def tasks_count(queue_name):
    ''' Connects to message queue using django settings and returns count of messages in queue with name queue_name. '''
    credentials = pika.PlainCredentials(settings.BROKER_USER, settings.BROKER_PASSWORD)
    parameters = pika.ConnectionParameters( credentials=credentials,
                                           host=settings.BROKER_HOST,
                                           port=settings.BROKER_PORT,
                                           virtual_host=settings.BROKER_VHOST)
    connection = pika.BlockingConnection(parameters=parameters)
    channel = connection.channel()
    queue = channel.queue_declare(queue=queue_name, durable=True)
    message_count = queue.method.message_count
    return message_count

I did not find documentation on inspecting the AMQP queue using pika, so I do not know about the correct solution.

+5
source

All Articles