feat: improve scheduler recurrence, DB config, and docs
- Broaden scheduler query window to next N days for proper recurring event expansion (scheduler.py) - Update DB connection logic for consistent .env loading and fallback (database.py) - Harden timezone handling and logging in scheduler and DB utils - Stop auto-deactivating recurring events before recurrence_end (API/events) - Update documentation to reflect new scheduler, API, and logging behavior
This commit is contained in:
@@ -37,6 +37,8 @@ def main():
|
||||
POLL_INTERVAL = 30 # Sekunden, Empfehlung für seltene Änderungen
|
||||
# 0 = aus; z.B. 600 für alle 10 Min
|
||||
REFRESH_SECONDS = int(os.getenv("REFRESH_SECONDS", "0"))
|
||||
# Konfigurierbares Zeitfenster in Tagen (Standard: 7)
|
||||
WINDOW_DAYS = int(os.getenv("EVENTS_WINDOW_DAYS", "7"))
|
||||
last_payloads = {} # group_id -> payload
|
||||
last_published_at = {} # group_id -> epoch seconds
|
||||
|
||||
@@ -55,8 +57,17 @@ def main():
|
||||
|
||||
while True:
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
# Query window: next N days to capture upcoming events and recurring instances
|
||||
# Clients need to know what's coming, not just what's active right now
|
||||
end_window = now + datetime.timedelta(days=WINDOW_DAYS)
|
||||
logging.debug(f"Fetching events window start={now.isoformat()} end={end_window.isoformat()} (days={WINDOW_DAYS})")
|
||||
# Hole alle aktiven Events (bereits formatierte Dictionaries)
|
||||
events = get_active_events(now, now)
|
||||
try:
|
||||
events = get_active_events(now, end_window)
|
||||
logging.debug(f"Fetched {len(events)} events for publishing window")
|
||||
except Exception as e:
|
||||
logging.exception(f"Error while fetching events: {e}")
|
||||
events = []
|
||||
|
||||
# Gruppiere Events nach group_id
|
||||
groups = {}
|
||||
@@ -67,6 +78,9 @@ def main():
|
||||
# Event ist bereits ein Dictionary im gewünschten Format
|
||||
groups[gid].append(event)
|
||||
|
||||
if not groups:
|
||||
logging.debug("No events grouped for any client group in current window")
|
||||
|
||||
# Sende pro Gruppe die Eventliste als retained Message, nur bei Änderung
|
||||
for gid, event_list in groups.items():
|
||||
# stabile Reihenfolge, um unnötige Publishes zu vermeiden
|
||||
@@ -87,7 +101,7 @@ def main():
|
||||
logging.error(
|
||||
f"Fehler beim Publish für Gruppe {gid}: {mqtt.error_string(result.rc)}")
|
||||
else:
|
||||
logging.info(f"Events für Gruppe {gid} gesendet")
|
||||
logging.info(f"Events für Gruppe {gid} gesendet (count={len(event_list)})")
|
||||
last_payloads[gid] = payload
|
||||
last_published_at[gid] = time.time()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user