Revert "Revert "Add a task to save-api-sms for high volume services.""

This commit is contained in:
Rebecca Law
2020-10-29 11:12:46 +00:00
committed by GitHub
parent d39379062d
commit 29b6f84f6c
7 changed files with 214 additions and 145 deletions

View File

@@ -287,13 +287,23 @@ def save_email(self,
@notify_celery.task(bind=True, name="save-api-email", max_retries=5, default_retry_delay=300)
@statsd(namespace="tasks")
def save_api_email(self,
encrypted_notification,
):
def save_api_email(self, encrypted_notification):
save_api_email_or_sms(self, encrypted_notification)
@notify_celery.task(bind=True, name="save-api-sms", max_retries=5, default_retry_delay=300)
@statsd(namespace="tasks")
def save_api_sms(self, encrypted_notification):
save_api_email_or_sms(self, encrypted_notification)
def save_api_email_or_sms(self, encrypted_notification):
notification = encryption.decrypt(encrypted_notification)
service = dao_fetch_service_by_id(notification['service_id'])
q = QueueNames.SEND_EMAIL if notification['notification_type'] == EMAIL_TYPE else QueueNames.SEND_SMS
provider_task = provider_tasks.deliver_email if notification['notification_type'] == EMAIL_TYPE \
else provider_tasks.deliver_sms
try:
persist_notification(
@@ -303,7 +313,7 @@ def save_api_email(self,
recipient=notification['to'],
service=service,
personalisation=notification.get('personalisation'),
notification_type=EMAIL_TYPE,
notification_type=notification['notification_type'],
client_reference=notification['client_reference'],
api_key_id=notification.get('api_key_id'),
key_type=KEY_TYPE_NORMAL,
@@ -313,14 +323,16 @@ def save_api_email(self,
document_download_count=notification['document_download_count']
)
q = QueueNames.SEND_EMAIL if not service.research_mode else QueueNames.RESEARCH_MODE
provider_tasks.deliver_email.apply_async(
q = q if not service.research_mode else QueueNames.RESEARCH_MODE
provider_task.apply_async(
[notification['id']],
queue=q
)
current_app.logger.debug(f"Email {notification['id']} has been persisted and sent to delivery queue.")
current_app.logger.debug(
f"{notification['notification_type']} {notification['id']} has been persisted and sent to delivery queue."
)
except IntegrityError:
current_app.logger.info(f"Email {notification['id']} already exists.")
current_app.logger.info(f"{notification['notification_type']} {notification['id']} already exists.")
except SQLAlchemyError:

View File

@@ -32,6 +32,7 @@ class QueueNames(object):
ANTIVIRUS = 'antivirus-tasks'
SANITISE_LETTERS = 'sanitise-letter-tasks'
SAVE_API_EMAIL = 'save-api-email-tasks'
SAVE_API_SMS = 'save-api-sms-tasks'
@staticmethod
def all_queues():
@@ -50,7 +51,8 @@ class QueueNames(object):
QueueNames.CALLBACKS,
QueueNames.LETTERS,
QueueNames.SMS_CALLBACKS,
QueueNames.SAVE_API_EMAIL
QueueNames.SAVE_API_EMAIL,
QueueNames.SAVE_API_SMS
]

View File

@@ -18,7 +18,7 @@ from app import (
)
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.tasks import save_api_email
from app.celery.tasks import save_api_email, save_api_sms
from app.clients.document_download import DocumentDownloadError
from app.config import QueueNames, TaskNames
from app.dao.templates_dao import get_precompiled_letter_template
@@ -69,7 +69,6 @@ from app.v2.notifications.notification_schemas import (
)
from app.v2.utils import get_valid_json
POST_NOTIFICATION_JSON_PARSE_DURATION_SECONDS = Histogram(
'post_notification_json_parse_duration_seconds',
'Time taken to parse and validate post request json',
@@ -201,14 +200,16 @@ def process_sms_or_email_notification(
template_with_content=template_with_content
)
if service.id in current_app.config.get('HIGH_VOLUME_SERVICE') and api_user.key_type == KEY_TYPE_NORMAL \
and notification_type == EMAIL_TYPE:
# Put GOV.UK Email notifications onto a queue
if service.id in current_app.config.get('HIGH_VOLUME_SERVICE') \
and api_user.key_type == KEY_TYPE_NORMAL \
and notification_type in [EMAIL_TYPE, SMS_TYPE]:
# Put service with high volumes of 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
# 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.
# NOTE: The high volume service should be aware that the notification is not immediately
# available by a GET request, it is recommend they use callbacks to keep track of status updates.
try:
save_email_to_queue(
save_email_or_sms_to_queue(
form=form,
notification_id=str(notification_id),
notification_type=notification_type,
@@ -258,7 +259,7 @@ def process_sms_or_email_notification(
return resp
def save_email_to_queue(
def save_email_or_sms_to_queue(
*,
notification_id,
form,
@@ -274,7 +275,7 @@ def save_email_to_queue(
"id": notification_id,
"template_id": str(template.id),
"template_version": template.version,
"to": form['email_address'],
"to": form['email_address'] if notification_type == EMAIL_TYPE else form['phone_number'],
"service_id": str(service_id),
"personalisation": personalisation,
"notification_type": notification_type,
@@ -290,7 +291,11 @@ def save_email_to_queue(
data
)
save_api_email.apply_async([encrypted], queue=QueueNames.SAVE_API_EMAIL)
if notification_type == EMAIL_TYPE:
save_api_email.apply_async([encrypted], queue=QueueNames.SAVE_API_EMAIL)
elif notification_type == SMS_TYPE:
save_api_sms.apply_async([encrypted], queue=QueueNames.SAVE_API_SMS)
return Notification(**data)