-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
45 lines (35 loc) · 1.05 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
from flask import Flask
import threading
import worker
import json
from bson import json_util
import datetime
import logger
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
class state:
status = ""
db_latest = ""
capacities = ""
stats = ""
def update_state(new_status, new_db_latest, new_capacities, new_stats):
state.status = json.dumps(new_status)
state.db_latest = json_util.dumps(new_db_latest)
state.capacities = json.dumps(new_capacities)
state.stats = json.dumps(new_stats)
def background_worker():
worker.start(update_state)
@app.route('/')
def index():
body = {}
body["date"] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
body["status"] = json.loads(state.status)
body["jobs"] = json.loads(state.db_latest)
body["capacities"] = json.loads(state.capacities)
body["stats"] = json.loads(state.stats)
return json.dumps(body)
if __name__ == '__main__':
thread = threading.Thread(target=background_worker)
thread.start()
app.run(host='0.0.0.0', port=5000, debug=True)