2020-10-21 15:17:03 -04:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import pytz
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-10-21 16:45:26 -04:00
|
|
|
def within_notification_window():
|
2020-10-21 15:17:03 -04:00
|
|
|
tz = pytz.timezone('US/Eastern')
|
|
|
|
now = (datetime.now(tz))
|
|
|
|
one_pm = (datetime.now(tz).replace(hour=13, minute=0, second=0, microsecond=0))
|
2020-11-13 14:28:39 -05:00
|
|
|
two_pm = (datetime.now(tz).replace(hour=17, minute=30, second=0, microsecond=0))
|
2020-10-21 15:17:03 -04:00
|
|
|
return one_pm <= now <= two_pm
|
|
|
|
|
|
|
|
|
2020-10-26 17:24:51 -04:00
|
|
|
def update():
|
2020-09-25 11:33:20 -04:00
|
|
|
with app.app_context():
|
2020-11-13 10:25:32 -05:00
|
|
|
if within_notification_window():
|
|
|
|
app.logger.info("Do not load new files during the notification window.")
|
|
|
|
else:
|
2020-10-15 15:29:40 -04:00
|
|
|
admin._update_data()
|
2020-10-26 17:24:51 -04:00
|
|
|
|
|
|
|
def notify():
|
|
|
|
with app.app_context():
|
|
|
|
if within_notification_window():
|
|
|
|
app.logger.info("Within Notification Window, sending messages.")
|
|
|
|
admin._notify_by_email()
|
|
|
|
admin._notify_by_text()
|
|
|
|
else:
|
|
|
|
app.logger.info("Not within the notification window, not sending messages.")
|
2020-10-21 15:17:03 -04:00
|
|
|
|
2020-09-25 11:33:20 -04:00
|
|
|
|
2020-10-15 15:29:40 -04:00
|
|
|
if app.config['RUN_SCHEDULED_TASKS']:
|
|
|
|
scheduler = BackgroundScheduler()
|
|
|
|
scheduler.add_jobstore('sqlalchemy', url=db.engine.url)
|
|
|
|
scheduler.add_job(
|
2020-10-26 17:24:51 -04:00
|
|
|
update, 'interval', minutes=60, id='update_data', replace_existing=True
|
|
|
|
)
|
|
|
|
scheduler.add_job(
|
|
|
|
notify, 'interval', minutes=app.config['SCHEDULED_TASK_MINUTES'],
|
|
|
|
id='notify', replace_existing=True
|
2020-10-15 15:29:40 -04:00
|
|
|
)
|
|
|
|
scheduler.start()
|
2020-09-24 11:51:50 -04:00
|
|
|
|
2020-10-15 15:29:40 -04:00
|
|
|
# Shut down the scheduler when exiting the app
|
|
|
|
atexit.register(lambda: scheduler.shutdown())
|
|
|
|
else:
|
|
|
|
app.logger.info("Currently not running scheduled tasks RUN_SCHEDULED_TASKS"
|
2020-10-26 17:24:51 -04:00
|
|
|
" is set to false in configuration.")
|