In order to capture the health of each node in multiple clusters I need to invoke them through http calls inside for loop but loop ends before the status comes back from servers failing the program to work correctly.
module.exports = function(robot) {
robot.respond(/nonprodoauthhealthcheck/i, function(msg){
var healthcheck=[];
var nodes = [{
"cluster": ["https://testurl1.com", "https://testurl2.com"],
"client_id": "clientid1",
"client_secret": "password1"
},
{
"cluster": ["https://testur3.com", "https://testurl4"],
"client_id": "clientid2",
"client_secret": "password2"
}
];
for(let i=0;i<nodes.length;i++){
console.log("i="+i)
client_id=nodes[i].client_id;
client_secret= nodes[i].client_secret;
clusteritem=nodes[i].cluster;
for(let j=0;j<clusteritem.length;j++){
var data="client_id="+client_id+"&client_secret="+client_secret+"&grant_type=client_credentials";
var queryJobUrls=clusteritem[j];
console.log("j="+j)
getJobUrls(queryJobUrls,data)
.then(function(response){
//msg.reply(response)
healthcheck.push(
{
"url":clusteritem[j],
"status":response
}
)
})
.catch(function(error){
//msg.reply(error)
healthcheck.push(
{
"url":clusteritem[j],
"status":error
}
)
})
};
}
//for(let k=0;k<healthcheck.length;k++){}
msg.reply(healthcheck);
});
function getJobUrls(queryJobUrls,data) {
return new Promise(function(resolve, reject){
var options = {
rejectUnauthorized: false
}
robot.http(queryJobUrls, options).header('Content-Type', 'application/x-www-form-urlencoded').post(data)(function(err, response, body) {
console.log("inside oauth call")
if (err != null){
reject(err);
}
else {
resolve(body);
}
});
})
}
}
I expect the code to populate a healthcheck array and look something like this:
healthcheck = [{"url":"http://testurl1.com",status="response from http call"},{"url":"http://testurl2.com",status="response from http call"},{"url":"http://testurl3.com",status="response from http call"},{"url":"http://testur4.com",status="response from http call"}]