Wenn Sie die Lösung von Kinesis entkoppeln wollen, können Sie das folgende Konstrukt verwenden können, cb(err, data)
unter der Annahme Ihrer Callback-Signatur:
var MY_RETRY_WAIT_TIME_IN_MILLISECONDS = 2000; // wait 2 seconds between retries
var MY_MAX_TRIES = 10; // don't try more than 10 times
function invoke(params, cb) { // this will recurse using setTimeout()
console_err('invoking lamda');
if(!params.trycount) // this is going to be incremented with each try
params.trycount = 1;
var lambda_params = {
FunctionName: 'mylambdafunction',
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: JSON.stringify(params)
};
lambda.invoke(lambda_params, function(err, obj){
if(err) {
console_err('got an error: ' + err.toString());
if(err.toString().match(/ServiceException/)) { // make whatever conditions you want here
if(params.trycount < MY_MAX_TRIES) {
params.trycount++;
console_err('Try ' + params.trycount +
' in ' + MY_RETRY_WAIT_TIME_IN_MILLISECONDS/1000 +
' seconds');
setTimeout(function() { // try again
invoke(params, cb);
}, MY_RETRY_WAIT_TIME_IN_MILLISECONDS);
} else
cb('trycount exceeded maximum');
} else
cb(err); // handle error that you don't want to retry on
} else {
console.log("Lambda call succeeded");
cb(null, obj.Payload); // handle successful call
}
});
}