Merge pull request #765 from alphagov/use-notify-queue-to-deliver

Send Notify's 2FA codes via only one queue
This commit is contained in:
Jenny Duckett
2016-12-12 15:16:59 +00:00
committed by GitHub
4 changed files with 94 additions and 75 deletions

View File

@@ -70,19 +70,22 @@ def persist_notification(template_id,
return notification
def send_notification_to_queue(notification, research_mode):
try:
research_mode = research_mode or notification.key_type == KEY_TYPE_TEST
def send_notification_to_queue(notification, research_mode, queue=None):
if research_mode or notification.key_type == KEY_TYPE_TEST:
queue = 'research-mode'
elif not queue:
if notification.notification_type == SMS_TYPE:
provider_tasks.deliver_sms.apply_async(
[str(notification.id)],
queue='send-sms' if not research_mode else 'research-mode'
)
queue = 'send-sms'
if notification.notification_type == EMAIL_TYPE:
provider_tasks.deliver_email.apply_async(
[str(notification.id)],
queue='send-email' if not research_mode else 'research-mode'
)
queue = 'send-email'
if notification.notification_type == SMS_TYPE:
deliver_task = provider_tasks.deliver_sms
if notification.notification_type == EMAIL_TYPE:
deliver_task = provider_tasks.deliver_email
try:
deliver_task.apply_async([str(notification.id)], queue=queue)
except Exception as e:
current_app.logger.exception(e)
dao_delete_notifications_and_history_by_id(notification.id)

View File

@@ -18,7 +18,11 @@ from app.dao.users_dao import (
from app.dao.permissions_dao import permission_dao
from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id
from app.models import SMS_TYPE
from app.models import SMS_TYPE, KEY_TYPE_NORMAL
from app.notifications.process_notifications import (
persist_notification,
send_notification_to_queue
)
from app.schemas import (
email_data_request_schema,
user_schema,
@@ -143,20 +147,22 @@ def send_user_sms_code(user_id):
mobile = user_to_send_to.mobile_number if verify_code.get('to', None) is None else verify_code.get('to')
sms_code_template_id = current_app.config['SMS_CODE_TEMPLATE_ID']
sms_code_template = dao_get_template_by_id(sms_code_template_id)
verification_message = encryption.encrypt({
'template': sms_code_template_id,
'template_version': sms_code_template.version,
'to': mobile,
'personalisation': {
'verify_code': secret_code
}
notify_service_id = current_app.config['NOTIFY_SERVICE_ID']
})
send_sms.apply_async([current_app.config['NOTIFY_SERVICE_ID'],
str(uuid.uuid4()),
verification_message,
datetime.utcnow().strftime(DATETIME_FORMAT)
], queue='notify')
saved_notification = persist_notification(
template_id=sms_code_template_id,
template_version=sms_code_template.version,
recipient=mobile,
service_id=notify_service_id,
personalisation={'verify_code': secret_code},
notification_type=SMS_TYPE,
api_key_id=None,
key_type=KEY_TYPE_NORMAL
)
# Assume that we never want to observe the Notify service's research mode
# setting for this notification - we still need to be able to log into the
# admin even if we're doing user research using this service:
send_notification_to_queue(saved_notification, False, queue='notify')
return jsonify({}), 204