fix worker app

This commit is contained in:
2023-03-20 20:00:40 -04:00
parent 3830bdf240
commit c2ba1ca5e7

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) createEvents(db, currDay, Period, Event)
# 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 # Call createEvents on initial launch of the script
with app.app_context(): run_create_events()
createEvents(db, currDay, Period, Event)
# Keep the program running indefinitely
while True: # Set up scheduler to run function at 59th minute of every hour
time.sleep(60) scheduler = BlockingScheduler()
scheduler.add_job(run_create_events, 'cron', minute=59)
# Start scheduler
scheduler.start()