2016-04-05 14:51:55 +01:00
|
|
|
import itertools
|
2016-06-20 13:33:53 +01:00
|
|
|
from datetime import (datetime)
|
2016-03-31 15:57:50 +01:00
|
|
|
|
|
|
|
|
from flask import current_app
|
2016-04-13 15:31:08 +01:00
|
|
|
from notifications_utils.recipients import (
|
2016-03-31 15:57:50 +01:00
|
|
|
RecipientCSV,
|
2016-04-05 14:51:55 +01:00
|
|
|
allowed_to_send_to
|
2016-03-31 15:57:50 +01:00
|
|
|
)
|
2016-06-20 13:33:53 +01:00
|
|
|
from notifications_utils.template import Template
|
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2016-03-31 15:57:50 +01:00
|
|
|
|
|
|
|
|
from app import (
|
|
|
|
|
create_uuid,
|
|
|
|
|
DATETIME_FORMAT,
|
|
|
|
|
notify_celery,
|
2016-05-10 09:04:22 +01:00
|
|
|
encryption
|
2016-03-31 15:57:50 +01:00
|
|
|
)
|
|
|
|
|
from app.aws import s3
|
2016-07-01 14:14:28 +01:00
|
|
|
from app.celery.provider_tasks import send_sms_to_provider, send_email_to_provider
|
2016-06-20 13:33:53 +01:00
|
|
|
from app.dao.jobs_dao import (
|
|
|
|
|
dao_update_job,
|
|
|
|
|
dao_get_job_by_id
|
|
|
|
|
)
|
2016-08-25 15:41:37 +01:00
|
|
|
from app.dao.notifications_dao import (dao_create_notification)
|
2016-08-25 11:55:38 +01:00
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id, dao_fetch_todays_stats_for_service
|
2016-06-20 13:33:53 +01:00
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2016-03-09 17:46:01 +00:00
|
|
|
from app.models import (
|
|
|
|
|
Notification,
|
2016-06-29 11:50:54 +01:00
|
|
|
EMAIL_TYPE,
|
2016-06-30 17:32:49 +01:00
|
|
|
SMS_TYPE,
|
2016-09-07 09:57:20 +01:00
|
|
|
KEY_TYPE_NORMAL,
|
|
|
|
|
KEY_TYPE_TEST
|
2016-03-09 17:46:01 +00:00
|
|
|
)
|
2016-08-05 10:44:43 +01:00
|
|
|
from app.statsd_decorators import statsd
|
2016-03-31 15:57:50 +01:00
|
|
|
|
2016-03-09 14:41:36 +00:00
|
|
|
|
2016-02-24 17:12:30 +00:00
|
|
|
@notify_celery.task(name="process-job")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-02-24 17:12:30 +00:00
|
|
|
def process_job(job_id):
|
2016-02-25 11:23:04 +00:00
|
|
|
start = datetime.utcnow()
|
2016-02-24 17:12:30 +00:00
|
|
|
job = dao_get_job_by_id(job_id)
|
2016-03-09 11:28:52 +00:00
|
|
|
|
|
|
|
|
service = job.service
|
|
|
|
|
|
2016-08-25 11:55:38 +01:00
|
|
|
total_sent = sum(row.count for row in dao_fetch_todays_stats_for_service(service.id))
|
2016-03-09 11:28:52 +00:00
|
|
|
|
2016-04-08 16:13:10 +01:00
|
|
|
if total_sent + job.notification_count > service.message_limit:
|
2016-03-09 13:57:53 +00:00
|
|
|
job.status = 'sending limits exceeded'
|
|
|
|
|
job.processing_finished = datetime.utcnow()
|
|
|
|
|
dao_update_job(job)
|
|
|
|
|
current_app.logger.info(
|
2016-04-08 16:13:10 +01:00
|
|
|
"Job {} size {} error. Sending limits {} exceeded".format(
|
|
|
|
|
job_id, job.notification_count, service.message_limit)
|
2016-03-09 13:57:53 +00:00
|
|
|
)
|
|
|
|
|
return
|
2016-03-09 11:28:52 +00:00
|
|
|
|
2016-02-24 17:12:30 +00:00
|
|
|
job.status = 'in progress'
|
|
|
|
|
dao_update_job(job)
|
|
|
|
|
|
2016-03-09 07:27:26 +00:00
|
|
|
template = Template(
|
2016-05-11 17:04:51 +01:00
|
|
|
dao_get_template_by_id(job.template_id, job.template_version).__dict__
|
2016-03-09 07:27:26 +00:00
|
|
|
)
|
|
|
|
|
|
2016-05-19 10:46:03 +01:00
|
|
|
for row_number, recipient, personalisation in RecipientCSV(
|
2016-04-07 13:44:04 +01:00
|
|
|
s3.get_job_from_s3(str(service.id), str(job_id)),
|
2016-03-09 14:41:36 +00:00
|
|
|
template_type=template.template_type,
|
|
|
|
|
placeholders=template.placeholders
|
2016-05-19 10:46:03 +01:00
|
|
|
).enumerated_recipients_and_personalisation:
|
2016-02-29 14:43:44 +00:00
|
|
|
|
2016-02-25 09:59:50 +00:00
|
|
|
encrypted = encryption.encrypt({
|
2016-04-08 16:13:10 +01:00
|
|
|
'template': str(template.id),
|
2016-05-11 17:04:51 +01:00
|
|
|
'template_version': job.template_version,
|
2016-02-24 17:12:30 +00:00
|
|
|
'job': str(job.id),
|
2016-03-06 12:51:45 +00:00
|
|
|
'to': recipient,
|
2016-05-19 10:46:03 +01:00
|
|
|
'row_number': row_number,
|
2016-04-29 17:24:00 +01:00
|
|
|
'personalisation': {
|
|
|
|
|
key: personalisation.get(key)
|
|
|
|
|
for key in template.placeholders
|
2016-08-23 12:05:47 +01:00
|
|
|
}
|
2016-02-24 17:12:30 +00:00
|
|
|
})
|
|
|
|
|
|
2016-06-29 11:50:54 +01:00
|
|
|
if template.template_type == SMS_TYPE:
|
2016-02-25 09:59:50 +00:00
|
|
|
send_sms.apply_async((
|
|
|
|
|
str(job.service_id),
|
2016-04-29 14:36:10 +01:00
|
|
|
create_uuid(),
|
2016-02-25 11:23:04 +00:00
|
|
|
encrypted,
|
2016-03-08 17:45:37 +00:00
|
|
|
datetime.utcnow().strftime(DATETIME_FORMAT)),
|
2016-08-30 17:27:01 +01:00
|
|
|
queue='db-sms'
|
2016-02-25 09:59:50 +00:00
|
|
|
)
|
|
|
|
|
|
2016-06-29 11:50:54 +01:00
|
|
|
if template.template_type == EMAIL_TYPE:
|
2016-05-17 12:41:13 +01:00
|
|
|
send_email.apply_async((
|
|
|
|
|
str(job.service_id),
|
|
|
|
|
create_uuid(),
|
2016-02-25 11:23:04 +00:00
|
|
|
encrypted,
|
2016-03-08 17:45:37 +00:00
|
|
|
datetime.utcnow().strftime(DATETIME_FORMAT)),
|
2016-08-30 17:27:01 +01:00
|
|
|
queue='db-email')
|
2016-02-24 17:12:30 +00:00
|
|
|
|
2016-02-25 11:23:04 +00:00
|
|
|
finished = datetime.utcnow()
|
2016-02-24 17:12:30 +00:00
|
|
|
job.status = 'finished'
|
2016-02-25 11:23:04 +00:00
|
|
|
job.processing_started = start
|
|
|
|
|
job.processing_finished = finished
|
2016-02-24 17:12:30 +00:00
|
|
|
dao_update_job(job)
|
2016-02-25 11:23:04 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"Job {} created at {} started at {} finished at {}".format(job_id, job.created_at, start, finished)
|
|
|
|
|
)
|
2016-02-09 13:31:45 +00:00
|
|
|
|
|
|
|
|
|
2016-08-10 08:46:37 +01:00
|
|
|
@notify_celery.task(bind=True, name="send-sms", max_retries=5, default_retry_delay=300)
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-30 17:32:49 +01:00
|
|
|
def send_sms(self,
|
|
|
|
|
service_id,
|
|
|
|
|
notification_id,
|
|
|
|
|
encrypted_notification,
|
|
|
|
|
created_at,
|
|
|
|
|
api_key_id=None,
|
|
|
|
|
key_type=KEY_TYPE_NORMAL):
|
2016-02-16 15:28:30 +00:00
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2016-03-01 08:48:27 +00:00
|
|
|
service = dao_fetch_service_by_id(service_id)
|
2016-05-10 09:04:22 +01:00
|
|
|
|
2016-09-07 09:57:20 +01:00
|
|
|
if not service_allowed_to_send_to(notification['to'], service, key_type):
|
2016-04-05 14:51:55 +01:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"SMS {} failed as restricted service".format(notification_id)
|
|
|
|
|
)
|
2016-05-31 14:55:06 +01:00
|
|
|
return
|
2016-03-03 12:05:18 +00:00
|
|
|
|
2016-02-15 16:01:14 +00:00
|
|
|
try:
|
2016-09-07 13:45:37 +01:00
|
|
|
dao_create_notification(
|
|
|
|
|
Notification.from_api_request(
|
|
|
|
|
created_at, notification, notification_id, service.id, SMS_TYPE, api_key_id, key_type
|
|
|
|
|
)
|
|
|
|
|
)
|
2016-08-30 10:42:24 +01:00
|
|
|
send_sms_to_provider.apply_async((service_id, notification_id), queue='send-sms')
|
2016-04-04 15:02:25 +01:00
|
|
|
|
|
|
|
|
current_app.logger.info(
|
2016-07-01 14:14:28 +01:00
|
|
|
"SMS {} created at {}".format(notification_id, created_at)
|
2016-04-04 15:02:25 +01:00
|
|
|
)
|
2016-06-03 14:54:46 +01:00
|
|
|
|
2016-02-22 17:17:29 +00:00
|
|
|
except SQLAlchemyError as e:
|
2016-08-10 08:53:15 +01:00
|
|
|
current_app.logger.exception("RETRY: send_sms notification {}".format(notification_id), e)
|
|
|
|
|
try:
|
|
|
|
|
raise self.retry(queue="retry", exc=e)
|
|
|
|
|
except self.MaxRetriesExceededError:
|
|
|
|
|
current_app.logger.exception(
|
|
|
|
|
"RETRY FAILED: task send_sms failed for notification {}".format(notification.id),
|
|
|
|
|
e
|
|
|
|
|
)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
|
|
|
|
|
2016-08-10 08:46:37 +01:00
|
|
|
@notify_celery.task(bind=True, name="send-email", max_retries=5, default_retry_delay=300)
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-07-01 17:12:03 +01:00
|
|
|
def send_email(self, service_id,
|
2016-06-30 17:32:49 +01:00
|
|
|
notification_id,
|
|
|
|
|
encrypted_notification,
|
|
|
|
|
created_at,
|
|
|
|
|
api_key_id=None,
|
|
|
|
|
key_type=KEY_TYPE_NORMAL):
|
2016-02-22 17:17:29 +00:00
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2016-04-05 14:51:55 +01:00
|
|
|
service = dao_fetch_service_by_id(service_id)
|
2016-02-25 11:23:04 +00:00
|
|
|
|
2016-09-07 09:57:20 +01:00
|
|
|
if not service_allowed_to_send_to(notification['to'], service, key_type):
|
2016-07-01 14:42:40 +01:00
|
|
|
current_app.logger.info("Email {} failed as restricted service".format(notification_id))
|
2016-05-31 14:55:06 +01:00
|
|
|
return
|
2016-03-03 12:05:18 +00:00
|
|
|
|
2016-02-22 17:17:29 +00:00
|
|
|
try:
|
2016-09-07 13:45:37 +01:00
|
|
|
dao_create_notification(
|
|
|
|
|
Notification.from_api_request(
|
|
|
|
|
created_at, notification, notification_id, service.id, EMAIL_TYPE, api_key_id, key_type
|
|
|
|
|
)
|
|
|
|
|
)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
2016-08-30 10:42:24 +01:00
|
|
|
send_email_to_provider.apply_async((service_id, notification_id), queue='send-email')
|
2016-04-04 15:02:25 +01:00
|
|
|
|
2016-07-01 14:14:28 +01:00
|
|
|
current_app.logger.info("Email {} created at {}".format(notification_id, created_at))
|
2016-02-16 17:42:04 +00:00
|
|
|
except SQLAlchemyError as e:
|
2016-08-10 08:53:15 +01:00
|
|
|
current_app.logger.exception("RETRY: send_email notification {}".format(notification_id), e)
|
|
|
|
|
try:
|
|
|
|
|
raise self.retry(queue="retry", exc=e)
|
|
|
|
|
except self.MaxRetriesExceededError:
|
|
|
|
|
current_app.logger.error(
|
|
|
|
|
"RETRY FAILED: task send_email failed for notification {}".format(notification.id),
|
|
|
|
|
e
|
|
|
|
|
)
|
2016-07-01 14:14:28 +01:00
|
|
|
|
|
|
|
|
|
2016-09-07 09:57:20 +01:00
|
|
|
def service_allowed_to_send_to(recipient, service, key_type):
|
|
|
|
|
if not service.restricted or key_type == KEY_TYPE_TEST:
|
2016-04-05 14:51:55 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
return allowed_to_send_to(
|
|
|
|
|
recipient,
|
|
|
|
|
itertools.chain.from_iterable(
|
|
|
|
|
[user.mobile_number, user.email_address] for user in service.users
|
|
|
|
|
)
|
|
|
|
|
)
|