diff --git a/icalendar_timeseries_server/cal.py b/icalendar_timeseries_server/cal.py index accff89..fb5269d 100644 --- a/icalendar_timeseries_server/cal.py +++ b/icalendar_timeseries_server/cal.py @@ -99,6 +99,15 @@ def scrape_calendar(name: str, config: CalendarConfig): _scrape_calendar(name, config, start, end) +def start_scrape_calendar(name: str, config: CalendarConfig): + # Get current time in configured timezone + tz = get_config().tz + now: datetime = datetime.now(tz) + # Schedule first calendar scraping + cron = Timer(0, lambda: scrape_calendar(name, config)) + cron.start() + + def get_calendar(name: str): global _SCRAPE_CACHE return _SCRAPE_CACHE.get(name, []) diff --git a/icalendar_timeseries_server/main.py b/icalendar_timeseries_server/main.py index 2456a2f..a1168f0 100644 --- a/icalendar_timeseries_server/main.py +++ b/icalendar_timeseries_server/main.py @@ -2,7 +2,7 @@ import sys import bottle -from icalendar_timeseries_server.cal import scrape_calendar +from icalendar_timeseries_server.cal import start_scrape_calendar from icalendar_timeseries_server.config import load_config, load_default_config, get_config # Contains decorated bottle handler function for /api/v1/query @@ -19,8 +19,10 @@ def main(): print(f'Can only read one config file, got "{" ".join(sys.argv[1:])}"') exit(1) config = get_config() + # Schedule calendar scraping in the background for calname in config.calendars.keys(): - scrape_calendar(calname, config.calendars[calname]) + start_scrape_calendar(calname, config.calendars[calname]) + # Start the Bottle HTTP server bottle.run(host=config.addr, port=get_config().port)