diff --git a/main.py b/main.py index 28d91e4..3ae500b 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,26 @@ from API import Webserver from dotenv import load_dotenv from time import sleep import os +from apscheduler.schedulers.background import BackgroundScheduler + +def update_shifts(pmt, db): + try: + print("🔄 Updating shifts...") + + pmt.login() + + days_ahead = int(os.environ.get("DAYS_TO_FETCH", 14)) + shifts = pmt.get_shifts(days_ahead) + + db.delete_future_shifts() + db.insert_shifts(shifts) + + print("✅ Shift update complete") + + except Exception as e: + print(f"❌ Error updating shifts: {e}") + + def main(): # Load the environment @@ -15,18 +35,17 @@ def main(): pmt = PMT() webserver = Webserver("0.0.0.0", 8080, db) - # fetch PMT shifts - pmt.login() - - days_ahead = int(os.environ.get("DAYS_TO_FETCH")) - shifts = pmt.get_shifts(days_ahead) - - db.delete_future_shifts() # delete all future shifts before importing, to make sure if a shift is removed, it actually gets removed here too. + # update the shifts on boot + update_shifts(pmt, db) - # Insert the new shifts into the database - db.insert_shifts(shifts) - + # Every hour at minute 0 + scheduler = BackgroundScheduler() + scheduler.add_job( + lambda: update_shifts(pmt, db), + trigger='cron', + minute=0 + ) webserver.run()