forked from fossasia/open-event-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcelery_tasks.py
More file actions
39 lines (34 loc) · 1.19 KB
/
celery_tasks.py
File metadata and controls
39 lines (34 loc) · 1.19 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
"""
This API is meant to store the Task Result for a celery task
and used for purposes such as polling
"""
from celery.result import AsyncResult
from flask import Blueprint, current_app, jsonify
from app.api.helpers.utilities import TASK_RESULTS
celery_routes = Blueprint('tasks', __name__, url_prefix='/v1')
@celery_routes.route('/tasks/<string:task_id>')
def celery_task(task_id):
"""
Get CeleryTask status of an API based task
"""
# in case of always eager, get results. don't call AsyncResult
# which rather looks in redis
if current_app.config.get('CELERY_ALWAYS_EAGER'):
state = TASK_RESULTS[task_id]['state']
info = TASK_RESULTS[task_id]['result']
else:
from app.api.helpers.tasks import celery
result = AsyncResult(id=task_id, app=celery)
state = result.state
info = result.info
# check
if state == 'SUCCESS':
if type(info) is dict:
# check if is error
if '__error' in info:
return info['result']
# return normal
return jsonify(state='SUCCESS', result=info)
if state == 'FAILURE':
return jsonify(state=state)
return jsonify(state=state)