Add scheduled task to send scheduled notifcations.

Fix the query to fetch scheduled notifications.
This commit is contained in:
Rebecca Law
2017-05-16 13:47:22 +01:00
parent 56f657de9b
commit 2e078f9fc8
5 changed files with 55 additions and 11 deletions

View File

@@ -14,13 +14,14 @@ from app.dao.jobs_dao import dao_set_scheduled_jobs_to_pending, dao_get_jobs_old
from app.dao.notifications_dao import (
delete_notifications_created_more_than_a_week_ago,
dao_timeout_notifications,
is_delivery_slow_for_provider
)
is_delivery_slow_for_provider,
dao_get_scheduled_notifications)
from app.dao.provider_details_dao import (
get_current_provider,
dao_toggle_sms_provider
)
from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago
from app.notifications.process_notifications import send_notification_to_queue
from app.statsd_decorators import statsd
from app.celery.tasks import process_job
@@ -46,6 +47,18 @@ def run_scheduled_jobs():
raise
@notify_celery.task(name='send-scheduled-notifications')
@statsd(namespace="tasks")
def send_scheduled_notifications():
try:
for notification in dao_get_scheduled_notifications():
send_notification_to_queue(notification, notification.service.research_mode)
except SQLAlchemyError as e:
current_app.logger.exception("Failed to send scheduled notifications")
raise
@notify_celery.task(name="delete-verify-codes")
@statsd(namespace="tasks")
def delete_verify_codes():

View File

@@ -109,6 +109,11 @@ class Config(object):
'schedule': crontab(minute=1),
'options': {'queue': 'periodic'}
},
'send-scheduled-notifications': {
'task': 'send-scheduled-notifications',
'schedule': crontab(minute=1),
'options': {'queue': 'periodic'}
},
'delete-verify-codes': {
'task': 'delete-verify-codes',
'schedule': timedelta(minutes=63),

View File

@@ -473,6 +473,10 @@ def dao_created_scheduled_notification(scheduled_notification):
@statsd(namespace="dao")
def dao_get_scheduled_notifications():
scheduled_notifications = ScheduledNotification.query.filter(
ScheduledNotification.scheduled_for < datetime.utcnow()).all()
return scheduled_notifications
notifications = Notification.query.join(
ScheduledNotification
).filter(
ScheduledNotification.scheduled_for < datetime.utcnow(),
Notification.status == NOTIFICATION_CREATED).all()
return notifications