Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Quick and dirty process management that we used to use. Shared for the Node.js Meetup

View gist:5624526
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
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
Something went wrong with that request. Please try again.