mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-05 02:41:14 -05:00
Merge pull request #523 from alphagov/fix-temp-failure-job
Add a lockmode for the update_notifications_status_by_id to prevent t…
This commit is contained in:
@@ -76,6 +76,7 @@ def delete_invitations():
|
|||||||
|
|
||||||
@notify_celery.task(name='timeout-sending-notifications')
|
@notify_celery.task(name='timeout-sending-notifications')
|
||||||
def timeout_notifications():
|
def timeout_notifications():
|
||||||
|
# TODO: optimize the query by adding the date where clause to this query.
|
||||||
notifications = get_notifications(filter_dict={'status': 'sending'})
|
notifications = get_notifications(filter_dict={'status': 'sending'})
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
for noti in notifications:
|
for noti in notifications:
|
||||||
@@ -83,10 +84,11 @@ def timeout_notifications():
|
|||||||
if (now - noti.created_at) > timedelta(
|
if (now - noti.created_at) > timedelta(
|
||||||
seconds=current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD')
|
seconds=current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD')
|
||||||
):
|
):
|
||||||
update_notification_status_by_id(noti.id, 'temporary-failure', STATISTICS_FAILURE)
|
# TODO: think about making this a bulk update rather than one at a time.
|
||||||
current_app.logger.info((
|
updated = update_notification_status_by_id(noti.id, 'temporary-failure', STATISTICS_FAILURE)
|
||||||
"Timeout period reached for notification ({})"
|
if updated:
|
||||||
", status has been updated.").format(noti.id))
|
current_app.logger.info(("Timeout period reached for notification ({})"
|
||||||
|
", status has been updated.").format(noti.id))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
current_app.logger.exception(e)
|
current_app.logger.exception(e)
|
||||||
current_app.logger.error((
|
current_app.logger.error((
|
||||||
|
|||||||
@@ -246,7 +246,7 @@ def _update_notification_status(notification, status, notification_statistics_st
|
|||||||
|
|
||||||
@transactional
|
@transactional
|
||||||
def update_notification_status_by_id(notification_id, status, notification_statistics_status=None):
|
def update_notification_status_by_id(notification_id, status, notification_statistics_status=None):
|
||||||
notification = Notification.query.filter(
|
notification = Notification.query.with_lockmode("update").filter(
|
||||||
Notification.id == notification_id,
|
Notification.id == notification_id,
|
||||||
or_(Notification.status == 'created',
|
or_(Notification.status == 'created',
|
||||||
Notification.status == 'sending',
|
Notification.status == 'sending',
|
||||||
|
|||||||
Reference in New Issue
Block a user