2017-02-13 15:05:39 +00:00
|
|
|
from datetime import (
|
|
|
|
|
datetime,
|
|
|
|
|
timedelta
|
|
|
|
|
)
|
2016-06-20 13:33:53 +01:00
|
|
|
|
2018-04-25 10:10:25 +01:00
|
|
|
import pytz
|
2016-06-20 13:33:53 +01:00
|
|
|
from flask import current_app
|
2018-02-06 09:35:33 +00:00
|
|
|
from notifications_utils.statsd_decorators import statsd
|
2018-04-25 10:10:25 +01:00
|
|
|
from sqlalchemy import and_, func
|
2016-06-20 13:33:53 +01:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
|
|
|
|
|
|
from app import notify_celery
|
2018-04-25 14:22:23 +01:00
|
|
|
from app import performance_platform_client, zendesk_client
|
2018-04-25 10:10:25 +01:00
|
|
|
from app.aws import s3
|
|
|
|
|
from app.celery.service_callback_tasks import (
|
|
|
|
|
send_delivery_status_to_service,
|
2018-07-18 17:03:16 +01:00
|
|
|
create_delivery_status_callback_data,
|
2018-04-25 10:10:25 +01:00
|
|
|
)
|
2018-04-25 14:22:23 +01:00
|
|
|
from app.celery.tasks import process_job
|
2018-04-25 10:10:25 +01:00
|
|
|
from app.config import QueueNames, TaskNames
|
2017-06-02 14:28:52 +01:00
|
|
|
from app.dao.inbound_sms_dao import delete_inbound_sms_created_more_than_a_week_ago
|
2018-02-16 10:56:12 +00:00
|
|
|
from app.dao.invited_org_user_dao import delete_org_invitations_created_more_than_two_days_ago
|
2018-04-25 10:10:25 +01:00
|
|
|
from app.dao.invited_user_dao import delete_invitations_created_more_than_two_days_ago
|
2017-07-24 15:13:18 +01:00
|
|
|
from app.dao.jobs_dao import (
|
|
|
|
|
dao_set_scheduled_jobs_to_pending,
|
2018-11-22 16:47:07 +00:00
|
|
|
dao_get_jobs_older_than_data_retention,
|
|
|
|
|
dao_archive_job
|
2017-10-16 12:32:44 +01:00
|
|
|
)
|
2018-04-25 10:10:25 +01:00
|
|
|
from app.dao.jobs_dao import dao_update_job
|
2017-01-27 12:30:56 +00:00
|
|
|
from app.dao.notifications_dao import (
|
2017-02-13 15:05:39 +00:00
|
|
|
dao_timeout_notifications,
|
2017-05-23 13:40:36 +01:00
|
|
|
is_delivery_slow_for_provider,
|
2017-05-24 13:21:22 +01:00
|
|
|
delete_notifications_created_more_than_a_week_ago_by_type,
|
2017-05-22 15:07:16 +01:00
|
|
|
dao_get_scheduled_notifications,
|
2017-09-15 17:46:08 +01:00
|
|
|
set_scheduled_notification_to_processed,
|
2018-03-23 15:38:35 +00:00
|
|
|
notifications_not_yet_sent
|
2017-09-15 17:46:08 +01:00
|
|
|
)
|
2017-02-13 15:05:39 +00:00
|
|
|
from app.dao.provider_details_dao import (
|
|
|
|
|
get_current_provider,
|
2018-12-11 15:14:08 +00:00
|
|
|
dao_toggle_sms_provider
|
2017-01-27 12:30:56 +00:00
|
|
|
)
|
2018-07-18 10:14:32 +01:00
|
|
|
from app.dao.service_callback_api_dao import get_service_delivery_status_callback_api_for_service
|
2018-04-25 10:10:25 +01:00
|
|
|
from app.dao.services_dao import (
|
|
|
|
|
dao_fetch_monthly_historical_stats_by_template
|
|
|
|
|
)
|
|
|
|
|
from app.dao.stats_template_usage_by_month_dao import insert_or_update_stats_for_template
|
2016-06-20 13:33:53 +01:00
|
|
|
from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago
|
2018-04-25 10:10:25 +01:00
|
|
|
from app.exceptions import NotificationTechnicalFailureException
|
2017-10-13 16:46:17 +01:00
|
|
|
from app.models import (
|
|
|
|
|
Job,
|
2018-01-17 14:43:28 +00:00
|
|
|
Notification,
|
|
|
|
|
NOTIFICATION_SENDING,
|
2017-10-13 16:46:17 +01:00
|
|
|
LETTER_TYPE,
|
2017-10-17 11:07:36 +01:00
|
|
|
JOB_STATUS_IN_PROGRESS,
|
2018-03-23 15:38:35 +00:00
|
|
|
JOB_STATUS_ERROR,
|
|
|
|
|
SMS_TYPE,
|
2018-04-25 10:10:25 +01:00
|
|
|
EMAIL_TYPE,
|
|
|
|
|
KEY_TYPE_NORMAL
|
2017-10-13 16:46:17 +01:00
|
|
|
)
|
2017-05-16 13:47:22 +01:00
|
|
|
from app.notifications.process_notifications import send_notification_to_queue
|
2018-04-25 10:10:25 +01:00
|
|
|
from app.performance_platform import total_sent_notifications, processing_time
|
2018-01-23 09:51:43 +00:00
|
|
|
from app.v2.errors import JobIncompleteError
|
2016-08-24 17:03:56 +01:00
|
|
|
|
|
|
|
|
|
2016-09-07 15:36:59 +01:00
|
|
|
@notify_celery.task(name="remove_csv_files")
|
|
|
|
|
@statsd(namespace="tasks")
|
2017-06-06 16:02:01 +01:00
|
|
|
def remove_csv_files(job_types):
|
2018-11-19 17:09:27 +00:00
|
|
|
jobs = dao_get_jobs_older_than_data_retention(notification_types=job_types)
|
2016-09-07 15:36:59 +01:00
|
|
|
for job in jobs:
|
|
|
|
|
s3.remove_job_from_s3(job.service_id, job.id)
|
2018-11-22 16:47:07 +00:00
|
|
|
dao_archive_job(job)
|
2016-09-07 15:36:59 +01:00
|
|
|
current_app.logger.info("Job ID {} has been removed from s3.".format(job.id))
|
|
|
|
|
|
|
|
|
|
|
2016-08-24 17:03:56 +01:00
|
|
|
@notify_celery.task(name="run-scheduled-jobs")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def run_scheduled_jobs():
|
|
|
|
|
try:
|
2016-10-07 12:28:42 +01:00
|
|
|
for job in dao_set_scheduled_jobs_to_pending():
|
2017-05-25 10:51:49 +01:00
|
|
|
process_job.apply_async([str(job.id)], queue=QueueNames.JOBS)
|
2016-10-07 12:55:48 +01:00
|
|
|
current_app.logger.info("Job ID {} added to process job queue".format(job.id))
|
2017-05-22 16:30:45 +01:00
|
|
|
except SQLAlchemyError:
|
2016-10-17 17:44:17 +01:00
|
|
|
current_app.logger.exception("Failed to run scheduled jobs")
|
2016-08-24 17:03:56 +01:00
|
|
|
raise
|
2016-06-20 13:33:53 +01:00
|
|
|
|
|
|
|
|
|
2017-05-16 13:47:22 +01:00
|
|
|
@notify_celery.task(name='send-scheduled-notifications')
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def send_scheduled_notifications():
|
|
|
|
|
try:
|
2017-05-17 12:59:00 +01:00
|
|
|
scheduled_notifications = dao_get_scheduled_notifications()
|
|
|
|
|
for notification in scheduled_notifications:
|
2017-05-16 13:47:22 +01:00
|
|
|
send_notification_to_queue(notification, notification.service.research_mode)
|
2017-05-22 15:07:16 +01:00
|
|
|
set_scheduled_notification_to_processed(notification.id)
|
2017-05-17 12:59:00 +01:00
|
|
|
current_app.logger.info(
|
2017-05-22 16:30:45 +01:00
|
|
|
"Sent {} scheduled notifications to the provider queue".format(len(scheduled_notifications)))
|
|
|
|
|
except SQLAlchemyError:
|
2017-05-16 13:47:22 +01:00
|
|
|
current_app.logger.exception("Failed to send scheduled notifications")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2016-06-20 13:33:53 +01:00
|
|
|
@notify_celery.task(name="delete-verify-codes")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
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)
|
|
|
|
|
)
|
2017-08-24 17:08:39 +01:00
|
|
|
except SQLAlchemyError:
|
2016-10-17 17:44:17 +01:00
|
|
|
current_app.logger.exception("Failed to delete verify codes")
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2017-05-23 13:40:36 +01:00
|
|
|
@notify_celery.task(name="delete-sms-notifications")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def delete_sms_notifications_older_than_seven_days():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago_by_type('sms')
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete {} job started {} finished {} deleted {} sms notifications".format(
|
|
|
|
|
'sms',
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-08-24 17:08:39 +01:00
|
|
|
except SQLAlchemyError:
|
2017-05-23 13:40:36 +01:00
|
|
|
current_app.logger.exception("Failed to delete sms notifications")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-email-notifications")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2017-05-23 13:40:36 +01:00
|
|
|
def delete_email_notifications_older_than_seven_days():
|
2016-06-20 13:33:53 +01:00
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
2017-05-23 13:40:36 +01:00
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago_by_type('email')
|
2016-06-20 13:33:53 +01:00
|
|
|
current_app.logger.info(
|
2017-05-23 13:40:36 +01:00
|
|
|
"Delete {} job started {} finished {} deleted {} email notifications".format(
|
|
|
|
|
'email',
|
2016-06-20 13:33:53 +01:00
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-08-24 17:08:39 +01:00
|
|
|
except SQLAlchemyError:
|
2018-12-20 10:57:14 +00:00
|
|
|
current_app.logger.exception("Failed to delete email notifications")
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2017-05-23 13:40:36 +01:00
|
|
|
@notify_celery.task(name="delete-letter-notifications")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2017-05-23 13:40:36 +01:00
|
|
|
def delete_letter_notifications_older_than_seven_days():
|
2016-06-20 13:33:53 +01:00
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
2017-05-23 13:40:36 +01:00
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago_by_type('letter')
|
2016-06-20 13:33:53 +01:00
|
|
|
current_app.logger.info(
|
2017-05-23 13:40:36 +01:00
|
|
|
"Delete {} job started {} finished {} deleted {} letter notifications".format(
|
|
|
|
|
'letter',
|
2016-06-20 13:33:53 +01:00
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-08-24 17:08:39 +01:00
|
|
|
except SQLAlchemyError:
|
2018-12-20 10:57:14 +00:00
|
|
|
current_app.logger.exception("Failed to delete letter notifications")
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-invitations")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
def delete_invitations():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
2018-02-16 10:56:12 +00:00
|
|
|
deleted_invites = delete_invitations_created_more_than_two_days_ago()
|
|
|
|
|
deleted_invites += delete_org_invitations_created_more_than_two_days_ago()
|
2016-06-20 13:33:53 +01:00
|
|
|
current_app.logger.info(
|
2018-02-16 14:42:03 +00:00
|
|
|
"Delete job started {} finished {} deleted {} invitations".format(start, datetime.utcnow(), deleted_invites)
|
2016-06-20 13:33:53 +01:00
|
|
|
)
|
2017-08-24 17:08:39 +01:00
|
|
|
except SQLAlchemyError:
|
2016-10-17 17:44:17 +01:00
|
|
|
current_app.logger.exception("Failed to delete invitations")
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='timeout-sending-notifications')
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
def timeout_notifications():
|
2018-03-16 17:18:44 +00:00
|
|
|
technical_failure_notifications, temporary_failure_notifications = \
|
|
|
|
|
dao_timeout_notifications(current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD'))
|
2017-12-07 16:37:36 +00:00
|
|
|
|
2018-03-16 17:18:44 +00:00
|
|
|
notifications = technical_failure_notifications + temporary_failure_notifications
|
2018-03-12 12:15:03 +00:00
|
|
|
for notification in notifications:
|
|
|
|
|
# queue callback task only if the service_callback_api exists
|
2018-07-18 10:14:32 +01:00
|
|
|
service_callback_api = get_service_delivery_status_callback_api_for_service(service_id=notification.service_id)
|
2018-03-12 12:15:03 +00:00
|
|
|
if service_callback_api:
|
2018-07-18 17:03:16 +01:00
|
|
|
encrypted_notification = create_delivery_status_callback_data(notification, service_callback_api)
|
2018-03-16 14:00:49 +00:00
|
|
|
send_delivery_status_to_service.apply_async([str(notification.id), encrypted_notification],
|
|
|
|
|
queue=QueueNames.CALLBACKS)
|
2017-12-07 16:37:36 +00:00
|
|
|
|
2018-03-12 12:15:03 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"Timeout period reached for {} notifications, status has been updated.".format(len(notifications)))
|
2018-03-16 17:18:44 +00:00
|
|
|
if technical_failure_notifications:
|
|
|
|
|
message = "{} notifications have been updated to technical-failure because they " \
|
|
|
|
|
"have timed out and are still in created.Notification ids: {}".format(
|
|
|
|
|
len(technical_failure_notifications), [str(x.id) for x in technical_failure_notifications])
|
|
|
|
|
raise NotificationTechnicalFailureException(message)
|
2017-01-27 12:30:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='send-daily-performance-platform-stats')
|
|
|
|
|
@statsd(namespace="tasks")
|
2017-01-30 18:24:18 +00:00
|
|
|
def send_daily_performance_platform_stats():
|
|
|
|
|
if performance_platform_client.active:
|
2018-03-05 16:44:20 +00:00
|
|
|
yesterday = datetime.utcnow() - timedelta(days=1)
|
|
|
|
|
send_total_sent_notifications_to_performance_platform(yesterday)
|
2017-08-30 14:36:16 +01:00
|
|
|
processing_time.send_processing_time_to_performance_platform()
|
|
|
|
|
|
|
|
|
|
|
2018-03-05 16:44:20 +00:00
|
|
|
def send_total_sent_notifications_to_performance_platform(day):
|
|
|
|
|
count_dict = total_sent_notifications.get_total_sent_notifications_for_day(day)
|
2017-08-30 14:36:16 +01:00
|
|
|
email_sent_count = count_dict.get('email').get('count')
|
|
|
|
|
sms_sent_count = count_dict.get('sms').get('count')
|
2018-03-02 16:05:26 +00:00
|
|
|
letter_sent_count = count_dict.get('letter').get('count')
|
2017-08-30 14:36:16 +01:00
|
|
|
start_date = count_dict.get('start_date')
|
|
|
|
|
|
|
|
|
|
current_app.logger.info(
|
2018-03-05 16:44:20 +00:00
|
|
|
"Attempting to update Performance Platform for {} with {} emails, {} text messages and {} letters"
|
|
|
|
|
.format(start_date, email_sent_count, sms_sent_count, letter_sent_count)
|
2017-08-30 14:36:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
|
|
|
|
start_date,
|
|
|
|
|
'sms',
|
|
|
|
|
sms_sent_count
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
|
|
|
|
start_date,
|
|
|
|
|
'email',
|
|
|
|
|
email_sent_count
|
|
|
|
|
)
|
2017-02-13 15:05:39 +00:00
|
|
|
|
2018-03-02 16:05:26 +00:00
|
|
|
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
|
|
|
|
start_date,
|
|
|
|
|
'letter',
|
|
|
|
|
letter_sent_count
|
|
|
|
|
)
|
|
|
|
|
|
2017-02-13 15:05:39 +00:00
|
|
|
|
|
|
|
|
@notify_celery.task(name='switch-current-sms-provider-on-slow-delivery')
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def switch_current_sms_provider_on_slow_delivery():
|
|
|
|
|
"""
|
|
|
|
|
Switch providers if there are at least two slow delivery notifications (more than four minutes)
|
|
|
|
|
in the last ten minutes. Search from the time we last switched to the current provider.
|
|
|
|
|
"""
|
2018-12-05 14:40:07 +00:00
|
|
|
current_provider = get_current_provider('sms')
|
|
|
|
|
if current_provider.updated_at > datetime.utcnow() - timedelta(minutes=10):
|
2018-12-11 15:28:38 +00:00
|
|
|
current_app.logger.info("Slow delivery notifications provider switched less than 10 minutes ago.")
|
2018-12-05 14:40:07 +00:00
|
|
|
return
|
|
|
|
|
slow_delivery_notifications = is_delivery_slow_for_provider(
|
|
|
|
|
provider=current_provider.identifier,
|
|
|
|
|
threshold=0.1,
|
|
|
|
|
created_at=datetime.utcnow() - timedelta(minutes=10),
|
|
|
|
|
delivery_time=timedelta(minutes=4),
|
|
|
|
|
)
|
2017-02-13 15:05:39 +00:00
|
|
|
|
2018-12-05 14:40:07 +00:00
|
|
|
if slow_delivery_notifications:
|
|
|
|
|
current_app.logger.warning(
|
|
|
|
|
'Slow delivery notifications detected for provider {}'.format(
|
|
|
|
|
current_provider.identifier
|
2017-02-13 15:05:39 +00:00
|
|
|
)
|
2018-12-05 14:40:07 +00:00
|
|
|
)
|
2017-02-13 15:05:39 +00:00
|
|
|
|
2018-12-11 15:14:08 +00:00
|
|
|
dao_toggle_sms_provider(current_provider.identifier)
|
2017-05-11 15:22:57 +01:00
|
|
|
|
|
|
|
|
|
2017-06-02 14:28:52 +01:00
|
|
|
@notify_celery.task(name="delete-inbound-sms")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def delete_inbound_sms_older_than_seven_days():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_inbound_sms_created_more_than_a_week_ago()
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete inbound sms job started {} finished {} deleted {} inbound sms notifications".format(
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-08-24 17:08:39 +01:00
|
|
|
except SQLAlchemyError:
|
2017-06-02 14:28:52 +01:00
|
|
|
current_app.logger.exception("Failed to delete inbound sms notifications")
|
|
|
|
|
raise
|
2017-06-07 16:31:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="remove_transformed_dvla_files")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def remove_transformed_dvla_files():
|
2018-11-19 17:09:27 +00:00
|
|
|
jobs = dao_get_jobs_older_than_data_retention(notification_types=[LETTER_TYPE])
|
2017-06-07 16:31:51 +01:00
|
|
|
for job in jobs:
|
|
|
|
|
s3.remove_transformed_dvla_file(job.id)
|
|
|
|
|
current_app.logger.info("Transformed dvla file for job {} has been removed from s3.".format(job.id))
|
2017-06-12 15:55:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete_dvla_response_files")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def delete_dvla_response_files_older_than_seven_days():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
bucket_objects = s3.get_s3_bucket_objects(
|
|
|
|
|
current_app.config['DVLA_RESPONSE_BUCKET_NAME'],
|
|
|
|
|
'root/dispatch'
|
|
|
|
|
)
|
|
|
|
|
older_than_seven_days = s3.filter_s3_bucket_objects_within_date_range(bucket_objects)
|
|
|
|
|
|
|
|
|
|
for f in older_than_seven_days:
|
|
|
|
|
s3.remove_s3_object(current_app.config['DVLA_RESPONSE_BUCKET_NAME'], f['Key'])
|
|
|
|
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete dvla response files started {} finished {} deleted {} files".format(
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
len(older_than_seven_days)
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-08-24 17:08:39 +01:00
|
|
|
except SQLAlchemyError:
|
2017-06-12 15:55:42 +01:00
|
|
|
current_app.logger.exception("Failed to delete dvla response files")
|
|
|
|
|
raise
|
2017-07-24 15:13:18 +01:00
|
|
|
|
|
|
|
|
|
2018-01-17 14:43:28 +00:00
|
|
|
@notify_celery.task(name="raise-alert-if-letter-notifications-still-sending")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def raise_alert_if_letter_notifications_still_sending():
|
|
|
|
|
today = datetime.utcnow().date()
|
|
|
|
|
|
|
|
|
|
# Do nothing on the weekend
|
|
|
|
|
if today.isoweekday() in [6, 7]:
|
|
|
|
|
return
|
|
|
|
|
|
2018-04-25 10:10:25 +01:00
|
|
|
if today.isoweekday() in [1, 2]:
|
2018-04-05 14:11:21 +01:00
|
|
|
offset_days = 4
|
2018-01-17 14:43:28 +00:00
|
|
|
else:
|
2018-04-05 14:11:21 +01:00
|
|
|
offset_days = 2
|
2018-01-17 14:43:28 +00:00
|
|
|
still_sending = Notification.query.filter(
|
|
|
|
|
Notification.notification_type == LETTER_TYPE,
|
|
|
|
|
Notification.status == NOTIFICATION_SENDING,
|
2018-04-25 10:10:25 +01:00
|
|
|
Notification.key_type == KEY_TYPE_NORMAL,
|
|
|
|
|
func.date(Notification.sent_at) <= today - timedelta(days=offset_days)
|
2018-01-17 14:43:28 +00:00
|
|
|
).count()
|
|
|
|
|
|
|
|
|
|
if still_sending:
|
2018-01-17 16:38:07 +00:00
|
|
|
message = "There are {} letters in the 'sending' state from {}".format(
|
|
|
|
|
still_sending,
|
|
|
|
|
(today - timedelta(days=offset_days)).strftime('%A %d %B')
|
2018-01-17 14:43:28 +00:00
|
|
|
)
|
2018-01-17 16:38:07 +00:00
|
|
|
# Only send alerts in production
|
2018-02-02 11:27:58 +00:00
|
|
|
if current_app.config['NOTIFY_ENVIRONMENT'] in ['live', 'production', 'test']:
|
2018-04-25 14:22:23 +01:00
|
|
|
zendesk_client.create_ticket(
|
2018-01-17 16:38:07 +00:00
|
|
|
subject="[{}] Letters still sending".format(current_app.config['NOTIFY_ENVIRONMENT']),
|
|
|
|
|
message=message,
|
2018-04-25 14:22:23 +01:00
|
|
|
ticket_type=zendesk_client.TYPE_INCIDENT
|
2018-01-17 16:38:07 +00:00
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
current_app.logger.info(message)
|
|
|
|
|
|
2018-01-17 14:43:28 +00:00
|
|
|
|
2017-10-12 16:21:08 +01:00
|
|
|
@notify_celery.task(name='check-job-status')
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def check_job_status():
|
|
|
|
|
"""
|
|
|
|
|
every x minutes do this check
|
|
|
|
|
select
|
|
|
|
|
from jobs
|
|
|
|
|
where job_status == 'in progress'
|
|
|
|
|
and template_type in ('sms', 'email')
|
|
|
|
|
and scheduled_at or created_at is older that 30 minutes.
|
|
|
|
|
if any results then
|
|
|
|
|
raise error
|
2017-10-12 16:23:28 +01:00
|
|
|
process the rows in the csv that are missing (in another task) just do the check here.
|
2017-10-12 16:21:08 +01:00
|
|
|
"""
|
|
|
|
|
thirty_minutes_ago = datetime.utcnow() - timedelta(minutes=30)
|
|
|
|
|
thirty_five_minutes_ago = datetime.utcnow() - timedelta(minutes=35)
|
|
|
|
|
|
|
|
|
|
jobs_not_complete_after_30_minutes = Job.query.filter(
|
|
|
|
|
Job.job_status == JOB_STATUS_IN_PROGRESS,
|
|
|
|
|
and_(thirty_five_minutes_ago < Job.processing_started, Job.processing_started < thirty_minutes_ago)
|
2017-10-18 09:50:39 +01:00
|
|
|
).order_by(Job.processing_started).all()
|
2017-10-12 16:21:08 +01:00
|
|
|
|
2018-03-09 16:34:47 +00:00
|
|
|
# temporarily mark them as ERROR so that they don't get picked up by future check_job_status tasks
|
|
|
|
|
# if they haven't been re-processed in time.
|
|
|
|
|
job_ids = []
|
|
|
|
|
for job in jobs_not_complete_after_30_minutes:
|
|
|
|
|
job.job_status = JOB_STATUS_ERROR
|
|
|
|
|
dao_update_job(job)
|
|
|
|
|
job_ids.append(str(job.id))
|
|
|
|
|
|
2017-10-12 16:21:08 +01:00
|
|
|
if job_ids:
|
2017-10-13 16:46:17 +01:00
|
|
|
notify_celery.send_task(
|
|
|
|
|
name=TaskNames.PROCESS_INCOMPLETE_JOBS,
|
|
|
|
|
args=(job_ids,),
|
|
|
|
|
queue=QueueNames.JOBS
|
|
|
|
|
)
|
2017-10-12 16:21:08 +01:00
|
|
|
raise JobIncompleteError("Job(s) {} have not completed.".format(job_ids))
|
2017-11-09 10:32:39 +00:00
|
|
|
|
|
|
|
|
|
2017-11-14 14:32:34 +00:00
|
|
|
@notify_celery.task(name='daily-stats-template-usage-by-month')
|
2017-11-09 10:32:39 +00:00
|
|
|
@statsd(namespace="tasks")
|
2017-11-09 14:13:42 +00:00
|
|
|
def daily_stats_template_usage_by_month():
|
2017-11-09 10:32:39 +00:00
|
|
|
results = dao_fetch_monthly_historical_stats_by_template()
|
|
|
|
|
|
|
|
|
|
for result in results:
|
2017-11-10 13:49:20 +00:00
|
|
|
if result.template_id:
|
|
|
|
|
insert_or_update_stats_for_template(
|
|
|
|
|
result.template_id,
|
|
|
|
|
result.month,
|
|
|
|
|
result.year,
|
|
|
|
|
result.count
|
|
|
|
|
)
|
2018-01-11 16:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='raise-alert-if-no-letter-ack-file')
|
|
|
|
|
@statsd(namespace="tasks")
|
2018-01-12 15:10:42 +00:00
|
|
|
def letter_raise_alert_if_no_ack_file_for_zip():
|
2018-01-16 09:29:31 +00:00
|
|
|
# get a list of zip files since yesterday
|
2018-01-18 14:44:23 +00:00
|
|
|
zip_file_set = set()
|
2018-01-16 09:29:31 +00:00
|
|
|
|
2018-01-12 15:10:42 +00:00
|
|
|
for key in s3.get_list_of_files_by_suffix(bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'],
|
2018-01-18 10:44:36 +00:00
|
|
|
subfolder=datetime.utcnow().strftime('%Y-%m-%d') + '/zips_sent',
|
|
|
|
|
suffix='.TXT'):
|
2018-11-15 17:24:37 +00:00
|
|
|
subname = key.split('/')[-1] # strip subfolder in name
|
2018-01-22 13:11:47 +00:00
|
|
|
zip_file_set.add(subname.upper().rstrip('.TXT'))
|
2018-01-12 15:10:42 +00:00
|
|
|
|
|
|
|
|
# get acknowledgement file
|
2018-01-18 14:44:23 +00:00
|
|
|
ack_file_set = set()
|
2018-01-19 09:24:03 +00:00
|
|
|
|
2018-11-15 17:24:37 +00:00
|
|
|
yesterday = datetime.now(tz=pytz.utc) - timedelta(days=1) # AWS datetime format
|
2018-01-19 09:24:03 +00:00
|
|
|
|
2018-01-12 15:10:42 +00:00
|
|
|
for key in s3.get_list_of_files_by_suffix(bucket_name=current_app.config['DVLA_RESPONSE_BUCKET_NAME'],
|
2018-01-17 12:21:56 +00:00
|
|
|
subfolder='root/dispatch', suffix='.ACK.txt', last_modified=yesterday):
|
2018-01-18 14:44:23 +00:00
|
|
|
ack_file_set.add(key)
|
2018-01-12 15:10:42 +00:00
|
|
|
|
2018-01-16 09:29:31 +00:00
|
|
|
today_str = datetime.utcnow().strftime('%Y%m%d')
|
2018-01-12 15:10:42 +00:00
|
|
|
|
2018-01-18 14:44:23 +00:00
|
|
|
ack_content_set = set()
|
|
|
|
|
for key in ack_file_set:
|
2018-01-16 09:29:31 +00:00
|
|
|
if today_str in key:
|
2018-01-12 15:10:42 +00:00
|
|
|
content = s3.get_s3_file(current_app.config['DVLA_RESPONSE_BUCKET_NAME'], key)
|
2018-11-15 17:24:37 +00:00
|
|
|
for zip_file in content.split('\n'): # each line
|
2018-01-12 15:10:42 +00:00
|
|
|
s = zip_file.split('|')
|
2018-01-18 14:44:23 +00:00
|
|
|
ack_content_set.add(s[0].upper())
|
2018-01-12 15:10:42 +00:00
|
|
|
|
2018-04-25 14:22:23 +01:00
|
|
|
message = (
|
|
|
|
|
"Letter ack file does not contain all zip files sent. "
|
|
|
|
|
"Missing ack for zip files: {}, "
|
|
|
|
|
"pdf bucket: {}, subfolder: {}, "
|
|
|
|
|
"ack bucket: {}"
|
|
|
|
|
).format(
|
|
|
|
|
str(sorted(zip_file_set - ack_content_set)),
|
|
|
|
|
current_app.config['LETTERS_PDF_BUCKET_NAME'],
|
|
|
|
|
datetime.utcnow().strftime('%Y-%m-%d') + '/zips_sent',
|
|
|
|
|
current_app.config['DVLA_RESPONSE_BUCKET_NAME']
|
|
|
|
|
)
|
2018-01-22 13:11:47 +00:00
|
|
|
# strip empty element before comparison
|
|
|
|
|
ack_content_set.discard('')
|
|
|
|
|
zip_file_set.discard('')
|
2018-01-23 09:51:43 +00:00
|
|
|
|
|
|
|
|
if len(zip_file_set - ack_content_set) > 0:
|
2018-02-02 11:27:58 +00:00
|
|
|
if current_app.config['NOTIFY_ENVIRONMENT'] in ['live', 'production', 'test']:
|
2018-04-25 14:22:23 +01:00
|
|
|
zendesk_client.create_ticket(
|
2018-01-22 12:44:03 +00:00
|
|
|
subject="Letter acknowledge error",
|
2018-04-25 14:22:23 +01:00
|
|
|
message=message,
|
|
|
|
|
ticket_type=zendesk_client.TYPE_INCIDENT
|
2018-01-22 12:44:03 +00:00
|
|
|
)
|
2018-04-25 14:22:23 +01:00
|
|
|
current_app.logger.error(message)
|
2018-01-16 09:29:31 +00:00
|
|
|
|
2018-01-18 14:44:23 +00:00
|
|
|
if len(ack_content_set - zip_file_set) > 0:
|
2018-01-16 09:29:31 +00:00
|
|
|
current_app.logger.info(
|
2018-01-18 14:44:23 +00:00
|
|
|
"letter ack contains zip that is not for today: {}".format(ack_content_set - zip_file_set)
|
2018-01-16 09:29:31 +00:00
|
|
|
)
|
2018-03-23 15:38:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='replay-created-notifications')
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def replay_created_notifications():
|
2018-03-26 10:26:24 +01:00
|
|
|
# if the notification has not be send after 4 hours + 15 minutes, then try to resend.
|
|
|
|
|
resend_created_notifications_older_than = (60 * 60 * 4) + (60 * 15)
|
2018-03-23 15:38:35 +00:00
|
|
|
for notification_type in (EMAIL_TYPE, SMS_TYPE):
|
|
|
|
|
notifications_to_resend = notifications_not_yet_sent(
|
2018-03-26 10:26:24 +01:00
|
|
|
resend_created_notifications_older_than,
|
2018-03-23 15:38:35 +00:00
|
|
|
notification_type
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
current_app.logger.info("Sending {} {} notifications "
|
|
|
|
|
"to the delivery queue because the notification "
|
|
|
|
|
"status was created.".format(len(notifications_to_resend), notification_type))
|
|
|
|
|
|
|
|
|
|
for n in notifications_to_resend:
|
|
|
|
|
send_notification_to_queue(notification=n, research_mode=n.service.research_mode)
|