<commit_before>/**
 * Created by nuintun on 2015/12/3.
 */

'use strict';

var cluster = require('cluster');

var ipc = require('ipc-main');

// cache
var workers = {};

/**
 * killWorker
 * @param name
 */
function killWorker(name){
  var worker = workers[name];

  if (worker && !worker.isDead()) {
    worker.kill('SIGTERM');

    delete workers[name];
  }
}

module.exports = {
  start: function (){
    ipc.on('emulator', function (event, project, action){
      var key = project.name + '-' + project.command.name;
      var worker = workers[key];

      switch (action) {
        case 'start':
          if (!worker) {
            var env = {};

            Object.keys(process.env).forEach(function (key){
              env[key] = process.env[key];
            });

            project.env.forEach(function (item){
              env[item.name] = item.value;
            });

            worker = cluster.fork(env);

            worker.on('message', function (message){
              event.sender.send('emulator', message.event, message.project, message.data);
            });

            worker.on('error', function (){
              killWorker(project.name);
            });

            delete project.env;

            worker.send(project);

            workers[project.name] = worker;
          }
          break;
        case 'stop':
          killWorker(project.name);
          break;
      }
    });
  }
};<commit_msg>update files<commit_after>/**
 * Created by nuintun on 2015/12/3.
 */

'use strict';

var cluster = require('cluster');

var ipc = require('ipc-main');

// cache
var workers = {};

/**
 * killWorker
 * @param name
 */
function killWorker(name){
  var worker = workers[name];

  if (worker && !worker.isDead()) {
    worker.kill('SIGTERM');

    delete workers[name];
  }
}

module.exports = {
  start: function (){
    ipc.on('emulator', function (event, project, action){
      var key = project.name + '-' + project.command.name;
      var worker = workers[key];

      switch (action) {
        case 'start':
          if (!worker || worker.isDead()) {
            var env = {};

            Object.keys(process.env).forEach(function (key){
              env[key] = process.env[key];
            });

            project.env.forEach(function (item){
              env[item.name] = item.value;
            });

            worker = cluster.fork(env);

            worker.on('message', function (message){
              event.sender.send('emulator', message.event, message.project, message.data);
            });

            worker.on('error', function (){
              killWorker(project.name);
            });

            delete project.env;

            worker.send(project);

            workers[project.name] = worker;
          } else {
            delete project.env;

            worker.send(project);
          }
          break;
        case 'stop':
          killWorker(project.name);
          break;
      }
    });
  }
};
{'fraction_non_alphanumeric': 0.152320107599193, 'fraction_numerical': 0.004707464694014795, 'mean_word_length': 1.7648698884758365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3772210', 'n_tokens_mistral': 874, 'n_tokens_neox': 838, 'n_words': 174}