Skip to content
Snippets Groups Projects
Commit 90401b7c authored by Simon Künzel's avatar Simon Künzel
Browse files

Minor scheduler and live config improvements

parent aebb26e8
No related branches found
No related tags found
1 merge request!5Rollout to production
Checking pipeline status
{
"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
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)
......@@ -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,7 +19,6 @@ 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()
try:
......@@ -26,14 +27,13 @@ def scheduled_function(delay_sec: int, initial_delay_sec: int or None = None, pr
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")
__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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment