Alessandro Da Rugna
Alessandro Da Rugna

Reputation: 4695

Get Celery task name from inside Django

I have a Django 1.5.1 webapp using Celery 3.0.23 with RabbitMQ 3.1.5.
When reading the TaskMeta informations from my backend using

from djcelery.models import TaskMeta
TaskMeta.objects.all()

I cannot read the task name (it is usually the method name). It is not stored in the database. Is there a way to store also the task name or to retrieve it at runtime?

If not, I am already thinking about storing it in the meta properties, but this implies that I manually have to call update_state on every task I create. This looks a bit clumsy to me.

Upvotes: 2

Views: 2295

Answers (1)

Artem Mezhenin
Artem Mezhenin

Reputation: 5757

You can get your task name, by looking into name property of a task, for example:

In [1]: from celery import task

In [2]: @task
   ...: def hello():
   ...:     print hello.name
   ...:     

In [3]: hello()
default.hello

In [4]: hello.name
Out[4]: 'default.hello'

If you wish, you can call that task by name with celery.execute.send_task().

UPD. When you are using RabbitMQ as broker, Celery use celeryev exchange to publish all status updates for tasks. Django will not have any information about tasks, because storing all this information is a huge performance hit. You can connect to celeryev exchange with command:

python manage.py celery events

Or you can flower for same task. Both of them will show you tasks in realtime(not history). You can try to use SQL-database as broker, way be this will help you.

Upvotes: 1

Related Questions