I'm writing Cloud Functions very first time in NodeJS and that's working very well, but whenever I tried to deploy I got a warning like this:
warning Avoid nesting promises promise/no-nesting (line 9)
I tried to get rid of this, but there are some issues which I need to resolve:
exports.updateUserCount = functions.pubsub.schedule('every 2 minutes').onRun(context => {
return admin.database().ref('/servers').once('value').then(snapshot =>{
var ips = {}
snapshot.val().forEach( (server, i) => { ips[server.label] = server.ipv4[0] })
var promises = snapshot.val().map( (server, index) => {
let sftp = new Client()
return Promise.all([sftp, server.label, index, sftp.connect({ host : server.ipv4[0], port : '22', username : 'user_name', password : '*********' })])
.then( values => {
return Promise.all([values[0], values[0].get('/etc/openvpn/server/openvpn-status.log'), values[1], values[2]])
}).then( data => {
data[0].end()
if (data[1]) {
let count = countWords(data[1].toString(),"client_list") - 1
console.log(count, `users connected now with ${data[2]} ${data[3]}`)
return Promise.all([ count, admin.database().ref(snapshot.child(index.toString()).ref).update({ userCount : count }) ])
}
console.log('file not found')
return false
}).catch(err => {
console.log(err);
});
})
console.log(`array ${JSON.stringify(ips)}`)
return Promise.all(promises)
}).catch(err => {
console.log(err);
});
});
If I try to put other code outside of nesting I can't get instance of sftp
and it can't be declared globally because each sftp
is connected to and having configs of different servers.
Any help will be appreciated and thanks is advance.