-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (47 loc) · 1.56 KB
/
app.py
File metadata and controls
51 lines (47 loc) · 1.56 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
45
46
47
48
49
50
51
import os, json
from flask import Flask, request, render_template
from rq import Queue
from worker import conn
from worker_tasks import run_script
app = Flask(__name__)
q = Queue(connection=conn)
def get_status(job):
app_url = 'https://mnist-rdm.herokuapp.com'
status = {
'id': job.id,
'result': job.result,
'status': '',
'message': '',
'link': ''
}
options = {
'status': 'failed'
} if job.is_failed else {
'status': 'pending',
'message': 'Still working. Wait a few minutes and click the link to see if the job is ready.',
'link': '<a href="{}?job={}">Click here.</a>'.format(app_url, job.id)
} if job.result == None else {
'status': 'completed'
}
status.update(options)
status.update(job.meta)
return status
@app.route("/")
def handle_job():
query_id = request.args.get('job')
if query_id:
found_job = q.fetch_job(query_id)
if found_job:
if found_job.result:
response = render_template('output.html', output=found_job.result)
else:
response = render_template('wait.html', status=get_status(found_job))
else:
response = { 'id': None, 'error_message': 'No job exists with the id number ' + query_id }
else:
new_job = q.enqueue(run_script, timeout='1h')
response = render_template('wait.html', status=get_status(new_job))
return response
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)