Add optional queue param to send_notification_to_queue

We want to use this function for sending internal notifications to a different
queue.
This commit is contained in:
Jenny Duckett
2016-12-09 12:10:42 +00:00
parent 32a21bfd3a
commit d5d079a150
2 changed files with 30 additions and 23 deletions

View File

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

View File

@@ -135,24 +135,28 @@ def test_persist_notification_with_optionals(sample_job, sample_api_key, mocker)
assert persisted_notification.reference is None assert persisted_notification.reference is None
@pytest.mark.parametrize('research_mode, queue, notification_type, key_type', @pytest.mark.parametrize('research_mode, requested_queue, expected_queue, notification_type, key_type',
[(True, 'research-mode', 'sms', 'normal'), [(True, None, 'research-mode', 'sms', 'normal'),
(True, 'research-mode', 'email', 'normal'), (True, None, 'research-mode', 'email', 'normal'),
(True, 'research-mode', 'email', 'team'), (True, None, 'research-mode', 'email', 'team'),
(False, 'send-sms', 'sms', 'normal'), (False, None, 'send-sms', 'sms', 'normal'),
(False, 'send-email', 'email', 'normal'), (False, None, 'send-email', 'email', 'normal'),
(False, 'send-sms', 'sms', 'team'), (False, None, 'send-sms', 'sms', 'team'),
(False, 'research-mode', 'sms', 'test')]) (False, None, 'research-mode', 'sms', 'test'),
(True, 'notify', 'research-mode', 'email', 'normal'),
(False, 'notify', 'notify', 'sms', 'normal'),
(False, 'notify', 'notify', 'email', 'normal'),
(False, 'notify', 'research-mode', 'sms', 'test')])
def test_send_notification_to_queue(notify_db, notify_db_session, def test_send_notification_to_queue(notify_db, notify_db_session,
research_mode, notification_type, research_mode, requested_queue, expected_queue,
queue, key_type, mocker): notification_type, key_type, mocker):
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type)) mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type))
template = sample_template(notify_db, notify_db_session) if notification_type == 'sms' \ template = sample_template(notify_db, notify_db_session) if notification_type == 'sms' \
else sample_email_template(notify_db, notify_db_session) else sample_email_template(notify_db, notify_db_session)
notification = sample_notification(notify_db, notify_db_session, template=template, key_type=key_type) notification = sample_notification(notify_db, notify_db_session, template=template, key_type=key_type)
send_notification_to_queue(notification=notification, research_mode=research_mode) send_notification_to_queue(notification=notification, research_mode=research_mode, queue=requested_queue)
mocked.assert_called_once_with([str(notification.id)], queue=queue) mocked.assert_called_once_with([str(notification.id)], queue=expected_queue)
def test_send_notification_to_queue_throws_exception_deletes_notification(sample_notification, mocker): def test_send_notification_to_queue_throws_exception_deletes_notification(sample_notification, mocker):