Просмотр исходного кода

Added task_status view, returning the current status and result of the task in JSON format.

Ask Solem 16 лет назад
Родитель
Сommit
baf13dea36
2 измененных файлов с 18 добавлено и 0 удалено
  1. 2 0
      celery/urls.py
  2. 16 0
      celery/views.py

+ 2 - 0
celery/urls.py

@@ -5,4 +5,6 @@ from celery import views
 urlpatterns = patterns("",
     url(r'^(?P<task_id>[\w\d\-]+)/done/?$', views.is_task_done,
         name="celery-is_task_done"),
+    url(r'^(?P<task_id>[\w\d\-]+)/status/?$', views.task_status,
+        name="celery-task_status"),
 )

+ 16 - 0
celery/views.py

@@ -1,8 +1,24 @@
+"""celery.views"""
 from django.http import Http404, HttpResponse
 from celery.task import is_done
+from celery.result import AsyncResult
 import simplejson
 
 
 def is_task_done(request, task_id):
+    """Returns task execute status in JSON format."""
     response_data = {"task": {"id": task_id, "executed": is_done(task_id)}}
     return HttpResponse(simplejson.dumps(response_data))
+
+
+def task_status(request, task_id):
+    """Returns task status and result in JSON format."""
+    async_result = AsyncResult(task_id)
+    response_data = {"task": {
+                        "id": task_id,
+                        "status": async_result.get_status(),
+                        "result": async_result.get_result(),
+    }}
+    return HttpResponse(simplejson.dumps(response_data))
+
+