Skip to content

Commit eb50ca7

Browse files
authored
Merge pull request #1758 from GSA/check_csvs
check which services have csvs
2 parents 5d9e612 + 0044a15 commit eb50ca7

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

app/aws/s3.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
from app import job_cache, job_cache_lock
1313
from app.clients import AWS_CLIENT_CONFIG
14+
15+
# from app.service.rest import get_service_by_id
1416
from notifications_utils import aware_utcnow
1517

1618
FILE_LOCATION_STRUCTURE = "service-{}-notify/{}.csv"
@@ -162,6 +164,34 @@ def cleanup_old_s3_objects():
162164
current_app.logger.exception(
163165
"#delete-old-s3-objects An error occurred while cleaning up old s3 objects",
164166
)
167+
try:
168+
response = s3_client.list_objects_v2(Bucket=bucket_name)
169+
170+
service_ids = set()
171+
while True:
172+
for obj in response.get("Contents", []):
173+
# Get the service id out of the upload key
174+
key = obj["Key"]
175+
object_arr = key.split("/")
176+
service_id = object_arr[0]
177+
service_id = service_id.replace("-service-notify", "")
178+
service_ids.add(service_id)
179+
if "NextContinuationToken" in response:
180+
response = s3_client.list_objects_v2(
181+
Bucket=bucket_name,
182+
ContinuationToken=response["NextContinuationToken"],
183+
)
184+
else:
185+
break
186+
retained_services = []
187+
for service_id in service_ids:
188+
retained_services.append(service_id)
189+
190+
return service_ids
191+
except Exception as error:
192+
current_app.logger.exception(
193+
f"#delete-old-s3-objects An error occurred while cleaning up old s3 objects: {str(error)}"
194+
)
165195

166196

167197
def get_job_id_from_s3_object_key(key):

app/celery/tasks.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
2020
from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service
2121
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
22+
from app.dao.services_dao import dao_fetch_service_by_id
2223
from app.dao.templates_dao import dao_get_template_by_id
2324
from app.enums import JobStatus, KeyType, NotificationType
2425
from app.errors import TotalRequestsError
@@ -496,7 +497,15 @@ def clean_job_cache():
496497

497498
@notify_celery.task(name="delete-old-s3-objects")
498499
def delete_old_s3_objects():
499-
s3.cleanup_old_s3_objects()
500+
501+
existing_service_ids = s3.cleanup_old_s3_objects()
502+
service_names = []
503+
for service_id in existing_service_ids:
504+
service = dao_fetch_service_by_id(service_id)
505+
service_names.append(service.name)
506+
current_app.logger.info(
507+
f"#delete-old-s3-objects Services with retained csvs: {service_names}"
508+
)
500509

501510

502511
@notify_celery.task(name="process-incomplete-jobs")

0 commit comments

Comments
 (0)