-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
81 lines (63 loc) · 2.48 KB
/
server.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from flask import Flask, jsonify, render_template
from flask_restful import Resource, Api, reqparse
import os, sys; sys.path.append(os.path.abspath('process_scheduler'))
from process_scheduler.common.serializer import JsonSerializer
from process_scheduler.common.parser import parseSyntax
from process_scheduler.scheduler.core import SchedulerFactory
from process_scheduler.process.process import ProcessManager
from process_scheduler.scheduler.timer import SystemTimer
from errors import error_translation
app = Flask(__name__)
api = Api(app, catch_all_404s=True, errors=error_translation)
class Scheduler(Resource):
parser = reqparse.RequestParser()
parser.add_argument('syntax', type=str, help="Process Description Syntax")
parser.add_argument('initial_process', type=str, help="name of the initial process")
def post(self, strategy, quantum=4, ts=10):
#import pdb; pdb.set_trace()
# parse process syntax
args = self.parser.parse_args()
processDescription = args.get("syntax")
init_p = args.get("initial_process")
# trying to parse the syntax
parseSyntax(processDescription)
# here comes the critical section: creating the Scheduler environmet including Singletons.
try:
# generate scheduler
scheduler = SchedulerFactory.getScheduler(str(strategy), quantum=quantum, timeslice=ts)
# run scheduler
scheduler.initialize(init_p)
scheduler.run()
# generate JSON
json_result = JsonSerializer().generateData()
except Exception as e:
# pass the exceptions to the api - it will translate it to json-requests
raise e
finally:
# we make sure that even after an exception, all Singletons get destroyed.
ProcessManager._drop()
SystemTimer._drop()
return json_result, 200
@app.teardown_request
def tearDown(self):
SystemTimer._drop()
ProcessManager._drop()
class SchedulerList(Resource):
def get(self):
possible_scheds = SchedulerFactory.getPossibleChoices()
options = {"installed scheduler": possible_scheds}
return jsonify(options)
# register endpoints
api.add_resource(SchedulerList, '/scheduler')
api.add_resource(Scheduler,
'/schedule/with/<string:strategy>',
'/schedule/with/<string:strategy>/timeslice/<int:ts>',
'/schedule/with/<string:strategy>/timeslice/<int:ts>/quantum/<int:quantum>',
'/schedule/with/<string:strategy>/quantum/<int:quantum>/timeslice/<int:ts>',
)
# mainpage
@app.route("/", methods=['GET'])
def mainpage():
return render_template('combined.html')
if __name__ == '__main__':
app.run(debug=False, threaded=True)