Skip to content

Issue: Amazon Ads API Report Polling Was Working, But Now Reports Stay in PENDING

0

We are experiencing an issue while polling the Amazon Ads API to check the status of reports. The polling mechanism was working fine before, but for the last 3 days, reports are not completing on time, and our retry logic stops before fetching the data.

API & Retry Mechanism Details: **API Endpoint: **https://advertising-apl.amazon.com/reporting/reports/:reportId (changed the url from apl to api [bcz its not allowing]) Expected Behavior: Reports should eventually move from PENDING to COMPLETED, allowing us to retrieve the data. **Actual Behavior: **Since the last 3 days, reports remain stuck in PENDING for an unusually long time.

Error Log:

**Amazon Ads API Logs **


Timestamp: 2025-01-31T14:29:22.724+05:30
{
    "timestamp": "2025-01-31 08:59:22,724",
    "level": "INFO",
    "message": "Attempting to execute process to get reports.",
    "logger": "ingestion",
    "func_name": "execute",
    "line_no": 256,
    "file_name": "amazon_ads.py",
    "thread_name": "MainThread",
    "process": 1
}

Timestamp: 2025-01-31T14:29:22.724+05:30
{
    "timestamp": "2025-01-31 08:59:22,724",
    "level": "INFO",
    "message": "Attempting to generate access token.",
    "logger": "ingestion",
    "func_name": "get_access_token",
    "line_no": 83,
    "file_name": "amazon_ads.py",
    "thread_name": "MainThread",
    "process": 1
}

Timestamp: 2025-01-31T14:29:22.812+05:30
{
    "timestamp": "2025-01-31 08:59:22,812",
    "level": "INFO",
    "message": "Iteration started",
    "logger": "ingestion",
    "func_name": "retry_until_condition_satisfied_reportVersion",
    "line_no": 203,
    "file_name": "request_util.py",
    "thread_name": "MainThread",
    "process": 1
}

Timestamp: 2025-01-31T14:29:22.947+05:30
{
    "timestamp": "2025-01-31 08:59:22,946",
    "level": "INFO",
    "message": "Reports are not yet completed at server end.",
    "logger": "ingestion",
    "func_name": "retry_until_condition_satisfied_reportVersion",
    "line_no": 209,
    "file_name": "request_util.py",
    "thread_name": "MainThread",
    "process": 1
}

Retry Logic Using tenacity:

@classmethod
    @tenacity.retry(
        retry=tenacity.retry_if_exception_type(RetryConditionUnsatisfiedError)
        or tenacity.retry_if_exception_type(RetryException),
        stop=tenacity.stop_after_attempt(MAX_RETRY_ATTEMPT),
        wait=tenacity.wait_exponential(multiplier=MULTIPLIER, max=MAX_WAIT_TIME),
        reraise=False,
        retry_error_callback=on_retry_error,
    )
    def retry_until_condition_satisfied(
        cls,
        method: str,
        url: str,
        condition: t.Callable[..., bool],
        params: t.Dict[str, t.Any] = {},
        retry_error_codes: t.List[int] = [],
    ) -> t.Any:
        logger.info("Iteration started")
        response = requests.request(method, url, **params)

        if response.status_code in API_SUCCESS_STATUS_CODES:
            if condition(response=response):
                return response
            logger.info("Reports are not yet completed at server end.")
            raise RetryConditionUnsatisfiedError(
                {
                    "errorMessage": "Reports are not yet completed at server end.",
                    "errorCode": response.status_code,
                    "response": response,
                },
            )

        if len(retry_error_codes) > 0:
            RETRY_ERRORS.extend(retry_error_codes)

        if response.status_code in RETRY_ERRORS:
            raise RetryException(
                {
                    "errorCode": response.status_code,
                    "response": response,
                    "errorMessage": f"API failed with {response.status_code} :: Error Text: {response.text}",
                },
            )

        raise RequestError({"errorCode": response.status_code, "response": response})

Issues Faced in the Last 3 Days:

Reports No Longer Move to COMPLETED: Previously, the function retried until the report was ready. Now, the report remains in PENDING, and retries fail.

  • "Possible Amazon API Changes* We haven't changed anything in our implementation. Has Amazon changed report processing times or introduced new limitations?

Unclear Wait Time Impact on Retries: Our settings should allow retries with an exponential backoff up to 244 seconds max. Could something in tenacity’s retry mechanism be preventing it from retrying long enough?

Questions:

  1. Has Amazon Ads API changed its report processing times in the last few days?
  2. Why do reports now stay in PENDING indefinitely, despite successful retries?
  3. What is the best way to handle long-running reports in Amazon Ads API?
  4. Any insights on API changes, debugging retry behavior, or potential workarounds would be greatly appreciated!
1 Answer
-1

Based on the information provided, it appears you're experiencing an issue with the Amazon Ads API where reports are remaining in a PENDING state for an unusually long time. This is a change from the previous behavior where reports would eventually move to COMPLETED status. While there's no specific information about recent changes to the Amazon Ads API processing times, this type of issue can occur due to various reasons.

  1. Regarding potential API changes, it's not uncommon for cloud services to experience temporary processing delays or backlogs. However, without official confirmation from Amazon, we can't say for certain if there have been any changes to report processing times.

  2. The reports staying in PENDING indefinitely could be due to a backlog on Amazon's side, or potentially a change in how long certain reports take to process. Your retry logic seems sound, using exponential backoff with a maximum wait time of 244 seconds.

  3. For handling long-running reports in the Amazon Ads API, consider implementing a more robust long-polling mechanism. Instead of continuously retrying within a single request, you might want to implement a system that periodically checks the status of the report, perhaps every few minutes or hours, depending on the expected processing time.

  4. To debug and work around this issue:

    a. Increase your maximum retry attempts and maximum wait time to account for potentially longer processing times.

    b. Implement a separate polling mechanism that runs independently of your main process, checking the status of pending reports at regular intervals.

    c. Consider implementing a queuing system where report requests are stored and periodically checked for completion, rather than blocking on a single request.

    d. Log more detailed information about each retry attempt, including the exact response from the API, to help identify any patterns or changes in behavior.

    e. Reach out to Amazon Ads API support for information on any known issues or changes in report processing times.

Remember that when dealing with external APIs, it's important to design your system to be resilient to changes in processing times and potential service disruptions. Implementing a more flexible polling strategy and robust error handling can help mitigate these types of issues in the future.
Sources
Guidance for Ingesting Amazon Vendor Central and Amazon Ads Data on AWS
Long polling is not enabled in Amazon SQS | Amazon Q, Detector Library

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.