diff --git a/BookingSystem/app.py b/BookingSystem/app.py index 68ea8f7..e411ed1 100644 --- a/BookingSystem/app.py +++ b/BookingSystem/app.py @@ -76,7 +76,8 @@ def context_processor() -> dict: MAX_DAYS=MAX_DAYS, MIN_LABELS=MIN_LABELS, MAX_LABELS=MAX_LABELS, - FQDN=urlparse(flask.request.base_url).hostname, ) + FQDN=urlparse(flask.request.base_url).hostname, + server_start_time=Settings.get('last_start_time')) @app.errorhandler(401) def unauthorized_401(_) -> tuple[flask.Response, int]: @@ -131,6 +132,7 @@ def debug_login() -> flask.Response: Minify(app, static=False, go=False) # Some static files don't minify well (breaks JS) init_db() Settings.verify_settings_exist() + Settings.set('last_start_time', str(datetime.now().timestamp())) threaded_start_routine(os.getpid()) return app diff --git a/BookingSystem/routes.py b/BookingSystem/routes.py index 8adb908..bb5c716 100644 --- a/BookingSystem/routes.py +++ b/BookingSystem/routes.py @@ -1,3 +1,5 @@ +import json + import flask import inventory @@ -51,7 +53,8 @@ def logout() -> flask.Response: def admin_settings() -> str: return flask.render_template('admin_settings.html', last_sent=Settings.get('report_last_sent') or 0, - send_reports=Settings.get('send_reports') == '1') + send_reports=Settings.get('send_reports') == '1', + routine_tasks_info=json.loads(Settings.get('routine_tasks') or '{}')) @app.route('/audits') diff --git a/BookingSystem/routine_tasks.py b/BookingSystem/routine_tasks.py index a490757..4599e13 100644 --- a/BookingSystem/routine_tasks.py +++ b/BookingSystem/routine_tasks.py @@ -1,14 +1,16 @@ # Run cron jobs within this file as python functions +import json import os import shutil import threading import time +from datetime import datetime import schedule from __init__ import logger -from db import DATABASE +from db import DATABASE, Settings from teams import send_report from user import prune_inactive @@ -22,7 +24,6 @@ def run(cls): while not cease_continuous_run.is_set(): schedule.run_pending() time.sleep(interval) - # TODO: Post to API when a task is run? (for debugging) continuous_thread = ScheduleThread() continuous_thread.start() @@ -40,9 +41,20 @@ def _routine_backup(): def start_routine(): - def _task(job_func): + def update_db(fn_name): + data = json.loads(Settings.get('routine_tasks') or '{}') + if not data.get(fn_name): + data[fn_name] = dict({'last_run': None, 'runs': 0}) + + data[fn_name]['last_run'] = datetime.now().timestamp() + data[fn_name]['runs'] += 1 + + Settings.set('routine_tasks', json.dumps(data)) + + def _task(job_func, job_name=''): try: job_func() + update_db(job_name) except Exception as e: logger.error(e) @@ -59,12 +71,12 @@ def run_mon_to_fri_at_time(job_func, at_time): job_func.__name__ = f'{func_name} (Fredag)' schedule.every().friday.at(at_time).do(job_func) - __routine_backup = lambda: _task(_routine_backup) - __send_report = lambda: _task(send_report) - __prune_inactive = lambda: _task(prune_inactive) - __routine_backup.__name__ = 'Ukentlig backup' - __send_report.__name__ = 'Send Dagsrapport' - __prune_inactive.__name__ = 'Rydd opp brukere' + __routine_backup = lambda: _task(_routine_backup, 'Ukentlig backup') + __send_report = lambda: _task(send_report, 'Send Dagsrapport') + __prune_inactive = lambda: _task(prune_inactive, 'Rydd opp brukere') + __routine_backup.__name__ = 'Ukentlig backup' # Set name for scheduler + __send_report.__name__ = 'Send Dagsrapport' # Set name for scheduler + __prune_inactive.__name__ = 'Rydd opp brukere' # Set name for scheduler # TODO: Add a setting to change the time of the day these run from frontend run_mon_to_fri_at_time(__send_report, "10:00") diff --git a/BookingSystem/templates/admin_settings.html b/BookingSystem/templates/admin_settings.html index da8a2ef..bfffe46 100644 --- a/BookingSystem/templates/admin_settings.html +++ b/BookingSystem/templates/admin_settings.html @@ -43,6 +43,32 @@

En beskjed på toppen av siden. La feltene stå tomme for å fjerne bulletin {% include 'forms/bulletin.html' %} + {% if routine_tasks %} +
+

Rutineoppgaver

+

Status på rutineoppgaver (debugging)

+
+ + + + + + + {% for task, values in routine_tasks_info.items() %} + + + + + + {% endfor %} +
OppgaveSist kjørtUtføringer
{{ task }}{{ values['last_run'] | strfunixtime('%H:%M:%S %d.%m.%Y') }}{{ values['runs'] }}
+ {% endif %} + +
+ Applikasjonen har kjørt siden + {{ server_start_time | strfunixtime }} +
+