10

I have a Django project on an Ubuntu EC2 node, which I have been using to set up an asynchronous using Celery.

I am following http://michal.karzynski.pl/blog/2014/05/18/setting-up-an-asynchronous-task-queue-for-django-using-celery-redis/ along with the docs.

I've been able to get a basic task working at the command line, using:

(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ celery --app=myproject.celery:app worker --loglevel=INFO

I just realized, that I have a bunch of tasks in my queue, that had not executed:

[2015-03-28 16:49:05,916: WARNING/MainProcess] Restoring 4 unacknowledged message(s).
(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ celery -A tp purge
WARNING: This will remove all tasks from queue: celery.
         There is no undo for this operation!

(to skip this prompt use the -f option)

Are you sure you want to delete all tasks (yes/NO)? yes
Purged 81 messages from 1 known task queue.

How do I get a list of the queued items from the command line?

user1592380
  • 34,265
  • 92
  • 284
  • 515

2 Answers2

23

If you want to get all scheduled tasks,

celery inspect scheduled

To find all active queues

celery inspect active_queues

For status

celery inspect stats

For all commands

celery inspect

If you want to get it explicitily.Since you are using redis as queue.Then

redis-cli

>KEYS * #find all keys

Then find out something related to celery

>LLEN KEY # i think it gives length of list
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • Thank you! I'm getting an error with this though: http://stackoverflow.com/questions/29331280/connection-refused-with-celery – user1592380 Mar 29 '15 at 15:53
  • I have a follow up at http://stackoverflow.com/questions/29377944/trouble-installing-supervisord-with-celery, if you are bored. – user1592380 Mar 31 '15 at 20:55
  • Another follow up at http://stackoverflow.com/questions/29402447/how-to-set-celeryconfig-file-in-in-django – user1592380 Apr 02 '15 at 01:07
  • This is useful information, but doesn't really answer the question. I'm, too, interested in listing all task in the queue. Reading man pages and --help from celery, I've found those commands, but they only list tasks either scheduled, active or reserved. If you consider non-scheduled tasks (eta/count), the bunch of tasks are just waiting in the queue. The redis stuff is kinda hacky, and doesn't provide any information beside the number of such tasks. Is that just not possible with celery ??? – orzel May 12 '17 at 10:40
14

Here is a copy-paste solution for Redis:

def get_celery_queue_len(queue_name):
    from yourproject.celery import app as celery_app
    with celery_app.pool.acquire(block=True) as conn:
        return conn.default_channel.client.llen(queue_name)


def get_celery_queue_items(queue_name):
    import base64
    import json
    from yourproject.celery import app as celery_app

    with celery_app.pool.acquire(block=True) as conn:
        tasks = conn.default_channel.client.lrange(queue_name, 0, -1)

    decoded_tasks = []

    for task in tasks:
        j = json.loads(task)
        body = json.loads(base64.b64decode(j['body']))
        decoded_tasks.append(body)

    return decoded_tasks

It works with Django. Just don't forget to change yourproject.celery.

Max Malysh
  • 29,384
  • 19
  • 111
  • 115