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 dotenv import load_dotenv
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import os
|
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():
|
def main():
|
||||||
# Load the environment
|
# Load the environment
|
||||||
@@ -15,18 +35,17 @@ def main():
|
|||||||
pmt = PMT()
|
pmt = PMT()
|
||||||
webserver = Webserver("0.0.0.0", 8080, db)
|
webserver = Webserver("0.0.0.0", 8080, db)
|
||||||
|
|
||||||
# fetch PMT shifts
|
# update the shifts on boot
|
||||||
pmt.login()
|
update_shifts(pmt, db)
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
|
|
||||||
# Insert the new shifts into the database
|
# Every hour at minute 0
|
||||||
db.insert_shifts(shifts)
|
scheduler = BackgroundScheduler()
|
||||||
|
scheduler.add_job(
|
||||||
|
lambda: update_shifts(pmt, db),
|
||||||
|
trigger='cron',
|
||||||
|
minute=0
|
||||||
|
)
|
||||||
|
|
||||||
webserver.run()
|
webserver.run()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user