mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 06:21:50 -05:00
Implemented deleted notification if SQS write fails
This commit is contained in:
@@ -106,7 +106,7 @@ def dao_get_template_usage(service_id, limit_days=None):
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_last_template_usage(template_id):
|
||||
return NotificationHistory.query.filter(NotificationHistory.template_id == template_id)\
|
||||
return NotificationHistory.query.filter(NotificationHistory.template_id == template_id) \
|
||||
.join(Template) \
|
||||
.order_by(desc(NotificationHistory.created_at)) \
|
||||
.first()
|
||||
@@ -275,3 +275,14 @@ def delete_notifications_created_more_than_a_week_ago(status):
|
||||
).delete(synchronize_session='fetch')
|
||||
db.session.commit()
|
||||
return deleted
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@transactional
|
||||
def dao_delete_notifications_and_history_by_id(notification_id):
|
||||
db.session.query(Notification).filter(
|
||||
Notification.id == notification_id
|
||||
).delete(synchronize_session='fetch')
|
||||
db.session.query(NotificationHistory).filter(
|
||||
NotificationHistory.id == notification_id
|
||||
).delete(synchronize_session='fetch')
|
||||
|
||||
@@ -13,8 +13,8 @@ from notifications_utils.recipients import allowed_to_send_to, first_column_head
|
||||
from notifications_utils.template import Template
|
||||
from notifications_utils.renderers import PassThrough
|
||||
from app.clients.email.aws_ses import get_aws_responses
|
||||
from app import api_user, encryption, create_uuid, DATETIME_FORMAT, DATE_FORMAT, statsd_client
|
||||
from app.dao.notifications_dao import dao_create_notification
|
||||
from app import api_user, create_uuid, DATETIME_FORMAT, statsd_client
|
||||
from app.dao.notifications_dao import dao_create_notification, dao_delete_notifications_and_history_by_id
|
||||
from app.dao.services_dao import dao_fetch_todays_stats_for_service
|
||||
from app.models import KEY_TYPE_TEAM, KEY_TYPE_TEST, Notification, KEY_TYPE_NORMAL, EMAIL_TYPE
|
||||
from app.dao import (
|
||||
@@ -36,7 +36,6 @@ from app.schemas import (
|
||||
day_schema,
|
||||
unarchived_template_schema
|
||||
)
|
||||
from app.celery.tasks import send_sms, send_email
|
||||
from app.utils import pagination_links
|
||||
|
||||
notifications = Blueprint('notifications', __name__)
|
||||
@@ -320,10 +319,15 @@ def persist_notification(
|
||||
)
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
send_sms_to_provider.apply_async((str(service.id), str(notification_id)), queue='send-sms')
|
||||
if notification_type == EMAIL_TYPE:
|
||||
send_email_to_provider.apply_async((str(service.id), str(notification_id)), queue='send-email')
|
||||
try:
|
||||
if notification_type == SMS_TYPE:
|
||||
send_sms_to_provider.apply_async((str(service.id), str(notification_id)), queue='send-sms')
|
||||
if notification_type == EMAIL_TYPE:
|
||||
send_email_to_provider.apply_async((str(service.id), str(notification_id)), queue='send-email')
|
||||
except Exception as e:
|
||||
current_app.logger.exception("Failed to send to SQS exception", e)
|
||||
dao_delete_notifications_and_history_by_id(notification_id)
|
||||
raise InvalidRequest(message="Internal server error", status_code=500)
|
||||
|
||||
current_app.logger.info(
|
||||
"{} {} created at {}".format(notification_type, notification_id, created_at)
|
||||
|
||||
Reference in New Issue
Block a user