puppet-master.js
function exit() {
// check for and do something with instances of Error
// passed as arguments i/e if arguments.length > 1 ...
// Array.from(arguments).map...
if (process.env.CHILD_PID) {
childProcess.execFile(`kill -9 ${process.env.CHILD_PID}`);
}
printResults();
process.removeListener('exit', exit);
process.exit();
}
process.on('SIGINT', exit);
process.on('SIGTERM', exit);
process.on('exit', exit);
function getTestResults(file, next) {
redis.hget('test_results', file, function afterRedisHget(error, results) {
if (error) {
return exit(error);
}
const parsedResults = JSON.parse(results);
// do something with parsedResults;
next();
});
}
(function orchestrate() {
if (filenames.length < 1) {
return exit();
}
const filename = filenames.shift();
const child = childProcess.fork(PATH_TO_SLAVE_FILE, [filename, 'NODE_ENV=test']);
const pid = childProcess.pid;
process.env.CHILD_PID = pid;
child.on('close', () => {
filesCompleted.push(filename);
getTestResults(filename, orchestrate);
});
})();
puppet-slave.js
const args = process.argv.slice(2);
const filename = args.shift();
function exit() {
// here we kill various API process pids
process.removeListener('exit', exit);
process.exit();
}
const file = require(`${process.cwd()}/path/to/dir/${filename}`);
file.vows(options).run({ reporter }, function afterTestRun(results) {
redis.hset('test_results', filename, JSON.stringify(results), function afterRedis() {
return exit();
});
});