moved background daemon into seperate module

This commit is contained in:
2026-08-11 17:53:36 +02:00
parent aa0c6cf7c0
commit 3fcd169192
4 changed files with 24 additions and 16 deletions
+6 -15
View File
@@ -1,5 +1,7 @@
from flask import Flask, jsonify, render_template
import logging
logging.basicConfig(level=logging.INFO)
from flask import Flask, jsonify, render_template
import os, sys, threading, time
from src.parser import (
@@ -9,14 +11,13 @@ from src.parser import (
)
from src.cache import Cache
from src.web_calendar import Calendar
from src.fetch_job import background_fetch_job
from src import config
app = Flask(__name__)
sessions_cache = Cache()
calendar = Calendar()
logging.basicConfig(level=logging.INFO)
# initial data fetch (required to work)
try:
sessions = calendar.fetch_sessions_since(config.CURRENT_SEMESTER_START)
@@ -50,18 +51,8 @@ def api_dashboard():
def api_sessions():
return jsonify(sessions_cache.get())
# background daemon
def fetch_job():
while True:
time.sleep(config.REFETCH_SECONDS)
try:
sessions = calendar.fetch_sessions_since(config.CURRENT_SEMESTER_START)
sessions_cache.set(sessions)
except Exception as e:
app.logger.warning(f"Failed to fetch from calDAV server, keeping old data. Error: {e}")
threading.Thread(target=background_fetch_job, daemon=True, args=(calendar, sessions_cache)).start()
if __name__ == "__main__":
if os.environ.get("WERKZEUG_RUN_MAIN") == "true" or not app.debug:
t = threading.Thread(target=fetch_job, daemon=True)
t.start()
app.run(host="0.0.0.0", port=5000, debug=False)
+2
View File
@@ -62,3 +62,5 @@ def get_current_semester_start() -> date:
CURRENT_SEMESTER_START = get_current_semester_start()
logger.info(f"Config loaded. Current semesters start: {str(CURRENT_SEMESTER_START)}")
+15
View File
@@ -0,0 +1,15 @@
import time, logging
from src import config
logger = logging.getLogger(__name__)
# background daemon
def background_fetch_job(calendar, sessions_cache):
while True:
time.sleep(config.REFETCH_SECONDS)
try:
sessions = calendar.fetch_sessions_since(config.CURRENT_SEMESTER_START)
sessions_cache.set(sessions)
except Exception as e:
logger.warning(f"Failed to fetch from calDAV server, keeping old data. \nError: {e}")
+1 -1
View File
@@ -44,7 +44,7 @@ class Calendar:
"minutes":int((e.dtend.value - e.dtstart.value).total_seconds() / 60)
})
logger.info(f"Fetched {len(event_list)} sessions from calDAV server")
logger.info(f"Fetched {len(event_list)} sessions from calDAV server")
return event_list
# grab only the sessions that fall into the current semester