<commit_before>var MultiserverClient = require('../gearman/multiserver-client').MultiserverClient;
var MultiserverWorker = require('../gearman/multiserver-worker').MultiserverWorker;

function initialize(conf) {
  var p = new Passthrough(
      new MultiserverClient(conf.servers));
  p._worker = new MultiserverWorker(
      conf.servers,
      'passthroughController',
      p.workHandler.bind(p));
}

function Passthrough(client, worker) {
  if(worker)
    this._worker = worker(this.workHandler);
  this._client = client;
}

Passthrough.prototype._runTask = function(task) {
  var _this = this;
  this._client.submitJob(task.func_name, task.payload)
    .on('complete', function() {
      _this._client.submitJob('delayedJobDone', JSON.stringify(task)); 
    })
    .on('fail', function() {
    });
};

Passthrough.prototype.workHandler = function(payload, worker) {
  var _this = this;
  worker.complete(); // calling complete already so that the runner
                     // doesn't potentially offer work to other controllers

  var task = JSON.parse(payload.toString());
  _this._runTask(task);
};

module.exports = initialize;
module.exports.Passthrough = Passthrough;
<commit_msg>Emit 'connect' from passthrough controller<commit_after>var util = require('util');
var events = require('events');
var MultiserverClient = require('../gearman/multiserver-client')
  .MultiserverClient;
var MultiserverWorker = require('../gearman/multiserver-worker')
  .MultiserverWorker;
var logger = require('../log');

/**
 * Passthrough component. Emits 'connect' when at least one server for both
 * worker and client roles are connected to.
 */

function Passthrough(conf) {
  events.EventEmitter.call(this);
  var that = this;
  this._client = new MultiserverClient(conf.servers);
  this._worker = new MultiserverWorker(conf.servers, 'passthroughController',
    function (payload, worker) {
      var task = JSON.parse(payload.toString());
      that.workHandler(task, worker);
    });
  this._client.on('connect', function() {
    if (that._worker.connected)
      that._emitConnected();
  });
  this._worker.on('connect', function() {
    if (that._client.connected)
      that._emitConnected();
  });
}

util.inherits(Passthrough, events.EventEmitter);

Passthrough.prototype._emitConnected = function() {
  logger.info('controller:', 'connected');
  this.emit('connect');
}

Passthrough.prototype._runTask = function(task) {
  var that = this;
  this._client.submitJob(task.func_name, task.payload)
    .on('complete', function() {
      that._client.submitJob('delayedJobDone', JSON.stringify(task));
    })
    .on('fail', function() {
    });
};

Passthrough.prototype.workHandler = function(task, worker) {
  worker.complete(); // calling complete already so that the runner
                     // doesn't potentially offer work to other controllers
  this._runTask(task);
};

module.exports = function(conf) {
  return new Passthrough(conf);
};
module.exports.Passthrough = Passthrough;

{'fraction_non_alphanumeric': 0.13875917278185457, 'fraction_numerical': 0.0, 'mean_word_length': 4.083050847457627, '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': '6862887', 'n_tokens_mistral': 928, 'n_tokens_neox': 914, 'n_words': 186}