Skip to content

Commit 5c13a3c

Browse files
committed
fix(encoding): improve error handling for image downloads and track failures
1 parent a00c92f commit 5c13a3c

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

‎main_api/application/use_cases/background_tasks.py‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from application.ports.queue import ITaskQueueService
66
from typing import Callable
77
import uuid
8-
from domain.exceptions import ZipGenerationError, StorageDownloadError, InferenceError
8+
from domain.exceptions import ZipGenerationError, InferenceError
99
from application.dtos import (
1010
BackgroundEncodingResult,
1111
BackgroundZipResult,
@@ -45,20 +45,22 @@ async def execute(
4545

4646
valid_keys: list[str] = []
4747
valid_b64: list[str] = []
48+
failed_keys: list[str] = []
4849
for key, b64 in zip(keys, b64_images):
4950
if isinstance(b64, Exception):
50-
raise StorageDownloadError(
51-
f"Failed to download {key} after retries: {b64}"
52-
)
51+
logger.warning(f"Failed to download {key} after retries: {b64}. Skipping this image.")
52+
failed_keys.append(key)
53+
continue
5354
elif b64 is not None:
5455
valid_keys.append(key)
5556
valid_b64.append(str(b64))
5657

5758
if not valid_keys:
5859
return {
5960
"encoded": 0,
60-
"no_encodings_found": len(keys),
61-
"total": len(keys)
61+
"no_encodings_found": 0,
62+
"total": len(keys),
63+
"failures": failed_keys
6264
}
6365

6466
try:
@@ -94,11 +96,12 @@ async def execute(
9496
logger.info(f"Database insert for {len(insert_data)} encodings took {db_time - db_start:.2f}s")
9597

9698
encoded = sum(1 for image_faces in results if any(f.get("embedding") for f in image_faces))
97-
no_encodings_found = len(keys) - encoded
99+
no_encodings_found = len(valid_keys) - encoded
98100
return {
99101
"encoded": encoded,
100102
"no_encodings_found": no_encodings_found,
101-
"total": len(keys)
103+
"total": len(keys),
104+
"failures": failed_keys
102105
}
103106
except Exception as e:
104107
raise InferenceError(f"Failed to infer batch: {e}") from e

0 commit comments

Comments
 (0)