Skip to content

Instantly share code, notes, and snippets.

@jdarling
Last active December 17, 2015 14:29
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 jdarling/5624526 to your computer and use it in GitHub Desktop.
Save jdarling/5624526 to your computer and use it in GitHub Desktop.
Quick and dirty process management that we used to use. Shared for the Node.js Meetup
var _children = [];
var Beaker = function(){
};
isNumeric = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
Beaker.startChild = function(fileName, callback){
var self = this;
var args = Array.prototype.slice.call(arguments);
if(self.debug) args.push('--debug');
var child = spawn('node', args);
child.processFile = fileName;
child.arguments = args;
child.on('exit', (function(aChild){
return function(code, signal){
var idx = _children.indexOf(aChild);
if(idx>-1&&_children[idx]) console.log(('Child ('+_children[idx].processFile+') '+aChild.pid+' is dying!').yellow);
else console.log(('Child ('+aChild.pid+') is dying!').yellow);
self.applog({
process: aChild.processFile,
pid: aChild.pid,
status: 'Dying',
code: code,
signal: signal
});
if(idx>-1){
_children.splice(idx, 1);
console.log('Child killed successfully!'.green);
}
};
})(child));
child.on('uncaughtException', (function(aChild){
return function(err){
self.applog({
process: aChild.processFile,
pid: aChild.pid,
status: 'Uncaught Exception',
error: err.toString()
});
};
})(child));
child.stdout.on('data', (function(aChild){
return function(data){
self.applog({
process: aChild.processFile,
pid: aChild.pid,
status: 'STDOUT',
data: data.toString()
});
};
})(child));
child.stderr.on('data', (function(aChild){
return function(data){
self.applog({
process: aChild.processFile,
pid: aChild.pid,
status: 'STDERR',
error: data.toString()
});
};
})(child));
self.applog({
process: child.processFile,
pid: child.pid,
arguments: child.arguments,
status: 'Starting'
});
_children.push(child);
if(typeof(callback)=='function') callback(child);
};
Beaker.restartChild = function(childName){
var self = this;
for(var idx in _children){
if(_children[idx].processFile.match(childName)){
var child = _children[idx];
var processName = child.processFile;
child.kill();
setTimeout((function(processName){
return function(){
self.startChild(processName);
};
})(processName), 500);
}
}
};
Beaker.killChild = function(details){
var self = this;
var killChild = function(idx){
if(_children[idx]){
console.log(('Killing ('+_children[idx].processFile+'): '+_children[idx].pid).yellow);
self.applog({
process: _children[idx].processFile,
pid: _children[idx].pid,
status: 'Killing'
});
var child = _children[idx];
_children.splice(idx, 1)
child.kill();
}
};
if(isNumeric(details)) killChild(details);
else{
var l = _children.length-1;
while(l>0){
if(_children[l].processFile.match(details)) killChild(l);
l--;
}
}
};
Beaker.childProcesses = function(options){
options = options || {};
if(options.filter){
var filter = new RegExp(options.filter);
filtered = [];
for(var i in _children){
if(_children[i].processFile.match(filter)) filtered.push(_children[i]);
}
return filtered;
}
return _children;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment