2016-04-05 14:51:55 +01:00
|
|
|
import itertools
|
2016-06-08 15:25:57 +01:00
|
|
|
from datetime import (datetime, timedelta)
|
2016-03-31 15:57:50 +01:00
|
|
|
|
|
|
|
|
from flask import current_app
|
2016-05-16 11:55:00 +01:00
|
|
|
from monotonic import monotonic
|
2016-06-07 12:53:31 +01:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2016-06-07 11:09:54 +01:00
|
|
|
|
2016-05-13 17:15:39 +01:00
|
|
|
from app import clients, statsd_client
|
2016-06-08 15:25:57 +01:00
|
|
|
from app.clients import STATISTICS_FAILURE
|
2016-05-10 09:04:22 +01:00
|
|
|
from app.clients.email import EmailClientException
|
2016-03-01 08:48:27 +00:00
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
2016-02-24 11:51:02 +00:00
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2016-05-10 09:04:22 +01:00
|
|
|
from app.dao.provider_details_dao import get_provider_details_by_notification_type
|
2016-06-17 16:39:03 +01:00
|
|
|
from app.celery.provider_tasks import send_sms_to_provider
|
2016-06-07 12:53:31 +01:00
|
|
|
from app.celery.research_mode_tasks import send_email_response
|
2016-03-31 15:57:50 +01:00
|
|
|
|
2016-06-17 13:49:33 +01:00
|
|
|
from notifications_utils.template import Template
|
2016-03-31 15:57:50 +01:00
|
|
|
|
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
|
|
|
validate_and_format_phone_number,
|
|
|
|
|
allowed_to_send_to
|
2016-03-31 15:57:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from app import (
|
|
|
|
|
create_uuid,
|
|
|
|
|
DATETIME_FORMAT,
|
|
|
|
|
DATE_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
|
|
|
|
|
from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago
|
|
|
|
|
from app.dao.invited_user_dao import delete_invitations_created_more_than_two_days_ago
|
|
|
|
|
|
2016-03-09 17:46:01 +00:00
|
|
|
from app.dao.notifications_dao import (
|
|
|
|
|
dao_create_notification,
|
|
|
|
|
dao_update_notification,
|
2016-04-05 11:07:21 +01:00
|
|
|
delete_notifications_created_more_than_a_week_ago,
|
2016-03-11 09:40:35 +00:00
|
|
|
dao_get_notification_statistics_for_service_and_day,
|
2016-06-08 15:25:57 +01:00
|
|
|
update_provider_stats,
|
|
|
|
|
get_notifications,
|
|
|
|
|
update_notification_status_by_id
|
2016-03-09 17:46:01 +00:00
|
|
|
)
|
2016-03-31 15:57:50 +01:00
|
|
|
|
|
|
|
|
from app.dao.jobs_dao import (
|
|
|
|
|
dao_update_job,
|
|
|
|
|
dao_get_job_by_id
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-09 17:46:01 +00:00
|
|
|
from app.models import (
|
|
|
|
|
Notification,
|
|
|
|
|
TEMPLATE_TYPE_EMAIL,
|
|
|
|
|
TEMPLATE_TYPE_SMS
|
|
|
|
|
)
|
2016-03-31 15:57:50 +01:00
|
|
|
|
2016-03-09 14:41:36 +00:00
|
|
|
|
2016-03-09 17:46:01 +00:00
|
|
|
@notify_celery.task(name="delete-verify-codes")
|
|
|
|
|
def delete_verify_codes():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_codes_older_created_more_than_a_day_ago()
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete job started {} finished {} deleted {} verify codes".format(start, datetime.utcnow(), deleted)
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError:
|
|
|
|
|
current_app.logger.info("Failed to delete verify codes")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-successful-notifications")
|
|
|
|
|
def delete_successful_notifications():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
2016-04-13 12:49:38 +01:00
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago('delivered')
|
2016-03-09 17:46:01 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete job started {} finished {} deleted {} successful notifications".format(
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError:
|
|
|
|
|
current_app.logger.info("Failed to delete successful notifications")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-failed-notifications")
|
|
|
|
|
def delete_failed_notifications():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
2016-04-05 11:07:21 +01:00
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago('failed')
|
2016-05-17 13:06:08 +01:00
|
|
|
deleted += delete_notifications_created_more_than_a_week_ago('technical-failure')
|
|
|
|
|
deleted += delete_notifications_created_more_than_a_week_ago('temporary-failure')
|
|
|
|
|
deleted += delete_notifications_created_more_than_a_week_ago('permanent-failure')
|
2016-03-09 17:46:01 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete job started {} finished {} deleted {} failed notifications".format(
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError:
|
|
|
|
|
current_app.logger.info("Failed to delete failed notifications")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-invitations")
|
|
|
|
|
def delete_invitations():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
2016-03-10 09:34:27 +00:00
|
|
|
deleted = delete_invitations_created_more_than_two_days_ago()
|
2016-03-09 17:46:01 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete job started {} finished {} deleted {} invitations".format(start, datetime.utcnow(), deleted)
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError:
|
|
|
|
|
current_app.logger.info("Failed to delete invitations")
|
|
|
|
|
raise
|
2016-02-24 17:12:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="process-job")
|
|
|
|
|
def process_job(job_id):
|
2016-05-16 11:55:00 +01:00
|
|
|
task_start = monotonic()
|
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
|
|
|
|
|
|
|
|
|
|
stats = dao_get_notification_statistics_for_service_and_day(
|
|
|
|
|
service_id=service.id,
|
|
|
|
|
day=job.created_at.strftime(DATE_FORMAT)
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-09 13:57:53 +00:00
|
|
|
total_sent = 0
|
2016-03-09 11:28:52 +00:00
|
|
|
if stats:
|
|
|
|
|
total_sent = stats.emails_requested + stats.sms_requested
|
|
|
|
|
|
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-05-10 09:04:22 +01:00
|
|
|
}
|
2016-02-24 17:12:30 +00:00
|
|
|
})
|
|
|
|
|
|
2016-03-09 07:27:26 +00:00
|
|
|
if template.template_type == 'sms':
|
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-02-25 09:59:50 +00:00
|
|
|
queue='bulk-sms'
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-09 07:27:26 +00:00
|
|
|
if template.template_type == 'email':
|
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-05-17 14:31:33 +01:00
|
|
|
{'reply_to_addresses': service.reply_to_email_address},
|
2016-02-25 09:59:50 +00:00
|
|
|
queue='bulk-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-04-05 14:28:19 +01:00
|
|
|
remove_job.apply_async((str(job_id),), queue='remove-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-05-13 17:15:39 +01:00
|
|
|
statsd_client.incr("notifications.tasks.process-job")
|
2016-05-16 11:55:00 +01:00
|
|
|
statsd_client.timing("notifications.tasks.process-job.task-time", monotonic() - task_start)
|
2016-02-09 13:31:45 +00:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:28:19 +01:00
|
|
|
@notify_celery.task(name="remove-job")
|
|
|
|
|
def remove_job(job_id):
|
|
|
|
|
job = dao_get_job_by_id(job_id)
|
2016-04-07 13:44:04 +01:00
|
|
|
s3.remove_job_from_s3(job.service.id, str(job_id))
|
2016-04-05 14:28:19 +01:00
|
|
|
current_app.logger.info("Job {} has been removed from s3.".format(job_id))
|
|
|
|
|
|
|
|
|
|
|
2016-06-03 14:54:46 +01:00
|
|
|
@notify_celery.task(bind=True, name="send-sms", max_retries=5, default_retry_delay=5)
|
|
|
|
|
def send_sms(self, service_id, notification_id, encrypted_notification, created_at):
|
2016-05-16 11:55:00 +01:00
|
|
|
task_start = monotonic()
|
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-04-05 14:51:55 +01:00
|
|
|
if not service_allowed_to_send_to(notification['to'], service):
|
|
|
|
|
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-04-21 11:37:38 +01:00
|
|
|
|
2016-02-25 11:23:04 +00:00
|
|
|
sent_at = datetime.utcnow()
|
2016-02-16 17:42:04 +00:00
|
|
|
notification_db_object = Notification(
|
|
|
|
|
id=notification_id,
|
|
|
|
|
template_id=notification['template'],
|
2016-05-11 17:04:51 +01:00
|
|
|
template_version=notification['template_version'],
|
2016-02-16 17:42:04 +00:00
|
|
|
to=notification['to'],
|
|
|
|
|
service_id=service_id,
|
2016-02-23 17:39:08 +00:00
|
|
|
job_id=notification.get('job', None),
|
2016-05-19 10:46:03 +01:00
|
|
|
job_row_number=notification.get('row_number', None),
|
2016-05-31 14:55:06 +01:00
|
|
|
status='sending',
|
2016-06-03 14:54:46 +01:00
|
|
|
created_at=datetime.strptime(created_at, DATETIME_FORMAT)
|
2016-02-16 17:42:04 +00:00
|
|
|
)
|
2016-06-03 14:54:46 +01:00
|
|
|
dao_create_notification(notification_db_object, TEMPLATE_TYPE_SMS)
|
|
|
|
|
|
|
|
|
|
send_sms_to_provider.apply_async((service_id, notification_id, encrypted_notification), queue='sms')
|
2016-04-04 15:02:25 +01:00
|
|
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"SMS {} created at {} sent at {}".format(notification_id, created_at, sent_at)
|
|
|
|
|
)
|
2016-06-03 14:54:46 +01:00
|
|
|
|
2016-05-13 17:15:39 +01:00
|
|
|
statsd_client.incr("notifications.tasks.send-sms")
|
2016-05-16 11:55:00 +01:00
|
|
|
statsd_client.timing("notifications.tasks.send-sms.task-time", monotonic() - task_start)
|
2016-02-22 17:17:29 +00:00
|
|
|
except SQLAlchemyError as e:
|
2016-04-21 11:37:38 +01:00
|
|
|
current_app.logger.exception(e)
|
2016-06-07 11:09:54 +01:00
|
|
|
raise self.retry(queue="retry", exc=e)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="send-email")
|
2016-06-13 16:16:56 +01:00
|
|
|
def send_email(service_id, notification_id, encrypted_notification, created_at, reply_to_addresses=None):
|
2016-05-16 11:55:00 +01:00
|
|
|
task_start = monotonic()
|
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-05-10 09:04:22 +01:00
|
|
|
provider = provider_to_use('email', notification_id)
|
|
|
|
|
|
2016-04-05 14:51:55 +01:00
|
|
|
if not service_allowed_to_send_to(notification['to'], service):
|
|
|
|
|
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-02-25 11:23:04 +00:00
|
|
|
sent_at = datetime.utcnow()
|
2016-02-22 17:17:29 +00:00
|
|
|
notification_db_object = Notification(
|
|
|
|
|
id=notification_id,
|
|
|
|
|
template_id=notification['template'],
|
2016-05-13 16:25:05 +01:00
|
|
|
template_version=notification['template_version'],
|
2016-02-22 17:17:29 +00:00
|
|
|
to=notification['to'],
|
|
|
|
|
service_id=service_id,
|
2016-02-24 09:55:05 +00:00
|
|
|
job_id=notification.get('job', None),
|
2016-05-19 10:46:03 +01:00
|
|
|
job_row_number=notification.get('row_number', None),
|
2016-05-31 14:55:06 +01:00
|
|
|
status='sending',
|
2016-03-08 17:45:37 +00:00
|
|
|
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
|
2016-02-25 11:23:04 +00:00
|
|
|
sent_at=sent_at,
|
2016-05-10 09:04:22 +01:00
|
|
|
sent_by=provider.get_name()
|
2016-02-22 17:17:29 +00:00
|
|
|
)
|
2016-04-21 11:37:38 +01:00
|
|
|
|
2016-06-07 12:53:31 +01:00
|
|
|
dao_create_notification(notification_db_object, TEMPLATE_TYPE_EMAIL)
|
2016-05-13 17:15:39 +01:00
|
|
|
statsd_client.timing_with_dates(
|
|
|
|
|
"notifications.tasks.send-email.queued-for",
|
|
|
|
|
sent_at,
|
|
|
|
|
datetime.strptime(created_at, DATETIME_FORMAT)
|
|
|
|
|
)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
2016-04-04 15:02:25 +01:00
|
|
|
try:
|
|
|
|
|
template = Template(
|
2016-05-13 16:25:05 +01:00
|
|
|
dao_get_template_by_id(notification['template'], notification['template_version']).__dict__,
|
2016-04-04 15:02:25 +01:00
|
|
|
values=notification.get('personalisation', {})
|
2016-02-22 17:17:29 +00:00
|
|
|
)
|
2016-05-17 14:31:33 +01:00
|
|
|
|
2016-05-31 16:55:26 +01:00
|
|
|
if service.research_mode:
|
|
|
|
|
reference = create_uuid()
|
2016-06-01 16:57:57 +01:00
|
|
|
send_email_response.apply_async(
|
|
|
|
|
(provider.get_name(), str(reference), notification['to']), queue='research-mode'
|
|
|
|
|
)
|
2016-05-31 16:55:26 +01:00
|
|
|
else:
|
2016-06-13 15:31:45 +01:00
|
|
|
from_address = '"{}" <{}@{}>'.format(service.name, service.email_from,
|
|
|
|
|
current_app.config['NOTIFY_EMAIL_DOMAIN'])
|
2016-05-31 16:55:26 +01:00
|
|
|
reference = provider.send_email(
|
|
|
|
|
from_address,
|
|
|
|
|
notification['to'],
|
|
|
|
|
template.replaced_subject,
|
|
|
|
|
body=template.replaced_govuk_escaped,
|
|
|
|
|
html_body=template.as_HTML_email,
|
|
|
|
|
reply_to_addresses=reply_to_addresses,
|
|
|
|
|
)
|
2016-05-13 17:15:39 +01:00
|
|
|
|
2016-06-02 09:52:47 +01:00
|
|
|
update_provider_stats(
|
2016-06-02 09:30:01 +01:00
|
|
|
notification_id,
|
|
|
|
|
'email',
|
2016-06-02 09:52:47 +01:00
|
|
|
provider.get_name()
|
2016-06-02 09:30:01 +01:00
|
|
|
)
|
2016-05-13 17:15:39 +01:00
|
|
|
|
2016-06-02 09:52:47 +01:00
|
|
|
notification_db_object.reference = reference
|
|
|
|
|
dao_update_notification(notification_db_object)
|
|
|
|
|
|
2016-05-10 09:04:22 +01:00
|
|
|
except EmailClientException as e:
|
2016-04-04 15:02:25 +01:00
|
|
|
current_app.logger.exception(e)
|
2016-05-17 13:06:08 +01:00
|
|
|
notification_db_object.status = 'technical-failure'
|
2016-05-13 16:25:05 +01:00
|
|
|
dao_update_notification(notification_db_object)
|
2016-04-04 15:02:25 +01:00
|
|
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Email {} created at {} sent at {}".format(notification_id, created_at, sent_at)
|
|
|
|
|
)
|
2016-05-13 17:15:39 +01:00
|
|
|
statsd_client.incr("notifications.tasks.send-email")
|
2016-05-16 11:55:00 +01:00
|
|
|
statsd_client.timing("notifications.tasks.send-email.task-time", monotonic() - task_start)
|
2016-02-16 17:42:04 +00:00
|
|
|
except SQLAlchemyError as e:
|
2016-04-21 11:37:38 +01:00
|
|
|
current_app.logger.exception(e)
|
2016-02-17 15:41:33 +00:00
|
|
|
|
|
|
|
|
|
2016-04-05 14:51:55 +01:00
|
|
|
def service_allowed_to_send_to(recipient, service):
|
|
|
|
|
if not service.restricted:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
return allowed_to_send_to(
|
|
|
|
|
recipient,
|
|
|
|
|
itertools.chain.from_iterable(
|
|
|
|
|
[user.mobile_number, user.email_address] for user in service.users
|
|
|
|
|
)
|
|
|
|
|
)
|
2016-05-10 09:04:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def provider_to_use(notification_type, notification_id):
|
|
|
|
|
active_providers_in_order = [
|
|
|
|
|
provider for provider in get_provider_details_by_notification_type(notification_type) if provider.active
|
2016-05-31 16:55:26 +01:00
|
|
|
]
|
2016-05-10 09:04:22 +01:00
|
|
|
|
2016-05-11 15:36:17 +01:00
|
|
|
if not active_providers_in_order:
|
2016-05-10 09:04:22 +01:00
|
|
|
current_app.logger.error(
|
|
|
|
|
"{} {} failed as no active providers".format(notification_type, notification_id)
|
|
|
|
|
)
|
|
|
|
|
raise Exception("No active {} providers".format(notification_type))
|
|
|
|
|
|
|
|
|
|
return clients.get_client_by_name_and_type(active_providers_in_order[0].identifier, notification_type)
|
2016-06-08 15:25:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='timeout-sending-notifications')
|
|
|
|
|
def timeout_notifications():
|
|
|
|
|
notifications = get_notifications(filter_dict={'status': 'sending'})
|
|
|
|
|
now = datetime.utcnow()
|
|
|
|
|
for noti in notifications:
|
|
|
|
|
try:
|
|
|
|
|
if (now - noti.created_at) > timedelta(
|
|
|
|
|
seconds=current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD')
|
|
|
|
|
):
|
|
|
|
|
update_notification_status_by_id(noti.id, 'temporary-failure', STATISTICS_FAILURE)
|
|
|
|
|
current_app.logger.info((
|
|
|
|
|
"Timeout period reached for notification ({})"
|
|
|
|
|
", status has been updated.").format(noti.id))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
current_app.logger.error((
|
|
|
|
|
"Exception raised trying to timeout notification ({})"
|
|
|
|
|
", skipping notification update.").format(noti.id))
|