forked from globocom/GloboNetworkAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
41 lines (33 loc) · 1.38 KB
/
views.py
File metadata and controls
41 lines (33 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
from rest_framework import status
from rest_framework.response import Response
from networkapi import celery_app
from networkapi.util.classes import CustomAPIView
class TaskView(CustomAPIView):
def get(self, request, *args, **kwargs):
"""
Query Celery for task status based on its taskid/jobid
Celery status can be one of:
PENDING - Job not yet run or unknown status
PROGRESS - Job is currently running
SUCCESS - Job completed successfully
FAILURE - Job failed
REVOKED - Job get canceled
"""
task_id = kwargs.get('task_id')
task = celery_app.AsyncResult(task_id)
if task.state == 'PENDING':
payload = dict(task_id=task.id, status=task.state)
elif task.state == 'SUCCESS':
payload = dict(task_id=task.id, status=task.state,
result=task.result)
elif task.state == 'PROGRESS':
payload = dict(task_id=task.id, status=task.state,
result=task.result)
elif task.state == 'FAILURE':
payload = dict(task_id=task.id, status=task.state,
result=task.result)
else:
payload = dict(task_id=task.id, status=task.state,
result=task.result)
return Response(payload, status=status.HTTP_200_OK)