You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
623 B

  1. from celery.result import AsyncResult
  2. from rest_framework.permissions import IsAuthenticated
  3. from rest_framework.response import Response
  4. from rest_framework.views import APIView
  5. class TaskStatus(APIView):
  6. permission_classes = (IsAuthenticated,)
  7. def get(self, request, *args, **kwargs):
  8. task = AsyncResult(kwargs['task_id'])
  9. ready = task.ready()
  10. error = ready and not task.successful()
  11. return Response({
  12. 'ready': ready,
  13. 'result': task.result if ready and not error else None,
  14. 'error': {'text': str(task.result)} if error else None,
  15. })