Fixes to the delete letter notifications.

If there are no files to delete we won't get an excpetion.
Wrap the delete file in a try/except to avoid stopping the entire task.
Fix the missing slash for the file name.
This commit is contained in:
Rebecca Law
2018-08-13 14:09:51 +01:00
parent bffef09d3d
commit f965322f25
3 changed files with 10 additions and 4 deletions

View File

@@ -61,7 +61,8 @@ def get_s3_bucket_objects(bucket_name, subfolder='', older_than=7, limit_days=2)
all_objects_in_bucket = []
for page in page_iterator:
all_objects_in_bucket.extend(page['Contents'])
if page.get('Contents'):
all_objects_in_bucket.extend(page['Contents'])
return all_objects_in_bucket

View File

@@ -6,6 +6,7 @@ from datetime import (
date
)
from boto.exception import BotoClientError
from flask import current_app
from notifications_utils.recipients import (
@@ -345,7 +346,7 @@ def _delete_letters_from_s3(query):
if letter.sent_at:
sent_at = str(letter.sent_at.date())
prefix = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
folder=sent_at,
folder=sent_at + "/",
reference=letter.reference,
duplex="D",
letter_class="2",
@@ -355,7 +356,11 @@ def _delete_letters_from_s3(query):
).upper()[:-5]
s3_objects = get_s3_bucket_objects(bucket_name=bucket_name, subfolder=prefix)
for s3_object in s3_objects:
remove_s3_object(bucket_name, s3_object['Key'])
try:
remove_s3_object(bucket_name, s3_object['Key'])
except BotoClientError:
current_app.logger.exception(
"Could not delete S3 object with filename: {}".format(s3_object['Key']))
@statsd(namespace="dao")

View File

@@ -114,7 +114,7 @@ def test_delete_notifications_for_days_of_retention(sample_service, notification
assert len(Notification.query.filter_by(notification_type=notification_type).all()) == 1
if notification_type == 'letter':
mock_get_s3.assert_called_with(bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'],
subfolder="{}NOTIFY.LETTER_REF.D.2.C.C".format(str(datetime.utcnow().date()))
subfolder="{}/NOTIFY.LETTER_REF.D.2.C.C".format(str(datetime.utcnow().date()))
)
assert mock_get_s3.call_count == 2
else: