I recently wanted a Django-based system to display how many Celery tasks were currently backed up, and also provide a way to clear a queue. Since we were using RabbitMQ as the Celery broker, one option was to build everything directly with Kombu, but there is a simpler approach that avoids writing too much broker-specific code.
To get the number of pending messages in a queue, use code like this. Replace your_app_name and your_queue_name with the real values from your environment:
1 | from celery import Celery |
One thing to note is that this will raise an error if the queue does not exist.
If you want to purge the queue, change queue_declare to queue_purge:
1 | app.connection().channel().queue_purge(your_queue_name) |