11

How can I test if a task (task_id) is still processed in celery? I have the following scenario:

  1. Start a task in a Django view
  2. Store the BaseAsyncResult in the session
  3. Shutdown the celery daemon (hard) so the task is not processed anymore
  4. Check if the task is 'dead'

Any ideas? Can a lookup all task being processed by celery and check if mine is still there?

Reto Aebersold
  • 16,306
  • 5
  • 55
  • 74
  • 1
    Hey I am also looking for something similar to this, did you ever get around to resolving this? I am wondering whether to store the task_id in django's cache framework instead. I know I can use something like this http://dpaste.com/370419/ to get the status of the task. But am confused between using database, cache to store task_id. – Chantz Jan 30 '11 at 23:18

2 Answers2

3

define a field (PickledObjectField) in your model to store the celery task:

class YourModel(models.Model):
    .
    .
    celery_task = PickledObjectField()
    .
    .

    def task():
        self.celery_task = SubmitTask.apply_async(args = self.task_detail())
        self.save()

In case your task is not specific on any model you should create one specifically for the celery tasks.

or else I suggest using django-celery. It has a nice monitoring feature:
http://ask.github.com/celery/userguide/monitoring.html#django-admin-monitor, saves the tasks details in a django model in a nice graphical way.

crodjer
  • 13,384
  • 9
  • 38
  • 52
0

I think there is a better way than to store a task object in the model. For example, in case you wanted to check if a group of task (parallel) have completed:

# in the script you launch the task
from celery import group

job = group(
    task1.s(param1, param2),
    task2.s(param3, param4)
)
result = job.apply_async()
result.save()

# save the result ID in your model
your_obj_model = YourModel.objects.get(id='1234')
your_obj_model.task_id = result.id
your_obj_model.save()

Then in your view

from celery.result import GroupResult
# ...
task_result = GroupResult.restore(your_obj_model.task_id)
task_finished = task_result.ready()
# will be True or False
Leonardo
  • 4,046
  • 5
  • 44
  • 85