luciotato / waitfor

Sequential programming for node.js, end of callback hell / pyramid of doom
MIT License
532 stars 29 forks source link

sigint & wait.for? #41

Closed mhassman closed 8 years ago

mhassman commented 8 years ago

Hi, Is there a way to initialize a new wait.for instance in a node.js sigint handler? Calls executing launchFiber fail within the shutdown handler. Thanks in advance!

luciotato commented 8 years ago

do you have a woking (failing) code example?

Cordiales Saludos, Lucio M. Tato Socio Gerente www.tecnogob.com

On Sun, Jan 3, 2016 at 11:41 PM, mhassman notifications@github.com wrote:

Hi, Is there a way to initialize a new waitfor instance in a nodejs sigint handler? Calls executing launchFiber fail within the shutdown handler Thanks in advance!

— Reply to this email directly or view it on GitHub https://github.com/luciotato/waitfor/issues/41.

mhassman commented 8 years ago

Apologies for delay - holidays. I put together a minimalistic example - below.. It turns out the issue only arises when using wait.for with mysql.


var mysql = require('mysql'); var wait = require('wait.for');

var mysql_info = { host: 'localhost', user: 'some-username', password: 'some-password', database: 'some-database' }; var connection = mysql.createConnection(mysql_info);

setInterval(print1, 1000);

function print1() { wait.launchFiber(print2, 'loop'); }

function print2(val) { console.log('A: ' + val + ' within fiber');

//this works when called by sigint handler (A & B are echoed to console)..
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;
  console.log('The solution is: ', rows[0].solution);
});

//this fails when called by sigint handler (A is echoed to console, B is not)..
//console.log(wait.forMethod(connection, "query", "SELECT 1 + 1 AS solution"));

console.log('B: ' + val + ' within fiber');

}

process.on('SIGINT', function() { wait.launchFiber(print2, 'sigint'); process.exit();

});

mhassman commented 8 years ago

ok.. i see what's happening.. just a matter of timing. I can delay process.exit(). Is there a wait.for queue i can reference or event emitted once the pending callbacks queue reaches zero?

luciotato commented 8 years ago

wait.launchFiber() "launchs" the fiber and returns immediatly, then process.exit()gets executed and all threads are killed.

try this:

var mysql = require('mysql');
var wait = require('wait.for');

var mysql_info = {
host: 'localhost',
user: 'some-username',
password: 'some-password',
database: 'some-database'
};
var connection = mysql.createConnection(mysql_info);

setInterval(print1, 1000);

function print1() {
wait.launchFiber(print2, 'loop');
}

function print2(val, doExit) {
console.log('A: ' + val + ' within fiber');

//this works when called by sigint handler (A & B are echoed to console)..
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;
  console.log('The solution is: ', rows[0].solution);
});

//this fails when called by sigint handler (A is echoed to console, B is not)..
//console.log(wait.forMethod(connection, "query", "SELECT 1 + 1 AS solution"));

console.log('B: ' + val + ' within fiber');

if (doExit) {process.exit();}
}

process.on('SIGINT', function() {
wait.launchFiber(print2, 'sigint', true);
});