54 lines
1.1 KiB
Python
54 lines
1.1 KiB
Python
from PMT import PMT
|
|
from DB import Database
|
|
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
|
|
load_dotenv(override=True)
|
|
|
|
|
|
# Set up database, webserver and PMT connection
|
|
db = Database()
|
|
pmt = PMT()
|
|
webserver = Webserver("0.0.0.0", 8080, db)
|
|
|
|
# update the shifts on boot
|
|
update_shifts(pmt, db)
|
|
|
|
|
|
# Every hour at minute 0
|
|
scheduler = BackgroundScheduler()
|
|
scheduler.add_job(
|
|
lambda: update_shifts(pmt, db),
|
|
trigger='cron',
|
|
minute=0
|
|
)
|
|
|
|
webserver.run()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|