我真的被困在这里了。根据定义,承诺只返回一次。但是,我需要找到一种方法来获得使用cron模块的promise。以下是我尝试做的缩短版本。一切都在第一次执行得很好但在此之后承诺永远不会解决。
不幸的是,我很难使用承诺来自的节点池模块。与HANA数据库建立连接池需要此模块。
谢谢!
var pool = require('generic-pool');
var servers = require('../config/servers');
var CronJob = require('cron').CronJob;
var hdb = require('hdb'); // database driver
var factories = {};
var pools = {};
for (let i = 0; i < servers.length; i++) {
factories[servers[i].name] = {
create: function(){
return new Promise(function(resolve, reject){
var client = hdb.createClient({
host: servers[i].host,
port: servers[i].port,
user: servers[i].user,
password: servers[i].password
});
resolve(client);
});
},
destroy: function(client){
if (!client.hadError && client.readyState !== 'closed') {
client.end();
}
}
};
pools[servers[i].name] = pool.createPool(factories[servers[i].name]);
}
var job = new CronJob({
cronTime: '0 * * * *',
onTick: () => {
async.waterfall([
(callbackWF) => {
var resourcePromise = pools[name].acquire();
console.log(moment().format('YYYY-MM-DD HH:mm:ss.SS - ') + 'Attempting to resolve promise for Name: ' + name + ' Table: ' + table);
resourcePromise.then( (client) => {
console.log(moment().format('YYYY-MM-DD HH:mm:ss.SS - ') + 'Got promise for Name: ' + name + ' Table: ' + table);
if (client.readyState != 'connected') {
client.connect( (err) => {
if (err) {
console.log(moment().format('YYYY-MM-DD HH:mm:ss.SS - ') + 'Error while connecting to client. Name: ' + name + ' Host: ' + host + ' Port: ' + port + ' User: ' + user + ' Error: ' + JSON.stringify(err));
return callbackWF('error', client);
}
callbackWF(null, client);
});
} else {
callbackWF(null, client);
}
}).catch( (err) => {
if (err) {
console.log(moment().format('YYYY-MM-DD HH:mm:ss.SS - ') + 'Error getting promise for connection pool. Name: ' + name + ' Host: ' + host + ' Port: ' + port + ' User: ' + user + ' Error: ' + JSON.stringify(err));
callbackWF('error');
}
console.log(moment().format('YYYY-MM-DD HH:mm:ss.SS - ') + 'Caught promise');
});
},
// Get data for table.
(client, callbackWF) => {
client.exec(select, {rowsAsArray: true}, (err, rows) => {
if (err) {
console.log(moment().format('YYYY-MM-DD HH:mm:ss.SS - ') + 'Error while getting data from ' + name + " table: " + table + ". " + err + select);
return callbackWF(err, client);
}
callbackWF(null, client);
});
}
], (err, client) => {
if (typeof client != 'undefined') {
console.log(moment().format('YYYY-MM-DD HH:mm:ss.SS - ') + 'Releasing pool for Name: ' + name + ' Table: ' + table);
pools[name].release(client);
}
});
},
start: true,
timeZone: 'UTC'
});
答案 0 :(得分:0)
我没有看到任何错误。承诺可能只返回一次,但您可以根据需要多次调用then()
。