Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to query Celery using AJAX from front-end to know if a created task is completed or not?

I am using Django with Celery + RabbitMQ to create video conversion tasks of videos being uploaded by the users. Now I know how to query celery to get the status. My question is where to save the task_id associated with each task, should I save it in models or in django's cache?

I know that similar questions have been asked in the past e.g. this stackoverflow question but there has been no definitive answer yet. I know the answer to this varies per developer's preferences but if someone can educate on pros/cons of different approaches, that will be great.

Just to re-iterate I will use the task_id to fire AJAX queries every so often from the front-end to know if the video conversion is completed or not.

like image 791
Chantz Avatar asked Dec 17 '22 17:12

Chantz


2 Answers

If you have django_celery installed, you can query views like task_status or is_task_successful that come in the djcelery.views package. They return a JSON dictionary so you can handle them with JavaScript.

like image 194
Camilo Díaz Repka Avatar answered Jan 06 '23 09:01

Camilo Díaz Repka


Refer to my answer on your linked question: Test if a celery task is still being processed

So after setting the celery_task field, you could easily define a ajax view which would return the task status in a required format.

like image 31
crodjer Avatar answered Jan 06 '23 09:01

crodjer