2017-12-13 15:52:38 +00:00
|
|
|
import math
|
2018-03-19 13:52:01 +00:00
|
|
|
from datetime import datetime
|
2017-12-19 14:18:05 +00:00
|
|
|
|
2018-03-19 13:52:01 +00:00
|
|
|
from botocore.exceptions import ClientError as BotoClientError
|
2017-12-19 14:18:05 +00:00
|
|
|
from flask import current_app
|
2017-12-11 11:00:27 +00:00
|
|
|
from requests import (
|
|
|
|
|
post as requests_post,
|
|
|
|
|
RequestException
|
|
|
|
|
)
|
2018-03-19 13:52:01 +00:00
|
|
|
|
|
|
|
|
from notifications_utils.statsd_decorators import statsd
|
2017-12-11 11:00:27 +00:00
|
|
|
|
|
|
|
|
from app import notify_celery
|
|
|
|
|
from app.aws import s3
|
2017-12-19 14:18:05 +00:00
|
|
|
from app.config import QueueNames, TaskNames
|
2017-12-13 15:52:38 +00:00
|
|
|
from app.dao.notifications_dao import (
|
|
|
|
|
get_notification_by_id,
|
|
|
|
|
update_notification_status_by_id,
|
2018-01-15 17:00:00 +00:00
|
|
|
dao_update_notification,
|
2018-03-23 12:04:37 +00:00
|
|
|
dao_get_notification_by_reference,
|
2018-01-15 17:00:00 +00:00
|
|
|
dao_get_notifications_by_references,
|
2018-03-19 13:52:01 +00:00
|
|
|
dao_update_notifications_by_reference,
|
2017-12-13 15:52:38 +00:00
|
|
|
)
|
2018-03-19 13:52:01 +00:00
|
|
|
from app.letters.utils import (
|
|
|
|
|
delete_pdf_from_letters_scan_bucket,
|
|
|
|
|
get_reference_from_filename,
|
2018-03-23 12:04:37 +00:00
|
|
|
move_scanned_pdf_to_test_or_live_pdf_bucket,
|
2018-03-19 13:52:01 +00:00
|
|
|
upload_letter_pdf
|
|
|
|
|
)
|
2018-03-23 12:04:37 +00:00
|
|
|
from app.models import (
|
|
|
|
|
KEY_TYPE_TEST,
|
|
|
|
|
NOTIFICATION_CREATED,
|
|
|
|
|
NOTIFICATION_DELIVERED,
|
|
|
|
|
NOTIFICATION_VIRUS_SCAN_FAILED,
|
|
|
|
|
)
|
2017-12-11 11:00:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(bind=True, name="create-letters-pdf", max_retries=15, default_retry_delay=300)
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def create_letters_pdf(self, notification_id):
|
|
|
|
|
try:
|
2017-12-12 11:56:42 +00:00
|
|
|
notification = get_notification_by_id(notification_id, _raise=True)
|
2017-12-11 11:00:27 +00:00
|
|
|
|
2017-12-13 15:52:38 +00:00
|
|
|
pdf_data, billable_units = get_letters_pdf(
|
2017-12-11 11:00:27 +00:00
|
|
|
notification.template,
|
|
|
|
|
contact_block=notification.reply_to_text,
|
|
|
|
|
org_id=notification.service.dvla_organisation.id,
|
|
|
|
|
values=notification.personalisation
|
|
|
|
|
)
|
2018-01-23 17:45:35 +00:00
|
|
|
|
2018-02-23 10:39:32 +00:00
|
|
|
upload_letter_pdf(notification, pdf_data)
|
2017-12-13 15:52:38 +00:00
|
|
|
|
2017-12-14 11:38:17 +00:00
|
|
|
notification.billable_units = billable_units
|
|
|
|
|
dao_update_notification(notification)
|
2017-12-13 15:52:38 +00:00
|
|
|
|
2017-12-14 11:38:17 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
|
'Letter notification reference {reference}: billable units set to {billable_units}'.format(
|
|
|
|
|
reference=str(notification.reference), billable_units=billable_units))
|
2017-12-13 15:52:38 +00:00
|
|
|
|
2017-12-11 11:00:27 +00:00
|
|
|
except (RequestException, BotoClientError):
|
|
|
|
|
try:
|
|
|
|
|
current_app.logger.exception(
|
|
|
|
|
"Letters PDF notification creation for id: {} failed".format(notification_id)
|
|
|
|
|
)
|
|
|
|
|
self.retry(queue=QueueNames.RETRY)
|
|
|
|
|
except self.MaxRetriesExceededError:
|
|
|
|
|
current_app.logger.exception(
|
|
|
|
|
"RETRY FAILED: task create_letters_pdf failed for notification {}".format(notification_id),
|
|
|
|
|
)
|
|
|
|
|
update_notification_status_by_id(notification_id, 'technical-failure')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_letters_pdf(template, contact_block, org_id, values):
|
|
|
|
|
template_for_letter_print = {
|
|
|
|
|
"subject": template.subject,
|
|
|
|
|
"content": template.content
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'letter_contact_block': contact_block,
|
|
|
|
|
'template': template_for_letter_print,
|
|
|
|
|
'values': values,
|
|
|
|
|
'dvla_org_id': org_id,
|
|
|
|
|
}
|
|
|
|
|
resp = requests_post(
|
|
|
|
|
'{}/print.pdf'.format(
|
|
|
|
|
current_app.config['TEMPLATE_PREVIEW_API_HOST']
|
|
|
|
|
),
|
|
|
|
|
json=data,
|
|
|
|
|
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
|
|
|
|
)
|
|
|
|
|
resp.raise_for_status()
|
|
|
|
|
|
2017-12-13 15:52:38 +00:00
|
|
|
pages_per_sheet = 2
|
|
|
|
|
billable_units = math.ceil(int(resp.headers.get("X-pdf-page-count", 0)) / pages_per_sheet)
|
|
|
|
|
|
|
|
|
|
return resp.content, billable_units
|
2017-12-19 14:18:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='collate-letter-pdfs-for-day')
|
|
|
|
|
def collate_letter_pdfs_for_day(date):
|
|
|
|
|
letter_pdfs = s3.get_s3_bucket_objects(
|
|
|
|
|
current_app.config['LETTERS_PDF_BUCKET_NAME'],
|
|
|
|
|
subfolder=date
|
|
|
|
|
)
|
|
|
|
|
for letters in group_letters(letter_pdfs):
|
|
|
|
|
filenames = [letter['Key'] for letter in letters]
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
'Calling task zip-and-send-letter-pdfs for {} pdfs of total size {:,} bytes'.format(
|
|
|
|
|
len(filenames),
|
|
|
|
|
sum(letter['Size'] for letter in letters)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
notify_celery.send_task(
|
|
|
|
|
name=TaskNames.ZIP_AND_SEND_LETTER_PDFS,
|
2017-12-22 15:38:49 +00:00
|
|
|
kwargs={'filenames_to_zip': filenames},
|
2018-01-02 17:18:01 +00:00
|
|
|
queue=QueueNames.PROCESS_FTP,
|
|
|
|
|
compression='zlib'
|
2017-12-19 14:18:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def group_letters(letter_pdfs):
|
|
|
|
|
"""
|
|
|
|
|
Group letters in chunks of MAX_LETTER_PDF_ZIP_FILESIZE. Will add files to lists, never going over that size.
|
|
|
|
|
If a single file is (somehow) larger than MAX_LETTER_PDF_ZIP_FILESIZE that'll be in a list on it's own.
|
|
|
|
|
If there are no files, will just exit (rather than yielding an empty list).
|
|
|
|
|
"""
|
|
|
|
|
running_filesize = 0
|
|
|
|
|
list_of_files = []
|
|
|
|
|
for letter in letter_pdfs:
|
2018-01-15 17:00:00 +00:00
|
|
|
if letter['Key'].lower().endswith('.pdf') and letter_in_created_state(letter['Key']):
|
2018-01-02 17:18:01 +00:00
|
|
|
if (
|
|
|
|
|
running_filesize + letter['Size'] > current_app.config['MAX_LETTER_PDF_ZIP_FILESIZE'] or
|
|
|
|
|
len(list_of_files) >= current_app.config['MAX_LETTER_PDF_COUNT_PER_ZIP']
|
|
|
|
|
):
|
2017-12-22 15:38:49 +00:00
|
|
|
yield list_of_files
|
|
|
|
|
running_filesize = 0
|
|
|
|
|
list_of_files = []
|
|
|
|
|
|
|
|
|
|
running_filesize += letter['Size']
|
|
|
|
|
list_of_files.append(letter)
|
2017-12-19 14:18:05 +00:00
|
|
|
|
|
|
|
|
if list_of_files:
|
|
|
|
|
yield list_of_files
|
2018-01-15 17:00:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def letter_in_created_state(filename):
|
|
|
|
|
# filename looks like '2018-01-13/NOTIFY.ABCDEF1234567890.D.2.C.C.20180113120000.PDF'
|
|
|
|
|
subfolder = filename.split('/')[0]
|
2018-03-19 13:52:01 +00:00
|
|
|
ref = get_reference_from_filename(filename)
|
2018-01-15 17:00:00 +00:00
|
|
|
notifications = dao_get_notifications_by_references([ref])
|
|
|
|
|
if notifications:
|
|
|
|
|
if notifications[0].status == NOTIFICATION_CREATED:
|
|
|
|
|
return True
|
|
|
|
|
current_app.logger.info('Collating letters for {} but notification with reference {} already in {}'.format(
|
|
|
|
|
subfolder,
|
|
|
|
|
ref,
|
|
|
|
|
notifications[0].status
|
|
|
|
|
))
|
|
|
|
|
return False
|
2018-03-19 13:52:01 +00:00
|
|
|
|
|
|
|
|
|
2018-03-20 14:56:42 +00:00
|
|
|
@notify_celery.task(name='process-virus-scan-passed')
|
|
|
|
|
def process_virus_scan_passed(filename):
|
2018-03-19 13:52:01 +00:00
|
|
|
current_app.logger.info('Virus scan passed: {}'.format(filename))
|
|
|
|
|
reference = get_reference_from_filename(filename)
|
2018-03-23 12:04:37 +00:00
|
|
|
notification = dao_get_notification_by_reference(reference)
|
2018-03-19 13:52:01 +00:00
|
|
|
|
2018-03-23 12:04:37 +00:00
|
|
|
is_test_key = notification.key_type == KEY_TYPE_TEST
|
|
|
|
|
move_scanned_pdf_to_test_or_live_pdf_bucket(
|
|
|
|
|
filename,
|
|
|
|
|
is_test_letter=is_test_key
|
|
|
|
|
)
|
|
|
|
|
update_letter_pdf_status(
|
|
|
|
|
reference,
|
|
|
|
|
NOTIFICATION_DELIVERED if is_test_key else NOTIFICATION_CREATED
|
|
|
|
|
)
|
2018-03-19 13:52:01 +00:00
|
|
|
|
|
|
|
|
|
2018-03-20 14:56:42 +00:00
|
|
|
@notify_celery.task(name='process-virus-scan-failed')
|
|
|
|
|
def process_virus_scan_failed(filename):
|
2018-03-23 14:24:23 +00:00
|
|
|
current_app.logger.exception('Virus scan failed: {}'.format(filename))
|
2018-03-19 13:52:01 +00:00
|
|
|
delete_pdf_from_letters_scan_bucket(filename)
|
|
|
|
|
reference = get_reference_from_filename(filename)
|
2018-03-23 12:04:37 +00:00
|
|
|
updated_count = update_letter_pdf_status(reference, NOTIFICATION_VIRUS_SCAN_FAILED)
|
2018-03-19 13:52:01 +00:00
|
|
|
|
|
|
|
|
if updated_count != 1:
|
|
|
|
|
raise Exception(
|
|
|
|
|
"There should only be one letter notification for each reference. Found {} notifications".format(
|
|
|
|
|
updated_count
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_letter_pdf_status(reference, status):
|
|
|
|
|
return dao_update_notifications_by_reference(
|
|
|
|
|
references=[reference],
|
|
|
|
|
update_dict={
|
|
|
|
|
'status': status,
|
|
|
|
|
'updated_at': datetime.utcnow()
|
|
|
|
|
})
|