coopernurse / node-pool

Generic resource pooling for node.js
2.38k stars 259 forks source link

Issues while using node-pool with cron #190

Closed ktravelet closed 7 years ago

ktravelet commented 7 years ago

Hi I'm currently running into a limitation of promises. Since promises can only be resolved 1 time, if I try to use cron with node-pool, it will work the first time but not after that. Does anyone have suggestions of how to return a promise again?

Thanks!

ktravelet commented 7 years ago

After some more testing, I was able to reduce the code for clarity. The first execution of the cron job, the promise will get resolved. Second time though the promise never gets resolved.

Thanks!

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: '* * * * *',
    onTick: () => {

        var resourcePromise = pools[name].acquire();

        console.log('Trying to resolve promise');

        resourcePromise.then( (client) => {

            console.log('Resolved promise');
           // do stuff with promise

        }).catch( (err) => {

           // do stuff with error

        });

    },
    start: true,
    timeZone: 'UTC'
});
ktravelet commented 7 years ago

never mind, I missed a return statement. Please close.

sandfox commented 7 years ago

:-)