Files
BellScheduler/app/worker.py

37 lines
1.3 KiB
Python

import os
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
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
app.config['SQLALCHEMY_DATABASE_URI'] =\
'mysql://' + os.environ['MYSQL_USER'] + \
':' + os.environ['MYSQL_PASSWORD'] + \
'@' + os.environ['MYSQL_HOST'] + \
':' + os.environ['MYSQL_PORT'] + \
'/' + os.environ['MYSQL_DB']
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
with app.app_context():
createEvents(db, currDay, Period, Event)
# Keep the program running indefinitely
while True:
time.sleep(60)