From 90401b7cc7516c730895e29105d83e604a2f7714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20K=C3=BCnzel?= <simonk@fsmpi.rwth-aachen.de> Date: Tue, 18 Jun 2024 16:07:21 +0200 Subject: [PATCH] Minor scheduler and live config improvements --- config/live_config.json | 9 ++++++++- src/api/live_config.py | 10 +++++++++- src/api/miscellaneous/scheduler.py | 18 +++++++++--------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/config/live_config.json b/config/live_config.json index d99ed9d..8b335dc 100644 --- a/config/live_config.json +++ b/config/live_config.json @@ -1,5 +1,12 @@ { "readonly": false, "disabled": false, - "static_announcements": [] + "static_announcements": [ + { + "is_enabled": false, + "type": "info", + "visibility": "all_pages", + "text": "Example static announcement. Other types: 'warning' and 'important'. Other visibilities: 'only_main_page' and 'all_pages_and_embed'" + } + ] } \ No newline at end of file diff --git a/src/api/live_config.py b/src/api/live_config.py index 44a3d58..e15c1d7 100644 --- a/src/api/live_config.py +++ b/src/api/live_config.py @@ -1,4 +1,5 @@ import os +import traceback from pathlib import Path import api @@ -72,7 +73,7 @@ class LiveConfig: config: LiveConfig = LiveConfig() -@scheduled_function(api.config["LIVE_CONFIG_UPDATE_INTERVAL_SECONDS"], 1) +@scheduled_function(api.config["LIVE_CONFIG_UPDATE_INTERVAL_SECONDS"], api.config["LIVE_CONFIG_UPDATE_INTERVAL_SECONDS"]) def _update_live_config(): if _LIVE_CONFIG_PATH is None: return @@ -81,3 +82,10 @@ def _update_live_config(): return config.update(CJsonObject(json.loads(_LIVE_CONFIG_PATH.read_text(encoding="UTF-8")))) # Exception is caught and logged by scheduler + + +try: + _update_live_config() +except Exception as e: + print(f"Exception when loading live config for the first time:") + traceback.print_exception(e) diff --git a/src/api/miscellaneous/scheduler.py b/src/api/miscellaneous/scheduler.py index 0b3f907..31d4f72 100644 --- a/src/api/miscellaneous/scheduler.py +++ b/src/api/miscellaneous/scheduler.py @@ -10,6 +10,8 @@ import api __SCHEDULER = sched.scheduler() +_DISABLE_SCHEDULER = api.config.get("DISABLE_SCHEDULER", False) + def scheduled_function(delay_sec: int, initial_delay_sec: int or None = None, priority: int = 0): if initial_delay_sec is None: @@ -17,23 +19,21 @@ def scheduled_function(delay_sec: int, initial_delay_sec: int or None = None, pr def decorator(func): def execute_scheduled(): - if DEBUG_ENABLED: - print(f"Scheduler: Executing {func.__name__}") - start_time = time.time_ns() + print(f"Scheduler: Executing {func.__name__}") + start_time = time.time_ns() try: func() except Exception as e: print(f"Scheduler: An exception occurred during execution of {func.__name__}:") traceback.print_exception(e) # TODO notify - if DEBUG_ENABLED: - # noinspection PyUnboundLocalVariable - total_time = time.time_ns() - start_time - print(f"Scheduler: Execution of {func.__name__} took {(total_time//1000)/1000}ms") + + total_time = time.time_ns() - start_time + print(f"Scheduler: Execution of {func.__name__} took {(total_time//1000)/1000}ms") __SCHEDULER.enter(delay_sec, priority, execute_scheduled) __SCHEDULER.enter(initial_delay_sec, priority, execute_scheduled) - print(f"Scheduler: Registered '{func.__name__}' to be executed every {delay_sec}s (first execution in {initial_delay_sec}s)") + print(f"Scheduler: Registered '{func.__name__}' to be executed every {delay_sec}s (first execution in {initial_delay_sec}s) {'(Scheduler is disabled)' if _DISABLE_SCHEDULER else ''}") return func return decorator @@ -45,5 +45,5 @@ def __run_scheduler_thread(): sleep(5) -if not api.config.get("DISABLE_SCHEDULER", False): +if not _DISABLE_SCHEDULER: threading.Thread(target=__run_scheduler_thread, daemon=True).start() \ No newline at end of file -- GitLab