{"size": 2091, "ext": "js", "max_stars_repo_path": "runner.js", "max_stars_repo_name": "simonswain/lancaster", "max_stars_repo_stars_event_min_datetime": "2015-10-15T01:11:53.000Z", "max_stars_repo_stars_event_max_datetime": "2016-04-14T07:48:35.000Z", "max_issues_repo_path": "runner.js", "max_issues_repo_name": "simonswain/lancaster", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "runner.js", "max_forks_repo_name": "simonswain/lancaster", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 17.0, "max_line_length": 62, "alphanum_fraction": 0.5246293639}
"strict";

var fs = require('fs');
var util = require('util');
var events = require('events');
var fork = require('child_process').fork;
var pad = require('padder');

function Runner (service_f, opts, done) {

  events.EventEmitter.call(this);
  process.setMaxListeners(0);

  var self = this;

  this.debug = true;
  this.stopping = false;

  var child;

  this.start = function(done) {

    // startedCallback is called on STARTED from child process
    self.startedCallback = done;

    self.stopping = false;

    child = fork(
      __dirname + '/' + service_f,
      {silent: true}
    );
    
    child.on('exit', this.onChildExit);
    
    child.stdout.on('data', this.onStdout);
    child.stderr.on('data', this.onStderr);

    done(null, {pid: child.pid});

  };

  this.onChildExit = function(code) {
    if (self.debug && code !== 0) {
      console.log(
        pad.timestamp(),
        'EXITED  ',
        pad.space(service_f, 20),
        code
      );
    }

    child.removeAllListeners();
    child = null;

    if (self.stoppedCallback) {
      return self.stoppedCallback();
    }
    
    // auto respawn

    // if (!self.stopping) {
    //   self.start();
    // }

  };

  this.onStdout = function(data){

    if (!self.debug) {
      return;
    }

    var s = data.toString().trim();

    if ( s === '') {
      return;
    }

    var x = s.split("\n");

    if (x.length === 0) {
      return;
    }

    var t = pad.timestamp();
    var log = [];
    for ( var i=0, ii = x.length; i < ii ; i++) {
      
      console.log(
        t,
        'STDOUT  ',
        pad.space(service_f, 20),
        x[i]);

      log.push(t + ' ' + x[i], "\n");

    }

    // if ( self.logFile && log.length > 0 ) {
    //   fs.appendFile(def.log, log.join(''));
    // }

  };

  this.onStderr = function(data){
    if ( self.debug ) {
      console.log(
        pad.timestamp(),
        'STDERR  ',
        pad.space(service_f, 20),
        data.toString().trim()
      );
    }
  };

  this.start(done);

}

util.inherits(Runner, events.EventEmitter);

module.exports = Runner;
