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);
}