added cron
This commit is contained in:
39
main.py
39
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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user