Added code to delete files that are stale (#1936)

This commit is contained in:
Alex Janousek
2025-08-27 15:13:00 -04:00
committed by GitHub
parent 55444db56e
commit 52e68eb9db
2 changed files with 28 additions and 2 deletions

View File

@@ -582,6 +582,12 @@ def _generate_notifications_report(service_id, report_id, limit_days):
count = 0
if len(pagination.items) == 0:
current_app.logger.info(f"SKIP {service_id}")
# Delete stale report when there's no new data
_, file_location, _, _, _ = get_csv_location(service_id, report_id)
s3.delete_s3_object(file_location)
current_app.logger.info(f"Deleted stale report {file_location} - no new data")
return
start_time = time.time()
for notification in pagination.items:

View File

@@ -1829,6 +1829,7 @@ def test_generate_notifications_report_normal_case(
mock_get_phone_number,
mock_get_personalisation,
mock_get_notifications,
notify_api,
):
mock_get_notifications.return_value.items = [get_mock_notification()]
@@ -1858,11 +1859,30 @@ def test_generate_notifications_report_normal_case(
assert isinstance(kwargs["filedata"], io.BytesIO)
@patch("app.aws.s3.delete_s3_object")
@patch("app.celery.tasks.get_csv_location")
@patch("app.dao.notifications_dao.get_notifications_for_service")
@patch("app.celery.tasks.current_app")
def test_generate_notifications_report_no_notifications(
mock_current_app, mock_get_notifications
mock_current_app,
mock_get_notifications,
mock_get_csv_location,
mock_delete_s3,
notify_api,
):
mock_get_notifications.return_value.items = []
mock_get_csv_location.return_value = (
"bucket",
"service-id-service-notify/report-id.csv",
"access",
"secret",
"region",
)
_generate_notifications_report("service-id", "report-id", 7)
mock_current_app.logger.info.assert_called_with("SKIP service-id")
mock_current_app.logger.info.assert_any_call("SKIP service-id")
mock_delete_s3.assert_called_once_with("service-id-service-notify/report-id.csv")
mock_current_app.logger.info.assert_any_call(
"Deleted stale report service-id-service-notify/report-id.csv - no new data"
)