2020-09-24 12:25:18 -04:00
|
|
|
from communicator import db, app
|
2020-09-24 11:51:50 -04:00
|
|
|
import time
|
|
|
|
import atexit
|
2020-09-22 13:59:40 -04:00
|
|
|
|
2020-09-24 11:51:50 -04:00
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
2020-09-22 13:59:40 -04:00
|
|
|
|
2020-09-25 11:33:20 -04:00
|
|
|
from communicator.api import admin
|
|
|
|
|
|
|
|
|
|
|
|
def update_and_notify():
|
|
|
|
with app.app_context():
|
2020-10-12 13:42:41 -04:00
|
|
|
if app.config['RUN_SCHEDULED_TASKS']:
|
|
|
|
admin.update_and_notify()
|
|
|
|
else:
|
|
|
|
app.logger.info("Currently not running scheduled tasks RUN_SCHEDULED_TASKS"
|
|
|
|
" is set to false in configuration.")
|
2020-09-25 11:33:20 -04:00
|
|
|
|
2020-09-24 11:51:50 -04:00
|
|
|
scheduler = BackgroundScheduler()
|
|
|
|
scheduler.add_jobstore('sqlalchemy', url=db.engine.url)
|
|
|
|
scheduler.add_job(
|
2020-09-25 11:33:20 -04:00
|
|
|
update_and_notify, 'interval', minutes=app.config['SCHEDULED_TASK_MINUTES'],
|
2020-09-24 12:25:18 -04:00
|
|
|
id='update_data', replace_existing=True
|
2020-09-24 11:51:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
scheduler.start()
|
|
|
|
|
|
|
|
# Shut down the scheduler when exiting the app
|
|
|
|
atexit.register(lambda: scheduler.shutdown())
|