Skip to content

Instantly share code, notes, and snippets.

@davidpaulhunt
Last active September 12, 2016 17:09
Show Gist options
  • Save davidpaulhunt/ce3be6746e1b3a095385df318a7c4e95 to your computer and use it in GitHub Desktop.
Save davidpaulhunt/ce3be6746e1b3a095385df318a7c4e95 to your computer and use it in GitHub Desktop.

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();
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment