<commit_before>
/*!
 * Engine - Worker
 * Copyright(c) 2010 LearnBoost <<EMAIL>>
 * MIT Licensed
 */

/**
 * Module dependencies.
 */

var EventEmitter = require('events').EventEmitter
  , spawn = require('child_process').spawn
  , binding = process.binding('net')
  , net = require('net');

/**
 * Node binary.
 */

var node = process.execPath

/**
 * Initialize a new `Worker` with the given `master`.
 *
 * @param {Master} master
 * @api private
 */

var Worker = module.exports = function Worker(master) {
  this.master = master;
  this.server = master.server;
};

/**
 * Inherit from `EventEmitter.prototype`.
 */

Worker.prototype.__proto__ = EventEmitter.prototype;

/**
 * Worker is a receiver.
 */

require('./mixins/receiver')(Worker.prototype);

/**
 * Start worker.
 *
 * @api private
 */

Worker.prototype.start = function(){
  var self = this;

  // stdin
  this.stdin = new net.Socket(0, 'unix');
  this.stdin.setEncoding('ascii');
  this.stdin.on('fd', this.server.listenFD.bind(this.server));
  this.stdin.on('data', function(chunk){
    self.frameCommand(self.master.sock, chunk);
  });
  this.stdin.resume();

  // signal handlers
  process.on('SIGINT', this.kill.bind(this));
  process.on('SIGTERM', this.kill.bind(this));
  process.on('SIGHUP', this.shutdown.bind(this));
  process.on('SIGQUIT', this.shutdown.bind(this));
  process.on('uncaughtException', this.kill.bind(this));

  // ping master
  setInterval(this.ping.bind(this), 20000); // TODO: options
};

/**
 * Immediate shutdown.
 *
 * @api private
 */

Worker.prototype.kill = function(){
  this.send('workerKilled');
  process.exit();
};

/**
 * Perform graceful shutdown.
 *
 * @api private
 */

Worker.prototype.shutdown = function(){
  var server = this.server;
  if (server.connections) {
    server.watcher.stop();
    setInterval(function(self){
      server.connections || self.kill();<commit_msg>output to stderr before killing<commit_after>
/*!
 * Engine - Worker
 * Copyright(c) 2010 LearnBoost <<EMAIL>>
 * MIT Licensed
 */

/**
 * Module dependencies.
 */

var EventEmitter = require('events').EventEmitter
  , spawn = require('child_process').spawn
  , binding = process.binding('net')
  , net = require('net');

/**
 * Node binary.
 */

var node = process.execPath

/**
 * Initialize a new `Worker` with the given `master`.
 *
 * @param {Master} master
 * @api private
 */

var Worker = module.exports = function Worker(master) {
  this.master = master;
  this.server = master.server;
};

/**
 * Inherit from `EventEmitter.prototype`.
 */

Worker.prototype.__proto__ = EventEmitter.prototype;

/**
 * Worker is a receiver.
 */

require('./mixins/receiver')(Worker.prototype);

/**
 * Start worker.
 *
 * @api private
 */

Worker.prototype.start = function(){
  var self = this;

  // stdin
  this.stdin = new net.Socket(0, 'unix');
  this.stdin.setEncoding('ascii');
  this.stdin.on('fd', this.server.listenFD.bind(this.server));
  this.stdin.on('data', function(chunk){
    self.frameCommand(self.master.sock, chunk);
  });
  this.stdin.resume();

  // signal handlers
  process.on('SIGINT', this.kill.bind(this));
  process.on('SIGTERM', this.kill.bind(this));
  process.on('SIGHUP', this.shutdown.bind(this));
  process.on('SIGQUIT', this.shutdown.bind(this));
  process.on('uncaughtException', function(err){
    console.error(err.stack || err.message);
    self.kill();
  });

  // ping master
  setInterval(this.ping.bind(this), 20000); // TODO: options
};

/**
 * Immediate shutdown.
 *
 * @api private
 */

Worker.prototype.kill = function(){
  this.send('workerKilled');
  process.exit();
};

/**
 * Perform graceful shutdown.
 *
 * @api private
 */

Worker.prototype.shutdown = function(){
  var server = this.server;
  if (server.connections) {
    server.watcher.stop();
    setInterval(function(self){
      server.connections || self.kill();
{'fraction_non_alphanumeric': 0.17964693665628245, 'fraction_numerical': 0.005192107995846314, 'mean_word_length': 3.6421686746987953, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5344019', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1274, 'n_words': 248}