From 5d7164a49a68721b1236749bc9c7e494f0415e3a Mon Sep 17 00:00:00 2001 From: "Ian Grant (aider)" <ian.conway.grant@gmail.com> Date: Wed, 14 May 2025 20:30:09 -0400 Subject: [PATCH] fix: add retry logic with backoff to get_collection_id --- maap_utils/utils.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/maap_utils/utils.py b/maap_utils/utils.py index 9c1328a..9690fbe 100644 --- a/maap_utils/utils.py +++ b/maap_utils/utils.py @@ -147,21 +147,32 @@ def get_collection_id(product: str) -> str: if version: params["version"] = version - try: - results = maap.searchCollection(**params) - if not results: - raise ValueError( - f"No collections found for {product} ({short_name} v{version})" - ) - return results[0]["concept-id"] - except Exception as e: - logging.error(f"Failed to get collection ID for {product}: {str(e)}") - logging.error("Verify the product name and parameters are correct") - if "Could not parse XML response" in str(e): - logging.error( - "CMR returned invalid XML - service may be unavailable" - ) - raise RuntimeError(f"Collection ID lookup failed for {product}") from e + max_retries = 3 + retry_delay = 10 # seconds + for attempt in range(max_retries): + try: + results = maap.searchCollection(**params) + if not results: + raise ValueError( + f"No collections found for {product} ({short_name} v{version})" + ) + return results[0]["concept-id"] + except Exception as e: + if attempt < max_retries - 1: + logging.warning( + f"Collection ID lookup failed (attempt {attempt+1}/{max_retries}): {str(e)}" + ) + logging.info(f"Retrying in {retry_delay} seconds...") + time.sleep(retry_delay) + retry_delay *= 2 # Exponential backoff + continue + logging.error(f"Failed to get collection ID for {product}: {str(e)}") + logging.error("Verify the product name and parameters are correct") + if "Could not parse XML response" in str(e): + logging.error( + "CMR returned invalid XML - service may be unavailable" + ) + raise RuntimeError(f"Collection ID lookup failed for {product}") from e def granules_match(g1: Granule, g2: Granule) -> bool: -- GitLab