From 74cbfe07a3630ac6607874069c4375da4a28a393 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Sat, 27 Jun 2020 18:17:36 +0200 Subject: [PATCH] fix tracker auto update --- WebHost.py | 21 ++++++++++++++++++++- WebHost/autohoster.py | 0 WebHost/templates/tracker.html | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 WebHost/autohoster.py diff --git a/WebHost.py b/WebHost.py index e792ab45..765b1250 100644 --- a/WebHost.py +++ b/WebHost.py @@ -10,6 +10,25 @@ from WebHost.models import db DEBUG = False port = 80 + +def autohost(config: dict): + from WebHost.models import * + import time + from datetime import timedelta, datetime + + def keep_running(): + db.bind(**config["pony"]) + db.generate_mapping(check_tables=False) + while 1: + time.sleep(3) + with db_session: + select( + room for room in Room if (datetime.utcnow() - room.last_activity) < timedelta(hours=room.timeout)) + + import threading + threading.Thread(target=keep_running).start() + + if __name__ == "__main__": multiprocessing.freeze_support() multiprocessing.set_start_method('spawn') @@ -19,7 +38,6 @@ if __name__ == "__main__": if os.path.exists(configpath): import yaml - with open(configpath) as c: app.config.update(yaml.safe_load(c)) @@ -27,6 +45,7 @@ if __name__ == "__main__": db.bind(**app.config["PONY"]) db.generate_mapping(create_tables=True) if DEBUG: + autohost(config) app.run(debug=True, port=port) else: serve(app, port=port, threads=app.config["WAITRESS_THREADS"]) diff --git a/WebHost/autohoster.py b/WebHost/autohoster.py new file mode 100644 index 00000000..e69de29b diff --git a/WebHost/templates/tracker.html b/WebHost/templates/tracker.html index c8440dbd..e4e1477c 100644 --- a/WebHost/templates/tracker.html +++ b/WebHost/templates/tracker.html @@ -21,7 +21,7 @@ function update() { var target = $("
"); - target.load("/tracker/{{ room.tracker.id }}", function (response, status) { + target.load("/tracker/{{ room.tracker }}", function (response, status) { if (status === "success") { target.find(".table").each(function (i, new_table) { var new_trs = $(new_table).find("tbody>tr");