Skip to content
Snippets Groups Projects
scheduler.py 1.04 KiB
Newer Older
import json
import time
import schedule

from map_handler.update_measurements import update_measurements_handler
from server.consts import LAKE_RELATIONS_PATH


def update_all_measurements(lake_names: list):
    """Loops through all lake names and calls get_measurements() on each lake"""
    for lake in lake_names:
        update_measurements_handler(None, lake)


def update_scheduler():
    """Schedules the updating of all maps every three days"""
    print("Updating all lake data. This may take some time...")

    # Parse all lake names to a list. Set encoding to utf-8 to retain scandinavian characters
    with open(LAKE_RELATIONS_PATH + 'all_lake_names.json', 'r', encoding='utf-8') as file:
        lake_names = json.load(file)

    # Run update_all_measurements on startup
    update_all_measurements(lake_names)

    # Schedule the updating of all maps every three days
    schedule.every(3).days.do(update_all_measurements(lake_names))

    # Keep scheduler running indefinitely
    while True:
        schedule.run_pending()
        time.sleep(1)