fix worker app #14

Merged
williamp merged 1 commits from worker-context-bugfix into testing 2023-03-21 00:25:53 +00:00

View File

@@ -3,7 +3,7 @@ from flask import Flask
from misc import currDay, datetime, time, timedelta from misc import currDay, datetime, time, timedelta
from db import db, Period, Event from db import db, Period, Event
from create_events import createEvents 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__)) basedir = os.path.abspath(os.path.dirname(__file__))
@@ -18,20 +18,18 @@ app.config['SQLALCHEMY_DATABASE_URI'] =\
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app) db.init_app(app)
# Calculate the next time the function should run (at the :59 mark of the next hour) # Define function to run create_events with app context
now = datetime.now() def run_create_events():
next_hour = now.replace(hour=now.hour + 1, minute=0, second=0, microsecond=0) with app.app_context():
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()
# Call createEvents on initial launch of the script
with app.app_context():
createEvents(db, currDay, Period, Event) createEvents(db, currDay, Period, Event)
# Keep the program running indefinitely # Call createEvents on initial launch of the script
while True: run_create_events()
time.sleep(60)
# 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()