Changed the timeout notications update query to set notifications still in created to a technical-failure and notifications still in sending or pending are set to a temporary-failure

This commit is contained in:
Rebecca Law
2016-12-16 11:40:58 +00:00
parent a6da1ac864
commit 40dca7f9c7
3 changed files with 45 additions and 11 deletions

View File

@@ -23,6 +23,7 @@ from app.models import (
NOTIFICATION_SENDING,
NOTIFICATION_PENDING,
NOTIFICATION_TECHNICAL_FAILURE,
NOTIFICATION_TEMPORARY_FAILURE,
KEY_TYPE_NORMAL, KEY_TYPE_TEST)
from app.dao.dao_utils import transactional
@@ -349,17 +350,32 @@ def dao_delete_notifications_and_history_by_id(notification_id):
def dao_timeout_notifications(timeout_period_in_seconds):
# update all notifications that are older that the timeout_period_in_seconds
# with a status of created|sending|pending
# with a status of created|sending|pending
# Notifications still in created status are marked with a technical-failure:
# the notification has failed to go to the provider
update_at = datetime.utcnow()
updated = db.session.query(Notification). \
filter(Notification.created_at < (datetime.utcnow() - timedelta(seconds=timeout_period_in_seconds))). \
filter(Notification.status.in_([NOTIFICATION_CREATED, NOTIFICATION_SENDING, NOTIFICATION_PENDING])). \
filter(Notification.status == NOTIFICATION_CREATED). \
update({'status': NOTIFICATION_TECHNICAL_FAILURE, 'updated_at': update_at}, synchronize_session=False)
db.session.query(NotificationHistory). \
filter(NotificationHistory.created_at < (datetime.utcnow() - timedelta(seconds=timeout_period_in_seconds))). \
filter(NotificationHistory.status.in_([NOTIFICATION_CREATED, NOTIFICATION_SENDING, NOTIFICATION_PENDING])). \
filter(NotificationHistory.status == NOTIFICATION_CREATED). \
update({'status': NOTIFICATION_TECHNICAL_FAILURE, 'updated_at': update_at}, synchronize_session=False)
# Notifications still in sending or pending status are marked with a temporary-failure:
# the notification was sent to the provider but there was not a delivery receipt, try to send again.
updated += db.session.query(Notification). \
filter(Notification.created_at < (datetime.utcnow() - timedelta(seconds=timeout_period_in_seconds))). \
filter(Notification.status.in_([NOTIFICATION_SENDING, NOTIFICATION_PENDING])). \
update({'status': NOTIFICATION_TEMPORARY_FAILURE, 'updated_at': update_at}, synchronize_session=False)
db.session.query(NotificationHistory). \
filter(NotificationHistory.created_at < (datetime.utcnow() - timedelta(seconds=timeout_period_in_seconds))). \
filter(NotificationHistory.status.in_([NOTIFICATION_SENDING, NOTIFICATION_PENDING])). \
update({'status': NOTIFICATION_TEMPORARY_FAILURE, 'updated_at': update_at}, synchronize_session=False)
db.session.commit()
return updated