2017-06-20 17:13:40 +01:00
|
|
|
import json
|
2017-10-11 18:14:56 +01:00
|
|
|
from datetime import datetime
|
2016-11-25 17:32:01 +00:00
|
|
|
|
2016-03-31 15:57:50 +01:00
|
|
|
from flask import current_app
|
2020-04-03 13:13:57 +01:00
|
|
|
from notifications_utils.recipients import RecipientCSV
|
2021-03-10 13:55:06 +00:00
|
|
|
from requests import HTTPError, RequestException, request
|
|
|
|
|
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
2017-11-27 15:11:58 +00:00
|
|
|
|
2023-03-02 20:20:31 -05:00
|
|
|
from app import create_uuid, encryption, notify_celery
|
2016-03-31 15:57:50 +01:00
|
|
|
from app.aws import s3
|
2023-03-02 20:20:31 -05:00
|
|
|
from app.celery import provider_tasks
|
2017-07-19 13:50:29 +01:00
|
|
|
from app.config import QueueNames
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.dao.inbound_sms_dao import dao_get_inbound_sms_by_id
|
|
|
|
|
from app.dao.jobs_dao import dao_get_job_by_id, dao_update_job
|
2017-09-20 11:12:37 +01:00
|
|
|
from app.dao.notifications_dao import (
|
2017-12-01 21:59:46 +00:00
|
|
|
dao_get_last_notification_added_for_job_id,
|
2021-03-10 13:55:06 +00:00
|
|
|
get_notification_by_id,
|
2017-10-27 13:53:55 +01:00
|
|
|
)
|
2018-11-01 17:00:44 +00:00
|
|
|
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
|
2017-06-20 17:13:40 +01:00
|
|
|
from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service
|
2018-11-01 17:00:44 +00:00
|
|
|
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
|
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 (
|
2016-06-29 11:50:54 +01:00
|
|
|
EMAIL_TYPE,
|
2017-04-06 17:16:08 +01:00
|
|
|
JOB_STATUS_CANCELLED,
|
|
|
|
|
JOB_STATUS_FINISHED,
|
2017-10-17 11:07:36 +01:00
|
|
|
JOB_STATUS_IN_PROGRESS,
|
|
|
|
|
JOB_STATUS_PENDING,
|
|
|
|
|
KEY_TYPE_NORMAL,
|
2017-10-16 12:32:44 +01:00
|
|
|
SMS_TYPE,
|
2017-09-26 09:56:09 +01:00
|
|
|
)
|
2016-11-11 10:41:39 +00:00
|
|
|
from app.notifications.process_notifications import persist_notification
|
2021-06-24 11:05:22 +01:00
|
|
|
from app.notifications.validators import check_service_over_daily_message_limit
|
2020-09-26 15:11:38 +01:00
|
|
|
from app.serialised_models import SerialisedService, SerialisedTemplate
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.service.utils import service_allowed_to_send_to
|
2023-03-02 20:20:31 -05:00
|
|
|
from app.utils import DATETIME_FORMAT
|
2021-06-24 11:05:22 +01:00
|
|
|
from app.v2.errors import TooManyRequestsError
|
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")
|
2018-11-01 17:00:44 +00:00
|
|
|
def process_job(job_id, sender_id=None):
|
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)
|
2019-10-01 13:05:05 +01:00
|
|
|
current_app.logger.info("Starting process-job task for job id {} with status: {}".format(job_id, job.job_status))
|
2016-03-09 11:28:52 +00:00
|
|
|
|
2017-02-02 11:34:00 +00:00
|
|
|
if job.job_status != JOB_STATUS_PENDING:
|
2016-10-07 12:54:04 +01:00
|
|
|
return
|
|
|
|
|
|
2016-03-09 11:28:52 +00:00
|
|
|
service = job.service
|
|
|
|
|
|
2021-06-15 07:58:17 +01:00
|
|
|
job.job_status = JOB_STATUS_IN_PROGRESS
|
|
|
|
|
job.processing_started = start
|
|
|
|
|
dao_update_job(job)
|
|
|
|
|
|
2017-02-02 11:34:00 +00:00
|
|
|
if not service.active:
|
|
|
|
|
job.job_status = JOB_STATUS_CANCELLED
|
|
|
|
|
dao_update_job(job)
|
2018-09-19 10:49:11 +01:00
|
|
|
current_app.logger.warning(
|
2017-02-02 11:34:00 +00:00
|
|
|
"Job {} has been cancelled, service {} is inactive".format(job_id, service.id))
|
|
|
|
|
return
|
|
|
|
|
|
2016-11-11 10:41:39 +00:00
|
|
|
if __sending_limits_for_job_exceeded(service, job, job_id):
|
2016-03-09 13:57:53 +00:00
|
|
|
return
|
2016-03-09 11:28:52 +00:00
|
|
|
|
2019-11-08 10:30:26 +00:00
|
|
|
recipient_csv, template, sender_id = get_recipient_csv_and_template_and_sender_id(job)
|
2016-03-09 07:27:26 +00:00
|
|
|
|
2019-10-01 13:05:05 +01:00
|
|
|
current_app.logger.info("Starting job {} processing {} notifications".format(job_id, job.notification_count))
|
2017-10-10 15:04:55 +01:00
|
|
|
|
2019-11-05 16:47:00 +00:00
|
|
|
for row in recipient_csv.get_rows():
|
2018-11-05 16:16:48 +00:00
|
|
|
process_row(row, template, job, service, sender_id=sender_id)
|
2016-02-24 17:12:30 +00:00
|
|
|
|
2018-02-13 18:38:32 +00:00
|
|
|
job_complete(job, start=start)
|
2017-10-13 16:46:17 +01:00
|
|
|
|
|
|
|
|
|
2018-02-13 18:38:32 +00:00
|
|
|
def job_complete(job, resumed=False, start=None):
|
|
|
|
|
job.job_status = JOB_STATUS_FINISHED
|
2017-04-05 11:57:56 +01:00
|
|
|
|
2016-02-25 11:23:04 +00:00
|
|
|
finished = datetime.utcnow()
|
|
|
|
|
job.processing_finished = finished
|
2016-02-24 17:12:30 +00:00
|
|
|
dao_update_job(job)
|
2017-10-13 16:46:17 +01:00
|
|
|
|
|
|
|
|
if resumed:
|
|
|
|
|
current_app.logger.info(
|
2017-12-08 17:32:18 +00:00
|
|
|
"Resumed Job {} completed at {}".format(job.id, job.created_at)
|
2017-10-13 16:46:17 +01:00
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-11-08 10:30:26 +00:00
|
|
|
def get_recipient_csv_and_template_and_sender_id(job):
|
2019-11-05 16:47:00 +00:00
|
|
|
db_template = dao_get_template_by_id(job.template_id, job.template_version)
|
2020-04-06 12:50:22 +01:00
|
|
|
template = db_template._as_utils_template()
|
2019-11-05 16:47:00 +00:00
|
|
|
|
2019-11-13 13:40:42 +00:00
|
|
|
contents, meta_data = s3.get_job_and_metadata_from_s3(service_id=str(job.service_id), job_id=str(job.id))
|
2020-04-20 15:18:40 +01:00
|
|
|
recipient_csv = RecipientCSV(contents, template=template)
|
2019-11-08 10:30:26 +00:00
|
|
|
|
|
|
|
|
return recipient_csv, template, meta_data.get("sender_id")
|
2019-11-05 16:47:00 +00:00
|
|
|
|
|
|
|
|
|
2018-11-05 16:16:48 +00:00
|
|
|
def process_row(row, template, job, service, sender_id=None):
|
2017-01-17 12:00:34 +00:00
|
|
|
template_type = template.template_type
|
|
|
|
|
encrypted = encryption.encrypt({
|
|
|
|
|
'template': str(template.id),
|
|
|
|
|
'template_version': job.template_version,
|
|
|
|
|
'job': str(job.id),
|
2018-03-05 15:55:46 +00:00
|
|
|
'to': row.recipient,
|
|
|
|
|
'row_number': row.index,
|
|
|
|
|
'personalisation': dict(row.personalisation)
|
2017-01-17 12:00:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
send_fns = {
|
2017-10-18 17:00:37 +01:00
|
|
|
SMS_TYPE: save_sms,
|
2023-03-02 20:20:31 -05:00
|
|
|
EMAIL_TYPE: save_email
|
2017-01-17 12:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
send_fn = send_fns[template_type]
|
|
|
|
|
|
2018-11-05 16:16:48 +00:00
|
|
|
task_kwargs = {}
|
|
|
|
|
if sender_id:
|
|
|
|
|
task_kwargs['sender_id'] = sender_id
|
|
|
|
|
|
2019-09-26 14:19:09 +01:00
|
|
|
notification_id = create_uuid()
|
2017-01-18 11:29:38 +00:00
|
|
|
send_fn.apply_async(
|
|
|
|
|
(
|
|
|
|
|
str(service.id),
|
2019-09-26 14:19:09 +01:00
|
|
|
notification_id,
|
2017-01-18 11:29:38 +00:00
|
|
|
encrypted,
|
|
|
|
|
),
|
2018-11-05 16:16:48 +00:00
|
|
|
task_kwargs,
|
2017-05-25 10:51:49 +01:00
|
|
|
queue=QueueNames.DATABASE if not service.research_mode else QueueNames.RESEARCH_MODE
|
2017-01-17 12:00:34 +00:00
|
|
|
)
|
2019-09-26 14:19:09 +01:00
|
|
|
return notification_id
|
2017-01-17 12:00:34 +00:00
|
|
|
|
|
|
|
|
|
2016-11-11 10:41:39 +00:00
|
|
|
def __sending_limits_for_job_exceeded(service, job, job_id):
|
2021-06-24 11:05:22 +01:00
|
|
|
try:
|
2021-06-28 10:12:43 +01:00
|
|
|
total_sent = check_service_over_daily_message_limit(KEY_TYPE_NORMAL, service)
|
|
|
|
|
if total_sent + job.notification_count > service.message_limit:
|
|
|
|
|
raise TooManyRequestsError(service.message_limit)
|
|
|
|
|
else:
|
|
|
|
|
return False
|
2021-06-24 11:05:22 +01:00
|
|
|
except TooManyRequestsError:
|
2016-11-11 10:41:39 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2017-10-18 17:00:37 +01:00
|
|
|
@notify_celery.task(bind=True, name="save-sms", max_retries=5, default_retry_delay=300)
|
|
|
|
|
def save_sms(self,
|
|
|
|
|
service_id,
|
|
|
|
|
notification_id,
|
2018-11-01 17:00:44 +00:00
|
|
|
encrypted_notification,
|
|
|
|
|
sender_id=None):
|
2017-10-18 17:00:37 +01:00
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2020-09-26 15:11:38 +01:00
|
|
|
service = SerialisedService.from_id(service_id)
|
|
|
|
|
template = SerialisedTemplate.from_id_and_service_id(
|
|
|
|
|
notification['template'],
|
|
|
|
|
service_id=service.id,
|
|
|
|
|
version=notification['template_version'],
|
|
|
|
|
)
|
2017-10-18 17:00:37 +01:00
|
|
|
|
2018-11-01 17:00:44 +00:00
|
|
|
if sender_id:
|
|
|
|
|
reply_to_text = dao_get_service_sms_senders_by_id(service_id, sender_id).sms_sender
|
|
|
|
|
else:
|
2020-09-26 15:11:38 +01:00
|
|
|
reply_to_text = template.reply_to_text
|
2018-11-01 17:00:44 +00:00
|
|
|
|
2018-11-01 16:06:07 +00:00
|
|
|
if not service_allowed_to_send_to(notification['to'], service, KEY_TYPE_NORMAL):
|
As Notify matures we probably need less logging, especially to report happy path events.
This PR is a proposal to reduce the average messages we see for a single notification from about 7 messages to 2.
Messaging would change to something like this:
February 2nd 2018, 15:39:05.885 Full delivery response from Firetext for notification: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
{'status': ['0'], 'reference': ['8eda51d5-cd82-4569-bfc9-d5570cdf2126'], 'time': ['2018-02-02 15:39:01'], 'code': ['000']}
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:57.727 SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to provider firetext at 2018-02-02 15:38:56.716814
February 2nd 2018, 15:38:56.727 Starting sending SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 to provider at 2018-02-02 15:38:56.408181
February 2nd 2018, 15:38:56.727 Firetext request for 8eda51d5-cd82-4569-bfc9-d5570cdf2126 finished in 0.30376038211397827
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to the priority-tasks queue for delivery
To somthing like this:
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
2018-02-02 15:55:25 +00:00
|
|
|
current_app.logger.debug(
|
2017-10-18 17:00:37 +01:00
|
|
|
"SMS {} failed as restricted service".format(notification_id)
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
template_id=notification['template'],
|
|
|
|
|
template_version=notification['template_version'],
|
|
|
|
|
recipient=notification['to'],
|
|
|
|
|
service=service,
|
|
|
|
|
personalisation=notification.get('personalisation'),
|
|
|
|
|
notification_type=SMS_TYPE,
|
2018-11-01 16:06:07 +00:00
|
|
|
api_key_id=None,
|
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
2017-10-18 17:00:37 +01:00
|
|
|
created_at=datetime.utcnow(),
|
|
|
|
|
job_id=notification.get('job', None),
|
|
|
|
|
job_row_number=notification.get('row_number', None),
|
2017-11-25 11:31:36 +00:00
|
|
|
notification_id=notification_id,
|
2018-11-01 17:00:44 +00:00
|
|
|
reply_to_text=reply_to_text
|
2017-10-18 17:00:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
provider_tasks.deliver_sms.apply_async(
|
|
|
|
|
[str(saved_notification.id)],
|
|
|
|
|
queue=QueueNames.SEND_SMS if not service.research_mode else QueueNames.RESEARCH_MODE
|
|
|
|
|
)
|
|
|
|
|
|
As Notify matures we probably need less logging, especially to report happy path events.
This PR is a proposal to reduce the average messages we see for a single notification from about 7 messages to 2.
Messaging would change to something like this:
February 2nd 2018, 15:39:05.885 Full delivery response from Firetext for notification: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
{'status': ['0'], 'reference': ['8eda51d5-cd82-4569-bfc9-d5570cdf2126'], 'time': ['2018-02-02 15:39:01'], 'code': ['000']}
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:57.727 SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to provider firetext at 2018-02-02 15:38:56.716814
February 2nd 2018, 15:38:56.727 Starting sending SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 to provider at 2018-02-02 15:38:56.408181
February 2nd 2018, 15:38:56.727 Firetext request for 8eda51d5-cd82-4569-bfc9-d5570cdf2126 finished in 0.30376038211397827
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to the priority-tasks queue for delivery
To somthing like this:
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
2018-02-02 15:55:25 +00:00
|
|
|
current_app.logger.debug(
|
2017-10-18 17:00:37 +01:00
|
|
|
"SMS {} created at {} for job {}".format(
|
|
|
|
|
saved_notification.id,
|
|
|
|
|
saved_notification.created_at,
|
|
|
|
|
notification.get('job', None))
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
handle_exception(self, notification, notification_id, e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(bind=True, name="save-email", max_retries=5, default_retry_delay=300)
|
|
|
|
|
def save_email(self,
|
|
|
|
|
service_id,
|
|
|
|
|
notification_id,
|
2018-11-01 17:00:44 +00:00
|
|
|
encrypted_notification,
|
|
|
|
|
sender_id=None):
|
2017-10-18 17:00:37 +01:00
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2017-12-15 17:15:31 +00:00
|
|
|
|
2020-09-26 15:11:38 +01:00
|
|
|
service = SerialisedService.from_id(service_id)
|
|
|
|
|
template = SerialisedTemplate.from_id_and_service_id(
|
|
|
|
|
notification['template'],
|
|
|
|
|
service_id=service.id,
|
|
|
|
|
version=notification['template_version'],
|
|
|
|
|
)
|
2017-10-18 17:00:37 +01:00
|
|
|
|
2018-11-01 17:00:44 +00:00
|
|
|
if sender_id:
|
|
|
|
|
reply_to_text = dao_get_reply_to_by_id(service_id, sender_id).email_address
|
|
|
|
|
else:
|
2020-09-26 15:11:38 +01:00
|
|
|
reply_to_text = template.reply_to_text
|
2018-11-01 17:00:44 +00:00
|
|
|
|
2018-11-01 16:06:07 +00:00
|
|
|
if not service_allowed_to_send_to(notification['to'], service, KEY_TYPE_NORMAL):
|
2017-10-18 17:00:37 +01:00
|
|
|
current_app.logger.info("Email {} failed as restricted service".format(notification_id))
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
template_id=notification['template'],
|
|
|
|
|
template_version=notification['template_version'],
|
|
|
|
|
recipient=notification['to'],
|
|
|
|
|
service=service,
|
|
|
|
|
personalisation=notification.get('personalisation'),
|
|
|
|
|
notification_type=EMAIL_TYPE,
|
2018-11-01 16:06:07 +00:00
|
|
|
api_key_id=None,
|
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
2017-10-18 17:00:37 +01:00
|
|
|
created_at=datetime.utcnow(),
|
|
|
|
|
job_id=notification.get('job', None),
|
|
|
|
|
job_row_number=notification.get('row_number', None),
|
2017-11-25 11:31:36 +00:00
|
|
|
notification_id=notification_id,
|
2018-11-01 17:00:44 +00:00
|
|
|
reply_to_text=reply_to_text
|
2017-10-18 17:00:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
provider_tasks.deliver_email.apply_async(
|
|
|
|
|
[str(saved_notification.id)],
|
|
|
|
|
queue=QueueNames.SEND_EMAIL if not service.research_mode else QueueNames.RESEARCH_MODE
|
|
|
|
|
)
|
|
|
|
|
|
As Notify matures we probably need less logging, especially to report happy path events.
This PR is a proposal to reduce the average messages we see for a single notification from about 7 messages to 2.
Messaging would change to something like this:
February 2nd 2018, 15:39:05.885 Full delivery response from Firetext for notification: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
{'status': ['0'], 'reference': ['8eda51d5-cd82-4569-bfc9-d5570cdf2126'], 'time': ['2018-02-02 15:39:01'], 'code': ['000']}
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:57.727 SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to provider firetext at 2018-02-02 15:38:56.716814
February 2nd 2018, 15:38:56.727 Starting sending SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 to provider at 2018-02-02 15:38:56.408181
February 2nd 2018, 15:38:56.727 Firetext request for 8eda51d5-cd82-4569-bfc9-d5570cdf2126 finished in 0.30376038211397827
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to the priority-tasks queue for delivery
To somthing like this:
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
2018-02-02 15:55:25 +00:00
|
|
|
current_app.logger.debug("Email {} created at {}".format(saved_notification.id, saved_notification.created_at))
|
2017-10-18 17:00:37 +01:00
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
handle_exception(self, notification, notification_id, e)
|
|
|
|
|
|
|
|
|
|
|
2020-03-25 07:59:05 +00:00
|
|
|
@notify_celery.task(bind=True, name="save-api-email", max_retries=5, default_retry_delay=300)
|
2020-10-29 11:12:46 +00:00
|
|
|
def save_api_email(self, encrypted_notification):
|
2020-03-25 07:59:05 +00:00
|
|
|
|
2020-10-29 11:12:46 +00:00
|
|
|
save_api_email_or_sms(self, encrypted_notification)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(bind=True, name="save-api-sms", max_retries=5, default_retry_delay=300)
|
|
|
|
|
def save_api_sms(self, encrypted_notification):
|
|
|
|
|
save_api_email_or_sms(self, encrypted_notification)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_api_email_or_sms(self, encrypted_notification):
|
2020-03-25 07:59:05 +00:00
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2020-09-26 15:11:38 +01:00
|
|
|
service = SerialisedService.from_id(notification['service_id'])
|
2020-10-29 11:12:46 +00:00
|
|
|
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
|
2020-03-25 07:59:05 +00:00
|
|
|
try:
|
|
|
|
|
|
2020-03-25 12:39:15 +00:00
|
|
|
persist_notification(
|
2020-03-25 07:59:05 +00:00
|
|
|
notification_id=notification["id"],
|
|
|
|
|
template_id=notification['template_id'],
|
|
|
|
|
template_version=notification['template_version'],
|
|
|
|
|
recipient=notification['to'],
|
|
|
|
|
service=service,
|
|
|
|
|
personalisation=notification.get('personalisation'),
|
2020-10-29 11:12:46 +00:00
|
|
|
notification_type=notification['notification_type'],
|
2020-03-25 07:59:05 +00:00
|
|
|
client_reference=notification['client_reference'],
|
|
|
|
|
api_key_id=notification.get('api_key_id'),
|
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
|
|
|
|
created_at=notification['created_at'],
|
|
|
|
|
reply_to_text=notification['reply_to_text'],
|
|
|
|
|
status=notification['status'],
|
|
|
|
|
document_download_count=notification['document_download_count']
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-29 11:12:46 +00:00
|
|
|
q = q if not service.research_mode else QueueNames.RESEARCH_MODE
|
|
|
|
|
provider_task.apply_async(
|
2020-03-25 07:59:05 +00:00
|
|
|
[notification['id']],
|
|
|
|
|
queue=q
|
|
|
|
|
)
|
2020-10-29 11:12:46 +00:00
|
|
|
current_app.logger.debug(
|
|
|
|
|
f"{notification['notification_type']} {notification['id']} has been persisted and sent to delivery queue."
|
|
|
|
|
)
|
2020-03-25 12:39:15 +00:00
|
|
|
except IntegrityError:
|
2020-10-29 11:12:46 +00:00
|
|
|
current_app.logger.info(f"{notification['notification_type']} {notification['id']} already exists.")
|
2020-03-25 07:59:05 +00:00
|
|
|
|
2020-03-26 08:44:28 +00:00
|
|
|
except SQLAlchemyError:
|
2020-03-25 12:39:15 +00:00
|
|
|
|
2020-03-25 07:59:05 +00:00
|
|
|
try:
|
2020-03-26 08:44:28 +00:00
|
|
|
self.retry(queue=QueueNames.RETRY)
|
2020-03-25 07:59:05 +00:00
|
|
|
except self.MaxRetriesExceededError:
|
2020-12-07 15:15:50 +00:00
|
|
|
current_app.logger.error(f"Max retry failed Failed to persist notification {notification['id']}")
|
2020-03-25 07:59:05 +00:00
|
|
|
|
|
|
|
|
|
2017-01-17 16:51:27 +00:00
|
|
|
def handle_exception(task, notification, notification_id, exc):
|
|
|
|
|
if not get_notification_by_id(notification_id):
|
|
|
|
|
retry_msg = '{task} notification for job {job} row number {row} and notification id {noti}'.format(
|
|
|
|
|
task=task.__name__,
|
|
|
|
|
job=notification.get('job', None),
|
|
|
|
|
row=notification.get('row_number', None),
|
|
|
|
|
noti=notification_id
|
|
|
|
|
)
|
|
|
|
|
# Sometimes, SQS plays the same message twice. We should be able to catch an IntegrityError, but it seems
|
|
|
|
|
# SQLAlchemy is throwing a FlushError. So we check if the notification id already exists then do not
|
|
|
|
|
# send to the retry queue.
|
2022-09-28 16:27:37 -04:00
|
|
|
# This probably (hopefully) is not an issue with Redis as the celery backing store
|
2017-01-17 16:51:27 +00:00
|
|
|
current_app.logger.exception('Retry' + retry_msg)
|
|
|
|
|
try:
|
2017-05-25 10:51:49 +01:00
|
|
|
task.retry(queue=QueueNames.RETRY, exc=exc)
|
2017-01-17 16:51:27 +00:00
|
|
|
except task.MaxRetriesExceededError:
|
2018-10-22 11:11:07 +01:00
|
|
|
current_app.logger.error('Max retry failed' + retry_msg)
|
2017-01-17 16:51:27 +00:00
|
|
|
|
|
|
|
|
|
2017-06-20 17:13:40 +01:00
|
|
|
@notify_celery.task(bind=True, name="send-inbound-sms", max_retries=5, default_retry_delay=300)
|
|
|
|
|
def send_inbound_sms_to_service(self, inbound_sms_id, service_id):
|
|
|
|
|
inbound_api = get_service_inbound_api_for_service(service_id=service_id)
|
|
|
|
|
if not inbound_api:
|
|
|
|
|
# No API data has been set for this service
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
inbound_sms = dao_get_inbound_sms_by_id(service_id=service_id,
|
|
|
|
|
inbound_id=inbound_sms_id)
|
|
|
|
|
data = {
|
|
|
|
|
"id": str(inbound_sms.id),
|
2017-11-23 15:22:18 +00:00
|
|
|
# TODO: should we be validating and formatting the phone number here?
|
2017-06-22 10:15:08 +01:00
|
|
|
"source_number": inbound_sms.user_number,
|
|
|
|
|
"destination_number": inbound_sms.notify_number,
|
|
|
|
|
"message": inbound_sms.content,
|
2017-06-20 17:13:40 +01:00
|
|
|
"date_received": inbound_sms.provider_date.strftime(DATETIME_FORMAT)
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 16:15:21 +00:00
|
|
|
try:
|
|
|
|
|
response = request(
|
|
|
|
|
method="POST",
|
|
|
|
|
url=inbound_api.url,
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers={
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Authorization': 'Bearer {}'.format(inbound_api.bearer_token)
|
|
|
|
|
},
|
|
|
|
|
timeout=60
|
|
|
|
|
)
|
2020-11-13 16:41:48 +00:00
|
|
|
current_app.logger.debug(
|
|
|
|
|
f"send_inbound_sms_to_service sending {inbound_sms_id} to {inbound_api.url}, " +
|
|
|
|
|
f"response {response.status_code}"
|
|
|
|
|
)
|
2017-12-01 16:15:21 +00:00
|
|
|
response.raise_for_status()
|
|
|
|
|
except RequestException as e:
|
|
|
|
|
current_app.logger.warning(
|
2020-11-13 16:41:48 +00:00
|
|
|
f"send_inbound_sms_to_service failed for service_id: {service_id} for inbound_sms_id: {inbound_sms_id} " +
|
|
|
|
|
f"and url: {inbound_api.url}. exception: {e}"
|
2017-12-01 16:15:21 +00:00
|
|
|
)
|
|
|
|
|
if not isinstance(e, HTTPError) or e.response.status_code >= 500:
|
|
|
|
|
try:
|
|
|
|
|
self.retry(queue=QueueNames.RETRY)
|
|
|
|
|
except self.MaxRetriesExceededError:
|
2018-10-22 11:11:07 +01:00
|
|
|
current_app.logger.error(
|
2020-12-07 15:15:50 +00:00
|
|
|
"Retry: send_inbound_sms_to_service has retried the max number of" +
|
2020-06-24 12:41:56 +01:00
|
|
|
f"times for service: {service_id} and inbound_sms {inbound_sms_id}"
|
2018-10-22 11:11:07 +01:00
|
|
|
)
|
2020-11-13 16:41:48 +00:00
|
|
|
else:
|
|
|
|
|
current_app.logger.warning(
|
|
|
|
|
f"send_inbound_sms_to_service is not being retried for service_id: {service_id} for " +
|
|
|
|
|
f"inbound_sms id: {inbound_sms_id} and url: {inbound_api.url}. exception: {e}"
|
|
|
|
|
)
|
2017-12-01 14:06:13 +00:00
|
|
|
|
|
|
|
|
|
2017-10-16 12:32:44 +01:00
|
|
|
@notify_celery.task(name='process-incomplete-jobs')
|
|
|
|
|
def process_incomplete_jobs(job_ids):
|
2018-03-09 17:16:48 +00:00
|
|
|
jobs = [dao_get_job_by_id(job_id) for job_id in job_ids]
|
|
|
|
|
|
|
|
|
|
# reset the processing start time so that the check_job_status scheduled task doesn't pick this job up again
|
|
|
|
|
for job in jobs:
|
|
|
|
|
job.job_status = JOB_STATUS_IN_PROGRESS
|
|
|
|
|
job.processing_started = datetime.utcnow()
|
|
|
|
|
dao_update_job(job)
|
|
|
|
|
|
2017-10-16 12:32:44 +01:00
|
|
|
current_app.logger.info("Resuming Job(s) {}".format(job_ids))
|
|
|
|
|
for job_id in job_ids:
|
|
|
|
|
process_incomplete_job(job_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_incomplete_job(job_id):
|
2017-10-17 11:07:36 +01:00
|
|
|
job = dao_get_job_by_id(job_id)
|
2017-10-16 12:32:44 +01:00
|
|
|
|
2017-10-17 11:07:36 +01:00
|
|
|
last_notification_added = dao_get_last_notification_added_for_job_id(job_id)
|
2017-10-16 12:32:44 +01:00
|
|
|
|
|
|
|
|
if last_notification_added:
|
|
|
|
|
resume_from_row = last_notification_added.job_row_number
|
|
|
|
|
else:
|
|
|
|
|
resume_from_row = -1 # The first row in the csv with a number is row 0
|
|
|
|
|
|
|
|
|
|
current_app.logger.info("Resuming job {} from row {}".format(job_id, resume_from_row))
|
|
|
|
|
|
2019-11-08 10:30:26 +00:00
|
|
|
recipient_csv, template, sender_id = get_recipient_csv_and_template_and_sender_id(job)
|
2017-10-16 12:32:44 +01:00
|
|
|
|
2019-11-05 16:47:00 +00:00
|
|
|
for row in recipient_csv.get_rows():
|
2018-03-05 15:55:46 +00:00
|
|
|
if row.index > resume_from_row:
|
2019-11-08 10:30:26 +00:00
|
|
|
process_row(row, template, job, job.service, sender_id=sender_id)
|
2017-10-16 12:32:44 +01:00
|
|
|
|
2018-02-13 18:38:32 +00:00
|
|
|
job_complete(job, resumed=True)
|