Setting up delay for retrying backend

Not applicable
How to setup a delay for retrying backend service in APIGEE? I tried using setTimeout in javascript as below,
setTimeout(function() {
  context.setVariable("response.content",backend.getResponse().status);
}, 2000);
But its throwing setTimeout undefined error.
Solved Solved
1 7 1,420
2 ACCEPTED SOLUTIONS

The snippet you are showing would't help retrying the backend anyways. For such usecases, I would recommend using nodejs - it should be pretty straightforward to implement such orchestration in node Thanks, PS: 'setTimeout' isn't available for the JS policy,

View solution in original post

Not applicable
Yes. As suggested by @Mukundha Madhavan, it is recommended to leverage Node.js to handle replays. I've tried in the past on a local Node.js app request-replay Node.js package https://www.npmjs.com/package/request-replay. As per the package example, the benefit is that you continue to leverage request module wrapped by the replay module.
var fs = require('fs');
var request = require('request');
var replay = require('request-replay');
 
// Note that the options argument is optional 
// Accepts the same options the retry module does and an additional 
// errorCodes array that default to ['EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT'] 
replay(request('http://google.com/doodle.png', function (err, response, body) {
    // Do things 
}), {
    retries: 10,
    factor: 3
})
.on('replay', function (replay) {
    // "replay" is an object that contains some useful information 
    console.log('request failed: ' + replay.error.code + ' ' + replay.error.message);
    console.log('replay nr: #' + replay.number);
    console.log('will retry in: ' + replay.delay + 'ms')
})
 

View solution in original post

7 REPLIES 7

APIs are typically accessed in a synchronous fashion. i.e a request from the client hits the backend and comes back to the client. A retry in Apigee will mean that we will timeout at the client end before we complete the retries. Worth revisiting the use case to see if polling can be moved to the client, or a server push or a web hook can solve the case.

Thanks for your response. But this is Asynchronous API call.And we want to retry the backend thrice with a delay of 30seconds if there is communication failure at backend. As it is Asynchronous call,caller is not blocked for the response. The response is returned to callback address finally. setTimeout function is common in javascript.But it seems to be not working in APIGEE javascript code.Is there any other alternate method to do this or is there any issue with our setTimeout code given in my aforementioned post?

@APIGEE team,Kindly let me know whether APIGEE supports delay.

The snippet you are showing would't help retrying the backend anyways. For such usecases, I would recommend using nodejs - it should be pretty straightforward to implement such orchestration in node Thanks, PS: 'setTimeout' isn't available for the JS policy,

Thanks Mukundha.The code snippet I have shown just tries the delay.We thought of taking one step at a time ,so after delay works thought of doing retry. Anyhow,as you mentioned setTimeout doesnt work here,we will try with nodejs.

Not applicable
Yes. As suggested by @Mukundha Madhavan, it is recommended to leverage Node.js to handle replays. I've tried in the past on a local Node.js app request-replay Node.js package https://www.npmjs.com/package/request-replay. As per the package example, the benefit is that you continue to leverage request module wrapped by the replay module.
var fs = require('fs');
var request = require('request');
var replay = require('request-replay');
 
// Note that the options argument is optional 
// Accepts the same options the retry module does and an additional 
// errorCodes array that default to ['EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT'] 
replay(request('http://google.com/doodle.png', function (err, response, body) {
    // Do things 
}), {
    retries: 10,
    factor: 3
})
.on('replay', function (replay) {
    // "replay" is an object that contains some useful information 
    console.log('request failed: ' + replay.error.code + ' ' + replay.error.message);
    console.log('replay nr: #' + replay.number);
    console.log('will retry in: ' + replay.delay + 'ms')
})
 

Thanks @Diego Zuluaga.We will try out this option.