fix worker app
This commit is contained in:
@@ -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()
|
Reference in New Issue
Block a user