Skip to content

Instantly share code, notes, and snippets.

@andygup
Created September 10, 2013 15:54
Show Gist options
  • Save andygup/6511504 to your computer and use it in GitHub Desktop.
Save andygup/6511504 to your computer and use it in GitHub Desktop.
Node background timer that runs in a child process.
/**
* Timer that runs in a node child process.
* Can be used to control multiple things in the main application.
* @type {*}
*/
var timers = require("timers");
var ___backgroundTimer;
/**
* @param msg Object
*/
process.on('message',function(msg){
if(msg.start == true){
var count = 0;
___backgroundTimer = timers.setInterval(function(){
try{
var date = new Date();
console.log("timer.js: datetime tick: " + date.toUTCString());
process.send("tick");
}
catch(err){
count++;
if(count == 3){
console.log("timer.js: shutdown timer...too many errors. " + err.message);
clearInterval(___backgroundTimer);
process.disconnect();
}
else{
console.log("timer.js: " + err.message + "\n" + err.stack);
}
}
},msg.interval);
}
})
//process.on('exit',function(){
// console.log("Shutting down timer.js");
// clearInterval(___backgroundTimer);
// process.disconnect();
//})
process.on('uncaughtException',function(err){
console.log("timer.js: " + err.message + "\n" + err.stack + "\n Stopping background timer");
clearInterval(___backgroundTimer);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment