Refactor send_sms and send_email to use common code to persist the notificaiton.

This commit is contained in:
Rebecca Law
2016-11-11 10:41:39 +00:00
parent abecb5ff98
commit eafbbd9809
4 changed files with 101 additions and 79 deletions

View File

@@ -17,7 +17,7 @@ from app.dao.jobs_dao import (
dao_update_job,
dao_get_job_by_id
)
from app.dao.notifications_dao import (dao_create_notification)
from app.dao.notifications_dao import dao_create_notification
from app.dao.services_dao import dao_fetch_service_by_id, fetch_todays_total_message_count
from app.dao.templates_dao import dao_get_template_by_id
from app.models import (
@@ -26,6 +26,7 @@ from app.models import (
SMS_TYPE,
KEY_TYPE_NORMAL
)
from app.notifications.process_notifications import persist_notification
from app.service.utils import service_allowed_to_send_to
from app.statsd_decorators import statsd
@@ -41,16 +42,7 @@ def process_job(job_id):
service = job.service
total_sent = fetch_todays_total_message_count(service.id)
if total_sent + job.notification_count > service.message_limit:
job.job_status = 'sending limits exceeded'
job.processing_finished = datetime.utcnow()
dao_update_job(job)
current_app.logger.info(
"Job {} size {} error. Sending limits {} exceeded".format(
job_id, job.notification_count, service.message_limit)
)
if __sending_limits_for_job_exceeded(service, job, job_id):
return
job.job_status = 'in progress'
@@ -106,6 +98,21 @@ def process_job(job_id):
)
def __sending_limits_for_job_exceeded(service, job, job_id):
total_sent = fetch_todays_total_message_count(service.id)
if total_sent + job.notification_count > service.message_limit:
job.job_status = 'sending limits exceeded'
job.processing_finished = datetime.utcnow()
dao_update_job(job)
current_app.logger.info(
"Job {} size {} error. Sending limits {} exceeded".format(
job_id, job.notification_count, service.message_limit)
)
return True
return False
@notify_celery.task(bind=True, name="send-sms", max_retries=5, default_retry_delay=300)
@statsd(namespace="tasks")
def send_sms(self,
@@ -125,11 +132,18 @@ def send_sms(self,
return
try:
dao_create_notification(
Notification.from_api_request(
created_at, notification, notification_id, service.id, SMS_TYPE, api_key_id, key_type
)
)
persist_notification(template_id=notification['template'],
template_version=notification['template_version'],
recipient=notification['to'],
service_id=service.id,
personalisation=notification.get('personalisation'),
notification_type=SMS_TYPE,
api_key_id=api_key_id,
key_type=key_type,
job_id=notification.get('job', None),
job_row_number=notification.get('row_number', None),
)
provider_tasks.deliver_sms.apply_async(
[notification_id],
queue='send-sms' if not service.research_mode else 'research-mode'
@@ -166,10 +180,17 @@ def send_email(self, service_id,
return
try:
dao_create_notification(
Notification.from_api_request(
created_at, notification, notification_id, service.id, EMAIL_TYPE, api_key_id, key_type
)
persist_notification(
template_id=notification['template'],
template_version=notification['template_version'],
recipient=notification['to'],
service_id=service.id,
personalisation=notification.get('personalisation'),
notification_type=EMAIL_TYPE,
api_key_id=api_key_id,
key_type=key_type,
job_id=notification.get('job', None),
job_row_number=notification.get('row_number', None),
)
provider_tasks.deliver_email.apply_async(

View File

@@ -572,7 +572,9 @@ class Notification(db.Model):
personalisation,
notification_type,
api_key_id,
key_type):
key_type,
job_id,
job_row_number):
return cls(
template_id=template_id,
template_version=template_version,
@@ -583,7 +585,9 @@ class Notification(db.Model):
personalisation=personalisation,
notification_type=notification_type,
api_key_id=api_key_id,
key_type=key_type
key_type=key_type,
job_id=job_id,
job_row_number=job_row_number,
)

View File

@@ -40,7 +40,9 @@ def persist_notification(template_id,
personalisation,
notification_type,
api_key_id,
key_type):
key_type,
job_id=None,
job_row_number=None):
notification = Notification.from_v2_api_request(template_id=template_id,
template_version=template_version,
recipient=recipient,
@@ -48,7 +50,10 @@ def persist_notification(template_id,
personalisation=personalisation,
notification_type=notification_type,
api_key_id=api_key_id,
key_type=key_type)
key_type=key_type,
job_id=job_id,
job_row_number=job_row_number
)
dao_create_notification(notification)
return notification