Created
April 11, 2014 07:00
-
-
Save jcppman/10445403 to your computer and use it in GitHub Desktop.
Weekly EM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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