-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
213 lines (181 loc) · 7.31 KB
/
app.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""Minimal POST-based API for test scheduling."""
import os
from datetime import datetime
from flask import Flask, jsonify, request
from schema import SchemaError
from covid_scheduling import (validate_people, validate_config,
assign_schedules, AssignmentError)
from celery import Celery
from celery.utils.log import get_task_logger
from werkzeug.exceptions import default_exceptions
from werkzeug.exceptions import HTTPException
DATE_FORMAT = '%Y-%m-%d'
app = Flask(__name__)
# Celery configuration template from
# https://flask.palletsprojects.com/en/1.1.x/patterns/celery/
def make_celery(app):
"""Initializes Celery with Flask context."""
celery = Celery(app.import_name,
backend=app.config['CELERY_RESULT_BACKEND'],
broker=app.config['CELERY_BROKER_URL'],
task_track_started=app.config['CELERY_TRACK_STARTED'])
celery.conf.update(app.config)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery
@app.errorhandler(Exception)
def handle_generic_error(error):
# https://stackoverflow.com/a/29332131
if isinstance(error, HTTPException):
code = error.code
error = str(error)
app.logger.error(f'HTTP error {code}: {error}')
else:
code = 500
error = 'Unknown internal error.'
app.logger.error(f'Internal error: {error}')
return jsonify(error=error), code
# Enable JSON error handling. (see https://stackoverflow.com/a/29332131)
for code in default_exceptions:
app.register_error_handler(code, handle_generic_error)
# Set up backend for job-based API.
app.config.update(CELERY_BROKER_URL=os.getenv('REDIS_URL', ''),
CELERY_RESULT_BACKEND=os.getenv('REDIS_URL', ''),
CELERY_TRACK_STARTED=True)
celery = make_celery(app)
task_logger = get_task_logger(__name__)
@celery.task(bind=True)
def run_scheduler(self, body):
# Load request.
try:
params = parse_request(body)
except InvalidUsage as ex:
task_logger.error(f'Schema error: {ex.message}', exc_info=True)
self.update_state(state='FAILURE',
meta={'error': f'Schema error: {ex.message}'})
except Exception:
task_logger.error('Unknown schema error.', exc_info=True)
self.update_state(state='FAILURE',
meta={'error': 'Unknowh schema error.'})
# Generate assignments.
try:
assignments, stats = assign_schedules(params['config'],
params['people'],
params['start_date'],
params['end_date'])
except AssignmentError as ex:
task_logger.error('Assignment error.', exc_info=True)
self.update_state(state='FAILURE',
meta={'error': f'Assignment error: {ex.message}'})
except Exception as ex:
task_logger.error('Unknown assignment error.', exc_info=True)
self.update_state(state='FAILURE',
meta={'error': 'Unknown assignment error.'})
return {
'people': assignments,
'stats': stats,
'start_date': params['start_date'].strftime(DATE_FORMAT),
'end_date': params['end_date'].strftime(DATE_FORMAT)
}
@app.route('/', methods=['POST'])
def schedule():
"""Main endpoint for synchronous schedule assignment."""
if not request.is_json:
raise InvalidUsage('Expected content-type is application/json.')
body = request.get_json()
params = parse_request(body)
try:
assignments, stats = assign_schedules(params['config'],
params['people'],
params['start_date'],
params['end_date'])
except AssignmentError as ex:
app.logger.error('Assignment error.', exc_info=True)
raise InvalidUsage(f'Assignment error: {ex.message}', 500)
except Exception as ex:
app.logger.error('Unknown assignment error.', exc_info=True)
raise InvalidUsage('Unknown assignment error.', 500)
return jsonify({
'people': assignments,
'stats': stats,
'start_date': params['start_date'].strftime(DATE_FORMAT),
'end_date': params['end_date'].strftime(DATE_FORMAT)
})
@app.route('/jobs', methods=['POST'])
def start_job():
"""Main endpoint for starting asynchronous jobs."""
if not request.is_json:
raise InvalidUsage('Expected content-type is application/json.')
body = request.get_json()
task = run_scheduler.delay(body)
return jsonify({'status': 'pending', 'id': task.id})
@app.route('/jobs/<task_id>', methods=['GET'])
def job_status(task_id):
task = run_scheduler.AsyncResult(task_id)
if task.state == 'FAILURE':
response = {
'status': 'failed',
'error': task.info.get('error', 'Unknown error.')
}
elif task.state == 'SUCCESS':
response = {'status': 'succeeded', 'result': task.info}
else:
response = {'status': task.state.lower()}
return jsonify(response)
def parse_request(body):
if 'config' not in body:
raise InvalidUsage("Expected campus configurations in 'config' field.")
if 'people' not in body:
raise InvalidUsage("Expected a 'people' field.")
try:
start_date = datetime.strptime(body.get('start', ''), DATE_FORMAT)
except (TypeError, ValueError):
raise InvalidUsage("Expected a start date in field 'start' "
"with format YYYY-MM-DD.")
try:
end_date = datetime.strptime(body.get('end', ''), DATE_FORMAT)
except (TypeError, ValueError):
raise InvalidUsage("Expected a start date in field 'end' "
"with format YYYY-MM-DD.")
try:
config = validate_config(body['config'])
except SchemaError as ex:
raise InvalidUsage('Could not validate configuration.',
payload={'fields': ex.autos})
try:
people = validate_people(body['people'], config)
except SchemaError as ex:
raise InvalidUsage('Could not validate people.',
payload={'fields': ex.autos})
return {
'config': config,
'people': people,
'start_date': start_date,
'end_date': end_date
}
# Error handling template from
# https://flask.palletsprojects.com/en/1.1.x/patterns/apierrors/
class InvalidUsage(Exception):
"""Raised for assignment failures."""
def __init__(self, message, status_code=400, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self):
"""Serializes the error."""
rv = dict(self.payload or ())
rv['error'] = self.message
return rv
@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
"""Handler for assignment failures."""
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
if __name__ == '__main__':
app.run(threaded=True, port=5000)