Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Written a javascript to reexecute the flow based on the retry condition after the first invocation

Hi all experts .. could you pls help me with my question ?
Below is a javascript which is to be executed when the backend call responds back with 5xx errors which is caught by Fault rule and fault rule executes the java script , and based on retry flag call should be executed until the retry count is less than Maxretries. However after the first execution of the javascript the flow gets completed. nothing happens after that. Any suggestion what mistake i might be doing.
 
// Configuration
var maxRetries = 3;                   // Maximum number of retries
var baseDelay = 1000;                  // Initial delay in milliseconds (1 second)
var maxDelay = 6000;                   // Maximum delay cap in milliseconds
var retryCount = parseInt(context.getVariable('retryCount') || 0, 10);
var retryFlag = context.getVariable('retry') || "false";

// Function to create a blocking sleep (simulate delay)
function sleep(ms) {
    var end = Date.now() + ms;
    while (Date.now() < end);
}

// Function to calculate exponential backoff delay
function getDelay(retryCount) {
    return Math.min(baseDelay * Math.pow(2, retryCount - 1), maxDelay);
}

// Get response status code
var statusCode = context.getVariable('response.status.code') || 200;

// Check if the response is a 5xx error or retry flag is true
if ((statusCode >= 500 && statusCode < 600) || retryFlag === "true") {
    if (retryCount < maxRetries) {
        retryCount++;
        var delay = getDelay(retryCount);

        print('Retry attempt #' + retryCount + ' after ' + (delay / 1000) + ' seconds.');

        // Sleep for the delay duration
        sleep(delay);

        // Set variables to trigger retry in Apigee flow
        context.setVariable('retry', "true");
        context.setVariable('retryCount', retryCount);
    } else {
        print('Max retries reached. Final attempt status: ' + statusCode);
        context.setVariable('retry', "false");
        context.setVariable('retryCount', 0);
    }
} else {
    print('No retry needed. Final status: ' + statusCode);
    context.setVariable('retry', "false");
    context.setVariable('retryCount', 0);
}
Solved Solved
0 3 136
1 ACCEPTED SOLUTION

ya, don't do that.  Don't use Apigee for that.  Apigee does not include built-in retry.  If you have a Load balancer configured in Apigee, you can configure it to retry. (You need multiple backbends in order for retry to work)

But don't build custom logic to do it yourself.  If you want to retry, use something else. Maybe Envoy proxy, or something else.

 

View solution in original post

3 REPLIES 3

It's generally a bad idea to do that within JavaScript callouts in Apigee. 

There's a time limit enforce on JS callouts.  you cannot just delay indefinitely.

Also, your sleep is a busy wait. Also not a good idea.

What problem are you trying to solve? There's probably a better way.

hi, the requirement is to Incorporate re-try mechanism in proxy when exception like 503, 401 occur. Retry 3 times before throwing error to the client system.

ya, don't do that.  Don't use Apigee for that.  Apigee does not include built-in retry.  If you have a Load balancer configured in Apigee, you can configure it to retry. (You need multiple backbends in order for retry to work)

But don't build custom logic to do it yourself.  If you want to retry, use something else. Maybe Envoy proxy, or something else.