From f50fe9cbdcf186883cf454e8df7bd03a9d3afc86 Mon Sep 17 00:00:00 2001 From: Tfloow <88899420+Tfloow@users.noreply.github.com> Date: Tue, 23 Jan 2024 20:49:17 +0100 Subject: [PATCH] Now doing request async so no more slow loading on user startup --- app.py | 43 ++++++++++++++++++++++++++++++------------- requirements.txt | 3 ++- services.json | 20 ++++++++++---------- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index 843a123..4624493 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,9 @@ import json import requests import csv +from apscheduler.schedulers.background import BackgroundScheduler # To schedule the check +import datetime +import logging # My modules import jsonUtility @@ -18,26 +21,40 @@ def urlOfService(services, service): return services[service]["url"] return "NaN" -def statusService(services, service): +def updateStatusService(services, service): url = urlOfService(services, service) if url == "NaN": print("[LOG]: You passed a service that is not tracked") return False - needToCheck = jsonUtility.deltaTimeService(services, service) + print(f"[LOG]: HTTP request for {services[service]["url"]}") + response = requests.get(url).status_code < 400 # Starting 400 codes are error for HTTP GET - if needToCheck: - print(f"[LOG]: HTTP request for {services[service]["url"]}") - response = requests.get(url).status_code < 400 # Starting 400 codes are error for HTTP GET - - jsonUtility.updateStatus(services, service, response) - - return response - else: - return services[service]["Last status"] + jsonUtility.updateStatus(services, service, response) + + return response + +def statusService(services, service): + url = urlOfService(services, service) + if url == "NaN": + print("[LOG]: You passed a service that is not tracked") + return False + + return services[service]["Last status"] + +def refreshServices(services): + print("[LOG]: Refreshing the services") + for service in services.keys(): + updateStatusService(services, service) + +# Setup Scheduler to periodically check the status of the website +scheduler = BackgroundScheduler() +scheduler.add_job(refreshServices, "interval" ,args=[services], minutes=jsonUtility.timeCheck, next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=1)) +# Start the scheduler +scheduler.start() -# Start the Flask app +# ------------------ Start the Flask app ------------------ app = Flask("UCLouvainDown") @app.route("/") @@ -135,7 +152,7 @@ def extractLog(): @app.route('/robots.txt') @app.route('/sitemap.xml') def static_from_root(): - return send_from_directory(app.static_folder, request.path[1:]) + return send_from_directory(app.static_folder, request.path[1:]) if __name__ == "__main__": app.run(host="0.0.0.0", debug=True) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 7d9619d..e1e3716 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ pytz>=2023.3.post1 Flask>=3.0.0 requests>=2.31.0 gunicorn>=21.2.0 -matplotlib>=3.8.2 \ No newline at end of file +matplotlib>=3.8.2 +APScheduler>=3.10.4 \ No newline at end of file diff --git a/services.json b/services.json index a1e4595..8b05950 100644 --- a/services.json +++ b/services.json @@ -1,51 +1,51 @@ { "404-Test": { - "Last access time": "2024-01-23T17:42:05", + "Last access time": "2024-01-23T19:48:44", "Last status": false, "url": "https://www.google.com/404" }, "ADE": { - "Last access time": "2024-01-23T17:42:05", + "Last access time": "2024-01-23T19:48:44", "Last status": true, "url": "https://horaire.uclouvain.be/direct/" }, "ADE-Scheduler": { - "Last access time": "2024-01-23T17:42:05", + "Last access time": "2024-01-23T19:48:45", "Last status": true, "url": "https://ade-scheduler.info.ucl.ac.be/calendar/" }, "Comproved": { - "Last access time": "2024-01-23T17:42:05", + "Last access time": "2024-01-23T19:48:46", "Last status": true, "url": "https://app.comproved.com/universite-de-louvain" }, "Gradescope": { - "Last access time": "2024-01-23T17:42:06", + "Last access time": "2024-01-23T19:48:48", "Last status": true, "url": "https://www.gradescope.com/" }, "Intranet": { - "Last access time": "2024-01-23T17:42:06", + "Last access time": "2024-01-23T19:48:49", "Last status": true, "url": "https://intranet.uclouvain.be" }, "LEPL1104": { - "Last access time": "2024-01-23T17:42:06", + "Last access time": "2024-01-23T19:48:49", "Last status": true, "url": "https://perso.uclouvain.be/vincent.legat/zouLab/epl1104.php" }, "LEPL1201": { - "Last access time": "2024-01-23T17:42:06", + "Last access time": "2024-01-23T19:48:50", "Last status": true, "url": "https://perso.uclouvain.be/vincent.legat/zouLab/epl1201.php" }, "Moodle": { - "Last access time": "2024-01-23T17:42:06", + "Last access time": "2024-01-23T19:48:51", "Last status": true, "url": "https://moodle.uclouvain.be/" }, "UCLouvain": { - "Last access time": "2024-01-23T17:42:07", + "Last access time": "2024-01-23T19:48:53", "Last status": true, "url": "https://www.uclouvain.be/" }