moved fetch daemon to seperate module

This commit is contained in:
2026-08-11 18:20:05 +02:00
parent 3fcd169192
commit eafc54b53b
2 changed files with 16 additions and 5 deletions
+3 -3
View File
@@ -2,7 +2,7 @@ import logging
logging.basicConfig(level=logging.INFO)
from flask import Flask, jsonify, render_template
import os, sys, threading, time
import os, sys, time
from src.parser import (
calculate_totals,
@@ -17,6 +17,7 @@ from src import config
app = Flask(__name__)
sessions_cache = Cache()
calendar = Calendar()
daemon = BGFetchJob()
# initial data fetch (required to work)
try:
@@ -51,8 +52,7 @@ def api_dashboard():
def api_sessions():
return jsonify(sessions_cache.get())
threading.Thread(target=background_fetch_job, daemon=True, args=(calendar, sessions_cache)).start()
daemon.run((calendar, sessions_cache))
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=False)
+13 -2
View File
@@ -1,10 +1,11 @@
import time, logging
import time, logging, threading
from src import config
FILE_LOCK_PATH = "/tmp/fetch_job.lock"
logger = logging.getLogger(__name__)
# background daemon
def background_fetch_job(calendar, sessions_cache):
while True:
time.sleep(config.REFETCH_SECONDS)
@@ -13,3 +14,13 @@ def background_fetch_job(calendar, sessions_cache):
sessions_cache.set(sessions)
except Exception as e:
logger.warning(f"Failed to fetch from calDAV server, keeping old data. \nError: {e}")
class BGFetchJob:
def __init__(self):
self.thread = None
def run(self, calendar, sessions_cache):
logger.info("Starting fetch job daemon")
self.thread = threading.Thread(target=background_fetch_job, args=(calendar, sessions_cache), daemon=True)
self.thread.start()