-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
44 lines (35 loc) · 1.37 KB
/
Copy pathapp.py
File metadata and controls
44 lines (35 loc) · 1.37 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
42
43
44
from flask import Flask
from flask_restplus import Resource, Api, reqparse
import time
from uuid import uuid4
from TaskMngr import TaskMngr
from distributed import Client
if __name__ == '__main__': # this is nec. to use the Client for some reason
tm = TaskMngr()
app = Flask(__name__)
api = Api(app, title='DistributedTasks API', prefix='/v1')
cli = Client()
@api.route('/test')
class test(Resource):
''' test that the app/api is running'''
def get(self):
return {'test': str(int(time.time()*1000))}
@api.route('/task')
@api.route('/task/<tid>')
class task(Resource):
''' for task operations '''
def post(self):
''' submit a new task to be executed and tracked '''
parser = reqparse.RequestParser()
parser.add_argument('task', required=1, help='Name of task function to submit.')
args = parser.parse_args()
tid, msg = tm.submitTask(cli, args.task)
return {'taskid': tid, 'message': msg}
def get(self, tid):
''' get result of task if finished, or the task status '''
resp = {}
resp['status'] = tm.getTaskStatus(tid)
if resp['status'] == 'finished':
resp['result'] = tm.getTaskResult(cli, tid)
return resp
app.run(debug=True)