2021-03-10 13:55:06 +00:00
|
|
|
from datetime import datetime, timedelta
|
2019-01-14 17:22:41 +00:00
|
|
|
|
|
|
|
|
import pytz
|
|
|
|
|
from flask import current_app
|
2021-09-23 15:40:55 +01:00
|
|
|
from notifications_utils.clients.zendesk.zendesk_client import (
|
|
|
|
|
NotifySupportTicket,
|
|
|
|
|
)
|
2019-01-14 17:22:41 +00:00
|
|
|
from sqlalchemy import func
|
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
|
|
2021-03-11 18:53:43 +00:00
|
|
|
from app import notify_celery, zendesk_client
|
2019-01-14 17:22:41 +00:00
|
|
|
from app.aws import s3
|
|
|
|
|
from app.celery.service_callback_tasks import (
|
|
|
|
|
create_delivery_status_callback_data,
|
2021-03-10 13:55:06 +00:00
|
|
|
send_delivery_status_to_service,
|
2019-01-14 17:22:41 +00:00
|
|
|
)
|
|
|
|
|
from app.config import QueueNames
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.cronitor import cronitor
|
2021-03-11 18:15:11 +00:00
|
|
|
from app.dao.fact_processing_time_dao import insert_update_processing_time
|
2019-02-26 17:57:35 +00:00
|
|
|
from app.dao.inbound_sms_dao import delete_inbound_sms_older_than_retention
|
2019-01-14 17:22:41 +00:00
|
|
|
from app.dao.jobs_dao import (
|
2021-03-10 13:55:06 +00:00
|
|
|
dao_archive_job,
|
2019-01-14 17:22:41 +00:00
|
|
|
dao_get_jobs_older_than_data_retention,
|
|
|
|
|
)
|
|
|
|
|
from app.dao.notifications_dao import (
|
2021-03-11 18:53:43 +00:00
|
|
|
dao_get_notifications_processing_time_stats,
|
2019-01-14 17:22:41 +00:00
|
|
|
dao_timeout_notifications,
|
2019-02-26 17:57:35 +00:00
|
|
|
delete_notifications_older_than_retention_by_type,
|
2019-01-14 17:22:41 +00:00
|
|
|
)
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.dao.service_callback_api_dao import (
|
|
|
|
|
get_service_delivery_status_callback_api_for_service,
|
|
|
|
|
)
|
2019-01-14 17:22:41 +00:00
|
|
|
from app.exceptions import NotificationTechnicalFailureException
|
|
|
|
|
from app.models import (
|
2019-01-16 14:11:03 +00:00
|
|
|
EMAIL_TYPE,
|
2021-03-10 13:55:06 +00:00
|
|
|
KEY_TYPE_NORMAL,
|
2019-01-14 17:22:41 +00:00
|
|
|
LETTER_TYPE,
|
2021-03-10 13:55:06 +00:00
|
|
|
NOTIFICATION_SENDING,
|
|
|
|
|
SMS_TYPE,
|
2021-03-11 18:15:11 +00:00
|
|
|
FactProcessingTime,
|
2021-03-10 13:55:06 +00:00
|
|
|
Notification,
|
2019-01-14 17:22:41 +00:00
|
|
|
)
|
2019-04-02 11:49:20 +01:00
|
|
|
from app.utils import get_london_midnight_in_utc
|
2019-01-14 17:22:41 +00:00
|
|
|
|
|
|
|
|
|
2019-01-16 14:11:03 +00:00
|
|
|
@notify_celery.task(name="remove_sms_email_jobs")
|
|
|
|
|
@cronitor("remove_sms_email_jobs")
|
2019-01-22 10:31:37 +00:00
|
|
|
def remove_sms_email_csv_files():
|
2019-01-16 14:11:03 +00:00
|
|
|
_remove_csv_files([EMAIL_TYPE, SMS_TYPE])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="remove_letter_jobs")
|
|
|
|
|
@cronitor("remove_letter_jobs")
|
2019-01-22 10:31:37 +00:00
|
|
|
def remove_letter_csv_files():
|
2019-01-16 14:11:03 +00:00
|
|
|
_remove_csv_files([LETTER_TYPE])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _remove_csv_files(job_types):
|
2019-01-14 17:22:41 +00:00
|
|
|
jobs = dao_get_jobs_older_than_data_retention(notification_types=job_types)
|
|
|
|
|
for job in jobs:
|
|
|
|
|
s3.remove_job_from_s3(job.service_id, job.id)
|
|
|
|
|
dao_archive_job(job)
|
|
|
|
|
current_app.logger.info("Job ID {} has been removed from s3.".format(job.id))
|
|
|
|
|
|
|
|
|
|
|
2020-04-07 16:41:33 +01:00
|
|
|
@notify_celery.task(name="delete-notifications-older-than-retention")
|
|
|
|
|
def delete_notifications_older_than_retention():
|
put delete tasks on the reporting worker
they share a lot with the reporting tasks (creating ft_billing and
ft_notification_status), in that they're run nightly, take a long time,
and we see error messages if they get run multiple times (due to
visibility timeout).
The periodic app has two concurrent processes - previously there was
just one delete task, which would use one of those processes, while the
other process would pick up anything else on the queue (at that time of
night, the regular provider switch checks and scheduled job checks).
However, when we switched to running the three delete notification types
separately, we saw visibility timeout issues - three tasks would be
created, all three would be picked up by one celery instance, the two
worker processes would start on two of them, and the third would sit on
the box, wait longer than the visibility timeout to be picked up (and
acknowledged), and so SQS would assume the task was lost and replay it.
it's queues all the way down!
By putting them on the reporting worker we can take advantage of tuning
that app (for example setting the prefetch multiplier to one) which is
designed to run large tasks. We've also got more concurrent workers on
this box, so we can run all three tasks at once.
2021-12-03 13:28:16 +00:00
|
|
|
delete_email_notifications_older_than_retention.apply_async(queue=QueueNames.REPORTING)
|
|
|
|
|
delete_sms_notifications_older_than_retention.apply_async(queue=QueueNames.REPORTING)
|
|
|
|
|
delete_letter_notifications_older_than_retention.apply_async(queue=QueueNames.REPORTING)
|
2020-04-07 16:41:33 +01:00
|
|
|
|
|
|
|
|
|
2019-01-14 17:22:41 +00:00
|
|
|
@notify_celery.task(name="delete-sms-notifications")
|
2019-01-16 14:11:03 +00:00
|
|
|
@cronitor("delete-sms-notifications")
|
2019-02-26 17:57:35 +00:00
|
|
|
def delete_sms_notifications_older_than_retention():
|
2021-12-01 14:28:08 +00:00
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_notifications_older_than_retention_by_type('sms')
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete {} job started {} finished {} deleted {} sms notifications".format(
|
|
|
|
|
'sms',
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
2019-01-14 17:22:41 +00:00
|
|
|
)
|
2021-12-01 14:28:08 +00:00
|
|
|
)
|
2019-01-14 17:22:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-email-notifications")
|
2019-01-16 14:11:03 +00:00
|
|
|
@cronitor("delete-email-notifications")
|
2019-02-26 17:57:35 +00:00
|
|
|
def delete_email_notifications_older_than_retention():
|
2021-12-01 14:28:08 +00:00
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_notifications_older_than_retention_by_type('email')
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete {} job started {} finished {} deleted {} email notifications".format(
|
|
|
|
|
'email',
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
2019-01-14 17:22:41 +00:00
|
|
|
)
|
2021-12-01 14:28:08 +00:00
|
|
|
)
|
2019-01-14 17:22:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-letter-notifications")
|
2019-01-16 14:11:03 +00:00
|
|
|
@cronitor("delete-letter-notifications")
|
2019-02-26 17:57:35 +00:00
|
|
|
def delete_letter_notifications_older_than_retention():
|
2021-12-01 14:28:08 +00:00
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_notifications_older_than_retention_by_type('letter')
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete {} job started {} finished {} deleted {} letter notifications".format(
|
|
|
|
|
'letter',
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
2019-01-14 17:22:41 +00:00
|
|
|
)
|
2021-12-01 14:28:08 +00:00
|
|
|
)
|
2019-01-14 17:22:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='timeout-sending-notifications')
|
2019-01-16 14:11:03 +00:00
|
|
|
@cronitor('timeout-sending-notifications')
|
2019-01-14 17:22:41 +00:00
|
|
|
def timeout_notifications():
|
2021-11-08 14:18:21 +00:00
|
|
|
# TEMPORARY: re-run the following code over small batches of notifications
|
|
|
|
|
# so that we can cope with a high volume that need processing. We've changed
|
|
|
|
|
# dao_timeout_notifications to return up to 100K notifications, so this task
|
|
|
|
|
# will operate on up to 500K - normally we only get around 20K.
|
|
|
|
|
for _ in range(0, 5):
|
|
|
|
|
technical_failure_notifications, temporary_failure_notifications = \
|
|
|
|
|
dao_timeout_notifications(current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD'))
|
|
|
|
|
|
|
|
|
|
notifications = technical_failure_notifications + temporary_failure_notifications
|
|
|
|
|
for notification in notifications:
|
|
|
|
|
# queue callback task only if the service_callback_api exists
|
|
|
|
|
service_callback_api = get_service_delivery_status_callback_api_for_service(service_id=notification.service_id) # noqa: E501
|
|
|
|
|
if service_callback_api:
|
|
|
|
|
encrypted_notification = create_delivery_status_callback_data(notification, service_callback_api)
|
|
|
|
|
send_delivery_status_to_service.apply_async([str(notification.id), encrypted_notification],
|
|
|
|
|
queue=QueueNames.CALLBACKS)
|
|
|
|
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Timeout period reached for {} notifications, status has been updated.".format(len(notifications)))
|
|
|
|
|
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)
|
|
|
|
|
|
2021-11-09 15:41:14 +00:00
|
|
|
if len(notifications) < 100000:
|
2021-11-08 14:18:21 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
raise RuntimeError("Some notifications may still be in sending.")
|
2019-01-14 17:22:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-inbound-sms")
|
2019-01-16 14:11:03 +00:00
|
|
|
@cronitor("delete-inbound-sms")
|
2019-02-26 17:57:35 +00:00
|
|
|
def delete_inbound_sms():
|
2019-01-14 17:22:41 +00:00
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
2019-02-26 17:57:35 +00:00
|
|
|
deleted = delete_inbound_sms_older_than_retention()
|
2019-01-14 17:22:41 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete inbound sms job started {} finished {} deleted {} inbound sms notifications".format(
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError:
|
|
|
|
|
current_app.logger.exception("Failed to delete inbound sms notifications")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="raise-alert-if-letter-notifications-still-sending")
|
2019-01-16 14:11:03 +00:00
|
|
|
@cronitor("raise-alert-if-letter-notifications-still-sending")
|
2019-01-14 17:22:41 +00:00
|
|
|
def raise_alert_if_letter_notifications_still_sending():
|
2020-04-02 09:36:56 +01:00
|
|
|
still_sending_count, sent_date = get_letter_notifications_still_sending_when_they_shouldnt_be()
|
|
|
|
|
|
|
|
|
|
if still_sending_count:
|
|
|
|
|
message = "There are {} letters in the 'sending' state from {}".format(
|
|
|
|
|
still_sending_count,
|
|
|
|
|
sent_date.strftime('%A %d %B')
|
|
|
|
|
)
|
|
|
|
|
# Only send alerts in production
|
|
|
|
|
if current_app.config['NOTIFY_ENVIRONMENT'] in ['live', 'production', 'test']:
|
2020-04-02 09:42:46 +01:00
|
|
|
message += ". Resolve using https://github.com/alphagov/notifications-manuals/wiki/Support-Runbook#deal-with-letters-still-in-sending" # noqa
|
2021-09-23 15:40:55 +01:00
|
|
|
|
|
|
|
|
ticket = NotifySupportTicket(
|
|
|
|
|
subject=f"[{current_app.config['NOTIFY_ENVIRONMENT']}] Letters still sending",
|
2021-10-29 10:33:46 +01:00
|
|
|
email_ccs=current_app.config['DVLA_EMAIL_ADDRESSES'],
|
2020-04-02 09:36:56 +01:00
|
|
|
message=message,
|
2021-09-23 15:40:55 +01:00
|
|
|
ticket_type=NotifySupportTicket.TYPE_INCIDENT,
|
|
|
|
|
technical_ticket=True,
|
|
|
|
|
ticket_categories=['notify_letters']
|
2020-04-02 09:36:56 +01:00
|
|
|
)
|
2021-09-23 15:40:55 +01:00
|
|
|
zendesk_client.send_ticket_to_zendesk(ticket)
|
2020-04-02 09:36:56 +01:00
|
|
|
else:
|
|
|
|
|
current_app.logger.info(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_letter_notifications_still_sending_when_they_shouldnt_be():
|
2019-01-14 17:22:41 +00:00
|
|
|
today = datetime.utcnow().date()
|
|
|
|
|
|
|
|
|
|
# Do nothing on the weekend
|
2019-10-08 18:16:45 +01:00
|
|
|
if today.isoweekday() in {6, 7}: # sat, sun
|
2020-04-02 09:36:56 +01:00
|
|
|
return 0, None
|
2019-01-14 17:22:41 +00:00
|
|
|
|
2019-10-08 18:16:45 +01:00
|
|
|
if today.isoweekday() in {1, 2}: # mon, tues. look for files from before the weekend
|
2019-01-14 17:22:41 +00:00
|
|
|
offset_days = 4
|
|
|
|
|
else:
|
|
|
|
|
offset_days = 2
|
2019-10-08 18:16:45 +01:00
|
|
|
|
2020-04-02 09:36:56 +01:00
|
|
|
expected_sent_date = today - timedelta(days=offset_days)
|
|
|
|
|
|
2019-10-08 18:16:45 +01:00
|
|
|
q = Notification.query.filter(
|
2019-01-14 17:22:41 +00:00
|
|
|
Notification.notification_type == LETTER_TYPE,
|
|
|
|
|
Notification.status == NOTIFICATION_SENDING,
|
|
|
|
|
Notification.key_type == KEY_TYPE_NORMAL,
|
2020-04-02 09:36:56 +01:00
|
|
|
func.date(Notification.sent_at) <= expected_sent_date
|
2019-10-08 18:16:45 +01:00
|
|
|
)
|
|
|
|
|
|
2020-04-02 09:36:56 +01:00
|
|
|
return q.count(), expected_sent_date
|
2019-01-14 17:22:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='raise-alert-if-no-letter-ack-file')
|
2019-01-16 14:11:03 +00:00
|
|
|
@cronitor('raise-alert-if-no-letter-ack-file')
|
2019-01-14 17:22:41 +00:00
|
|
|
def letter_raise_alert_if_no_ack_file_for_zip():
|
|
|
|
|
# get a list of zip files since yesterday
|
|
|
|
|
zip_file_set = set()
|
2019-04-03 11:03:42 +01:00
|
|
|
today_str = datetime.utcnow().strftime('%Y-%m-%d')
|
|
|
|
|
yesterday = datetime.now(tz=pytz.utc) - timedelta(days=1) # AWS datetime format
|
2019-01-14 17:22:41 +00:00
|
|
|
|
|
|
|
|
for key in s3.get_list_of_files_by_suffix(bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'],
|
2019-04-03 11:03:42 +01:00
|
|
|
subfolder=today_str + '/zips_sent',
|
2019-01-14 17:22:41 +00:00
|
|
|
suffix='.TXT'):
|
|
|
|
|
subname = key.split('/')[-1] # strip subfolder in name
|
2019-04-08 11:58:25 +01:00
|
|
|
zip_file_set.add(subname.upper().replace('.ZIP.TXT', ''))
|
2019-01-14 17:22:41 +00:00
|
|
|
|
|
|
|
|
# get acknowledgement file
|
|
|
|
|
ack_file_set = set()
|
|
|
|
|
|
|
|
|
|
for key in s3.get_list_of_files_by_suffix(bucket_name=current_app.config['DVLA_RESPONSE_BUCKET_NAME'],
|
|
|
|
|
subfolder='root/dispatch', suffix='.ACK.txt', last_modified=yesterday):
|
2019-04-08 11:58:25 +01:00
|
|
|
ack_file_set.add(key.lstrip('root/dispatch').upper().replace('.ACK.TXT', ''))
|
2019-01-14 17:22:41 +00:00
|
|
|
|
2021-10-08 13:30:09 +01:00
|
|
|
message = '\n'.join([
|
|
|
|
|
"Letter ack file does not contain all zip files sent."
|
|
|
|
|
"",
|
|
|
|
|
f"See runbook at https://github.com/alphagov/notifications-manuals/wiki/Support-Runbook#letter-ack-file-does-not-contain-all-zip-files-sent\n", # noqa
|
|
|
|
|
f"pdf bucket: {current_app.config['LETTERS_PDF_BUCKET_NAME']}, subfolder: {datetime.utcnow().strftime('%Y-%m-%d')}/zips_sent", # noqa
|
|
|
|
|
f"ack bucket: {current_app.config['DVLA_RESPONSE_BUCKET_NAME']}",
|
|
|
|
|
"",
|
|
|
|
|
f"Missing ack for zip files: {str(sorted(zip_file_set - ack_file_set))}",
|
|
|
|
|
])
|
|
|
|
|
|
2019-01-14 17:22:41 +00:00
|
|
|
# strip empty element before comparison
|
2019-04-03 11:03:42 +01:00
|
|
|
ack_file_set.discard('')
|
2019-01-14 17:22:41 +00:00
|
|
|
zip_file_set.discard('')
|
|
|
|
|
|
2019-04-03 11:03:42 +01:00
|
|
|
if len(zip_file_set - ack_file_set) > 0:
|
2019-01-14 17:22:41 +00:00
|
|
|
if current_app.config['NOTIFY_ENVIRONMENT'] in ['live', 'production', 'test']:
|
2021-09-23 16:08:30 +01:00
|
|
|
ticket = NotifySupportTicket(
|
2019-01-14 17:22:41 +00:00
|
|
|
subject="Letter acknowledge error",
|
|
|
|
|
message=message,
|
2021-09-23 16:08:30 +01:00
|
|
|
ticket_type=NotifySupportTicket.TYPE_INCIDENT,
|
|
|
|
|
technical_ticket=True,
|
|
|
|
|
ticket_categories=['notify_letters']
|
2019-01-14 17:22:41 +00:00
|
|
|
)
|
2021-09-23 16:08:30 +01:00
|
|
|
zendesk_client.send_ticket_to_zendesk(ticket)
|
2019-01-14 17:22:41 +00:00
|
|
|
current_app.logger.error(message)
|
|
|
|
|
|
2019-04-03 11:03:42 +01:00
|
|
|
if len(ack_file_set - zip_file_set) > 0:
|
2019-01-14 17:22:41 +00:00
|
|
|
current_app.logger.info(
|
2019-04-03 11:03:42 +01:00
|
|
|
"letter ack contains zip that is not for today: {}".format(ack_file_set - zip_file_set)
|
2019-01-14 17:22:41 +00:00
|
|
|
)
|
2021-03-11 18:15:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='save-daily-notification-processing-time')
|
|
|
|
|
@cronitor("save-daily-notification-processing-time")
|
|
|
|
|
def save_daily_notification_processing_time(bst_date=None):
|
|
|
|
|
# bst_date is a string in the format of "YYYY-MM-DD"
|
|
|
|
|
if bst_date is None:
|
|
|
|
|
# if a date is not provided, we run against yesterdays data
|
|
|
|
|
bst_date = (datetime.utcnow() - timedelta(days=1)).date()
|
|
|
|
|
else:
|
|
|
|
|
bst_date = datetime.strptime(bst_date, "%Y-%m-%d").date()
|
|
|
|
|
|
|
|
|
|
start_time = get_london_midnight_in_utc(bst_date)
|
|
|
|
|
end_time = get_london_midnight_in_utc(bst_date + timedelta(days=1))
|
2021-03-11 18:53:43 +00:00
|
|
|
result = dao_get_notifications_processing_time_stats(start_time, end_time)
|
2021-03-11 18:15:11 +00:00
|
|
|
insert_update_processing_time(
|
|
|
|
|
FactProcessingTime(
|
|
|
|
|
bst_date=bst_date,
|
|
|
|
|
messages_total=result.messages_total,
|
|
|
|
|
messages_within_10_secs=result.messages_within_10_secs
|
|
|
|
|
)
|
|
|
|
|
)
|