fix worker app #14

Merged
williamp merged 1 commits from worker-context-bugfix into testing 2023-03-21 00:25:53 +00:00
Showing only changes of commit c2ba1ca5e7 - Show all commits

View File

@@ -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()
# Call createEvents on initial launch of the script
# Define function to run create_events with app context
def run_create_events():
with app.app_context():
createEvents(db, currDay, Period, Event)
# Keep the program running indefinitely
while True:
time.sleep(60)
# Call createEvents on initial launch of the script
run_create_events()
# 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()