@@ -365,33 +365,56 @@ def sync_collections():
365365 logging .info ("🏁 Sync-Durchlauf beendet." )
366366
367367def main ():
368+ import subprocess
369+
368370 config = load_config ()
369371 if not config :
370372 sys .exit (1 )
371373
372374 settings = config .get ("settings" , {})
373- run_schedules = settings .get ("run_schedules" , ["NOW" ])
374375
375- if isinstance (run_schedules , str ):
376- run_schedules = [run_schedules ]
377-
378- # NEU: Beim Start des Skripts (ausserhalb des Syncs) noch nicht rotieren
376+ # Log beim Start einmalig ohne Rotation initialisieren
379377 setup_logger (settings .get ("log_level" , "INFO" ), rotate = False )
380378
381- if any (s .upper () == "NOW" for s in run_schedules ):
382- logging .info ("⚡ Modus 'NOW' erkannt: Führe Sync sofort aus und beende danach." )
383- sync_collections ()
384- logging .info ("👋 Container/Skript wird beendet. Ciao!" )
385- sys .exit (0 )
386- else :
387- logging .info (f"🕒 Standby-Modus aktiviert. Geplante Syncs täglich um: { ', ' .join (run_schedules )} Uhr." )
388-
389- for time_str in run_schedules :
390- schedule .every ().day .at (time_str ).do (sync_collections )
379+ logging .info ("🌐 Starte Streamlit Dashboard im Hintergrund..." )
380+ # Streamlit als separaten Prozess abfeuern
381+ subprocess .Popen (["streamlit" , "run" , "ui.py" , "--server.port=8501" , "--server.address=0.0.0.0" ])
382+
383+ logging .info ("🕒 Starte intelligenten Hintergrund-Scheduler..." )
384+ last_schedules = []
385+
386+ while True :
387+ current_config = load_config ()
388+ if current_config :
389+ current_settings = current_config .get ("settings" , {})
390+ # Wir mergen hier die Keys, um den Bug zwischen ui.py und config.yml zu fixen
391+ current_schedules = current_settings .get ("sync_times" , current_settings .get ("run_schedules" , ["04:30" ]))
391392
392- while True :
393- schedule .run_pending ()
394- time .sleep (60 )
393+ if isinstance (current_schedules , str ):
394+ current_schedules = [current_schedules ]
395+
396+ # Wenn NOW getriggert wird
397+ if any (s .upper () == "NOW" for s in current_schedules ):
398+ logging .info ("⚡ Modus 'NOW' erkannt: Führe Sync sofort aus." )
399+ sync_collections ()
400+ # NOW aus der Config löschen, damit er nicht im Loop festhängt
401+ current_schedules = [s for s in current_schedules if s .upper () != "NOW" ]
402+ current_settings ["sync_times" ] = current_schedules
403+ current_config ["settings" ] = current_settings
404+ with open ("config.yml" , "w" , encoding = "utf-8" ) as f :
405+ yaml .dump (current_config , f , default_flow_style = False , allow_unicode = True )
406+
407+ # Bei Änderungen am Zeitplan direkt neu aufbauen
408+ if current_schedules != last_schedules :
409+ schedule .clear ()
410+ for time_str in current_schedules :
411+ if time_str .upper () != "NOW" :
412+ schedule .every ().day .at (time_str ).do (sync_collections )
413+ logging .info (f"🔄 Zeitplan aktiv: Syncs täglich um { ', ' .join (current_schedules )} Uhr" )
414+ last_schedules = current_schedules
415+
416+ schedule .run_pending ()
417+ time .sleep (10 )
395418
396419if __name__ == "__main__" :
397420 main ()
0 commit comments