|
11 | 11 |
|
12 | 12 | from app import job_cache, job_cache_lock |
13 | 13 | from app.clients import AWS_CLIENT_CONFIG |
| 14 | + |
| 15 | +# from app.service.rest import get_service_by_id |
14 | 16 | from notifications_utils import aware_utcnow |
15 | 17 |
|
16 | 18 | FILE_LOCATION_STRUCTURE = "service-{}-notify/{}.csv" |
@@ -162,6 +164,34 @@ def cleanup_old_s3_objects(): |
162 | 164 | current_app.logger.exception( |
163 | 165 | "#delete-old-s3-objects An error occurred while cleaning up old s3 objects", |
164 | 166 | ) |
| 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 | + ) |
165 | 195 |
|
166 | 196 |
|
167 | 197 | def get_job_id_from_s3_object_key(key): |
|
0 commit comments