Compare commits

...

1 Commits

Author SHA1 Message Date
Rebecca Law
abf36ecd82 Commit the db session before sending notification to the queue.
When a high volume service posts a notification we want to commit the db session before we send the message to SQS. This will release the db connection earlier.
2020-06-19 16:41:43 +01:00
2 changed files with 24 additions and 13 deletions

View File

@@ -15,8 +15,8 @@ from app import (
notify_celery, notify_celery,
document_download_client, document_download_client,
encryption, encryption,
DATETIME_FORMAT DATETIME_FORMAT,
) db)
from app.celery.letters_pdf_tasks import get_pdf_for_templated_letter, sanitise_letter from app.celery.letters_pdf_tasks import get_pdf_for_templated_letter, sanitise_letter
from app.celery.research_mode_tasks import create_fake_letter_response_file from app.celery.research_mode_tasks import create_fake_letter_response_file
from app.celery.tasks import save_api_email from app.celery.tasks import save_api_email
@@ -187,8 +187,10 @@ def process_sms_or_email_notification(
if document_download_count: if document_download_count:
# We changed personalisation which means we need to update the content # We changed personalisation which means we need to update the content
template.values = personalisation template.values = personalisation
api_key_id = api_key.id
key_type = api_key.key_type key_type = api_key.key_type
service_in_research_mode = service.research_mode service_in_research_mode = service.research_mode
template_version = template._template['version']
resp = create_response_for_post_notification( resp = create_response_for_post_notification(
notification_id=notification_id, notification_id=notification_id,
client_reference=form.get('reference', None), client_reference=form.get('reference', None),
@@ -200,19 +202,22 @@ def process_sms_or_email_notification(
scheduled_for=form.get("scheduled_for", None), scheduled_for=form.get("scheduled_for", None),
template_with_content=template) template_with_content=template)
if str(service.id) in current_app.config.get('HIGH_VOLUME_SERVICE') and api_key.key_type == KEY_TYPE_NORMAL \ if str(service.id) in current_app.config.get('HIGH_VOLUME_SERVICE') and key_type == KEY_TYPE_NORMAL \
and notification_type == EMAIL_TYPE: and notification_type == EMAIL_TYPE:
# Put GOV.UK Email notifications onto a queue # Put GOV.UK Email notifications onto a queue
# To take the pressure off the db for API requests put the notification for our high volume service onto a queue # To take the pressure off the db for API requests put the notification for our high volume service onto a queue
# the task will then save the notification, then call send_notification_to_queue. # the task will then save the notification, then call send_notification_to_queue.
# We know that this team does not use the GET request, but relies on callbacks to get the status updates. # We know that this team does not use the GET request, but relies on callbacks to get the status updates.
try: try:
db.session.commit()
save_email_to_queue( save_email_to_queue(
form=form, form=form,
notification_id=str(notification_id), notification_id=str(notification_id),
notification_type=notification_type, notification_type=notification_type,
api_key=api_key, api_key_id=api_key_id,
template=template, key_type=key_type,
template_id=template.id,
template_version=template_version,
service_id=service.id, service_id=service.id,
personalisation=personalisation, personalisation=personalisation,
document_download_count=document_download_count, document_download_count=document_download_count,
@@ -229,12 +234,12 @@ def process_sms_or_email_notification(
persist_notification( persist_notification(
notification_id=notification_id, notification_id=notification_id,
template_id=template.id, template_id=template.id,
template_version=template._template['version'], template_version=template_version,
recipient=form_send_to, recipient=form_send_to,
service=service, service=service,
personalisation=personalisation, personalisation=personalisation,
notification_type=notification_type, notification_type=notification_type,
api_key_id=api_key.id, api_key_id=api_key_id,
key_type=key_type, key_type=key_type,
client_reference=form.get('reference', None), client_reference=form.get('reference', None),
simulated=simulated, simulated=simulated,
@@ -266,23 +271,26 @@ def save_email_to_queue(
notification_id, notification_id,
form, form,
notification_type, notification_type,
api_key, api_key_id,
template, key_type,
template_id,
template_version,
service_id, service_id,
personalisation, personalisation,
document_download_count, document_download_count,
reply_to_text=None reply_to_text=None
): ):
db.session.commit()
data = { data = {
"id": notification_id, "id": notification_id,
"template_id": str(template.id), "template_id": str(template_id),
"template_version": template._template['version'], "template_version": template_version,
"to": form['email_address'], "to": form['email_address'],
"service_id": str(service_id), "service_id": str(service_id),
"personalisation": personalisation, "personalisation": personalisation,
"notification_type": notification_type, "notification_type": notification_type,
"api_key_id": str(api_key.id), "api_key_id": str(api_key_id),
"key_type": api_key.key_type, "key_type": key_type,
"client_reference": form.get('reference', None), "client_reference": form.get('reference', None),
"reply_to_text": reply_to_text, "reply_to_text": reply_to_text,
"document_download_count": document_download_count, "document_download_count": document_download_count,

View File

@@ -1006,11 +1006,14 @@ def test_post_notifications_saves_email_normally_if_save_email_to_queue_fails(cl
"template_id": template.id, "template_id": template.id,
"personalisation": {"message": "Dear citizen, have a nice day"} "personalisation": {"message": "Dear citizen, have a nice day"}
} }
print("******** Start")
response = client.post( response = client.post(
path='/v2/notifications/email', path='/v2/notifications/email',
data=json.dumps(data), data=json.dumps(data),
headers=[('Content-Type', 'application/json'), create_authorization_header(service_id=service.id)] headers=[('Content-Type', 'application/json'), create_authorization_header(service_id=service.id)]
) )
print("********** End")
json_resp = response.get_json() json_resp = response.get_json()