diff --git a/robotoff/images.py b/robotoff/images.py index 497a51858b..bc5803f909 100644 --- a/robotoff/images.py +++ b/robotoff/images.py @@ -203,6 +203,11 @@ def delete_images(product_id: ProductIdentifier, image_ids: list[str]): :param image_ids: a list of image IDs to delete. Each image ID must be a digit. """ + + if not product_id.is_valid(): + logger.warning("Could not delete images, invalid product identifier") + return + server_type = product_id.server_type.name # Perform batching as we don't know the number of images to delete updated_models = [] diff --git a/robotoff/types.py b/robotoff/types.py index 67efe083b6..d936b1768e 100644 --- a/robotoff/types.py +++ b/robotoff/types.py @@ -283,6 +283,9 @@ def __repr__(self) -> str: def __hash__(self) -> int: return hash((self.barcode, self.server_type)) + def is_valid(self) -> bool: + return bool(self.barcode and self.server_type) + @enum.unique class ElasticSearchIndex(str, enum.Enum): diff --git a/robotoff/workers/tasks/product_updated.py b/robotoff/workers/tasks/product_updated.py index 6309c2ac76..3fb16ea52b 100644 --- a/robotoff/workers/tasks/product_updated.py +++ b/robotoff/workers/tasks/product_updated.py @@ -30,6 +30,11 @@ def update_insights_job(product_id: ProductIdentifier, diffs: JSONType) -> None: """ logger.info("Running `update_insights` for %s", product_id) + # Check for valid product identifier + if not product_id.is_valid(): + logger.info("Invalid product identifier received, skipping product update") + return + try: with Lock( name=f"robotoff:product_update_job:{product_id.server_type.name}:{product_id.barcode}",