Move first scrape from foreground to background

This commit is contained in:
s3lph 2019-08-21 13:51:43 +02:00
parent ffc720cabe
commit 8d3e28a11d
2 changed files with 13 additions and 2 deletions

View file

@ -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, [])

View file

@ -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)