diff --git a/app/worker.py b/app/worker.py index 34e292f..288a29e 100644 --- a/app/worker.py +++ b/app/worker.py @@ -3,7 +3,7 @@ from flask import Flask from misc import currDay, datetime, time, timedelta from db import db, Period, Event from create_events import createEvents -from apscheduler.schedulers.background import BackgroundScheduler +from apscheduler.schedulers.background import BlockingScheduler basedir = os.path.abspath(os.path.dirname(__file__)) @@ -18,20 +18,18 @@ app.config['SQLALCHEMY_DATABASE_URI'] =\ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) -# Calculate the next time the function should run (at the :59 mark of the next hour) -now = datetime.now() -next_hour = now.replace(hour=now.hour + 1, minute=0, second=0, microsecond=0) -next_run = next_hour - timedelta(minutes=1) - -# Create scheduler and add job to call createEvents at the specified time -scheduler = BackgroundScheduler() -scheduler.add_job(createEvents, 'date', run_date=next_run, args=[db, currDay, Period, Event]) -scheduler.start() +# Define function to run create_events with app context +def run_create_events(): + with app.app_context(): + createEvents(db, currDay, Period, Event) # Call createEvents on initial launch of the script -with app.app_context(): - createEvents(db, currDay, Period, Event) +run_create_events() -# Keep the program running indefinitely -while True: - time.sleep(60) \ No newline at end of file + +# Set up scheduler to run function at 59th minute of every hour +scheduler = BlockingScheduler() +scheduler.add_job(run_create_events, 'cron', minute=59) + +# Start scheduler +scheduler.start() \ No newline at end of file