Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to query state of a celery tasks using django-celery-results during the execution of a task?

I am using Celery + RabbitMQ for queuing tasks in my Django App,

I want to track the state of a task using the task_id and the task_state.

For that i created a TaskModel(Model) to store the task_id, task_state and some additional data in the database. On task execution, a new TaskModel object is save and updated as the task progresses. Everything is working fine.

However, i still need to add a lot of functionality and features and error protections etc. That's when i remembered the celery documentation mentions the django-celery-results.

So i followed the django-celery-results documentation instructions. Tasks results get stored in the default django database in a dedicated table, However only after the task concludes... and not during the PENDING, STARTED states.

Is it possible to use django-celery-results to store and query tasks during the PENDING and STARTED states? or not?

Thanks

like image 867
Ouss Avatar asked Jan 30 '17 23:01

Ouss


1 Answers

After reviewing the source code of django-celery-result it turns out the code is pretty simple and straight-forward.

In order to use django-celery-result to store tasks after the task function is called use the following:

from django_celery_results.models import TaskResult
import json

@shared_task(bind=True)
def foo(self, count):
 print('hello')
 task_result = TaskResult.objects.get(self.request.id)
 counter = 1
 interval = 20 #Limit updates to reduce database queries
 interval_count = count/interval
 for i in range(count):
  print(i)
  if counter>= interval_count:
   interval_count+=count/interval
   task_result.meta = json.dumps({'progress': counter/count})
   task_result.save()
  counter+=1
 task_result.save()
 return

def goo()
 task = foo.delay(1000)
 task_result = TaskResult(task_id=task.task_id)
 task_result.save()
like image 172
Ouss Avatar answered Oct 06 '22 14:10

Ouss