Hi,
I have a function that closes tickets using the API. I have written an error trap for 429 errors. Mostly the function runs fine without triggering 429 errors (i.e. there is no throttling issue). However, sometimes multiple 429 errors occur in the console and it seems to go into an infinite loop.
I've put the function below. Note that the console.log does not seem to fire -- meaning the error trap does not catch the 429 error.
Can anyone advise?
async function closeTickets(arrayTicketIDs)
{
// close tickets
API = "/api/v2/tickets/update_many.json?ids=" + arrayTicketIDs;
myURL = encodeURI(API);
var settings = {
type: 'PUT',
url: myURL,
autoRetry: false,
dataType: 'json',
data:{
"ticket": { "status": "closed" }
}
};
ret = "retry";
let W = 0; // Exponential backoff strategy -- for 429 error, we want to wait 2^W seconds, where W increases by 1 for each 429 error
let retryAfter = 0;
while(ret = "retry"){
ret = await client.request(settings).then(
function(data) {
W = 0; // reset wait time
var json = getJSONInfo2(data);
return json.job_status.url;
},
async function(response) {
if (response.status === 429) {
retryAfter = 1000 * Math.pow(2, W);
console.log("429 error closing tickets. Waiting... ",retryAfter)
W = W + 1; //exponential increase in wait time.
await sleep(retryAfter)
return "retry"
}
else
{
showError(response);
console.log("retry")
return "retry"
}
},
);
}
return ret;
}