Skip to content

Instantly share code, notes, and snippets.

@erikhazzard
Created September 29, 2015 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikhazzard/84dca1dae510c671a537 to your computer and use it in GitHub Desktop.
Save erikhazzard/84dca1dae510c671a537 to your computer and use it in GitHub Desktop.
Scraper process callback examples

Run each script and kill it (control + c). When scraper is required, we cannot hook into process.on callbacks

/**
* If scraper is not included, callback gets called
*/
process.on('SIGINT', function(){
console.log('Hello I get called');
process.exit(1);
});
setTimeout(function(){}, 19000); // keep alive
/**
* If scraper is included before process.on calls, the callbacks never get called
*/
var scraperjs = require('scraperjs');
process.on('SIGINT', function(){
console.log('Hello I never get called');
process.exit(1);
});
setTimeout(function(){}, 19000); // keep alive
/**
* If scraper is not included, callbacks are all called
*/
function close() {
console.log('Received SIGINT...emitting cleanup\n');
process.emit('cleanup');
setTimeout(function() {
console.log('>>>>>>>>>> Exiting \n');
process.exit(1);
}, 1000);
}
process.on('SIGINT', close);
process.on('cleanup', function(){
console.log("<<< CLEANING >>>");
setTimeout(function(){
console.log('<<< TIMEOUT CALLED >>>');
}, 100);
});
setTimeout(function(){}, 19000); // keep alive
/**
* If scraperjs is included after everything is set up, the timeout in process.on('cleanup')
* is never called.
*/
function close() {
console.log('Received SIGINT...emitting cleanup\n');
process.emit('cleanup');
setTimeout(function() {
console.log('>>>>>>>>>> Exiting \n');
process.exit(1);
}, 1000);
}
process.on('SIGINT', close);
process.on('cleanup', function(){
console.log("<<< CLEANING >>>");
setTimeout(function(){
console.log('<<< THIS IS NEVER CALLED >>>');
}, 100);
});
setTimeout(function(){}, 19000); // keep alive
// If we import it down here, the initial process signal is caught, but timeout
// callbacks never get called
var scraperjs = require('scraperjs');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment