Skip to content

Instantly share code, notes, and snippets.

@jcppman
Created April 11, 2014 07:00
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 jcppman/10445403 to your computer and use it in GitHub Desktop.
Save jcppman/10445403 to your computer and use it in GitHub Desktop.
Weekly EM
Larva.prototype.spawn = function larvaSpawn (addArgs) {
/*
* Some initialization, and arguments type checking,
* deleted for the 50 lines rule!
*/
process.nextTick(function () {
var child = spawn(command, args, {
env: env
});
// Plug a life detector to check if the larva is alive
child.stdout.on('data', function (msg) {
that.lastHeartBeat = new Date().getTime();
});
child.on('exit', function () {
that.emit('exit', arguments);
});
carrier.carry(child.stdout, function onLine (line) {
var parsed;
var event;
var data;
try {
parsed = JSON.parse(line);
event = parsed.event;
if (typeof event === 'undefined') {
event = 'info';
}
data = parsed.data;
that.emit(event, data);
} catch (err) {
that.emit('info', line);
}
});
carrier.carry(child.stderr, function onError (line) {
that.emit('error', line);
});
that.childProcess = child;
});
// Make it chainable
return that;
};
/*
* Stdout signal flow:
*
* /====> heartbeat
* stdout ===
* \====> carrier ====> larva.emit
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment