<commit_before>var fs = require('fs');
var path = require('path');
var serviceLocator = require('./service-locator');
var CMApiClient = require('./cm-api-client');
var JanusHttpClient = require('./janus/http-client');
var Streams = require('./streams');
var Logger = require('./logger');
var CmApplication = require('./cm-application');

var HttpServer = require('./http-server');
var JanusProxy = require('./janus/proxy');
var JobManager = require('./job/manager');
var Promise = require('bluebird');

function Application(config) {
  this.config = config;
}

Application.prototype.registerServices = function() {
  serviceLocator.register('logger', function() {
    var logger = new Logger('app');
    logger
      .pipe(Logger.backends.nodeConsole.formatMinilog)
      .pipe(Logger.backends.nodeConsole);

    var logFilePath = path.resolve(path.dirname(__dirname), this.config['logPath']);
    var stream = fs.createWriteStream(logFilePath, {flags: 'a', defaultEncoding: 'utf8'});
    logger
      .pipe(stream);
    return logger;
  }.bind(this));

  serviceLocator.register('cm-api-client', function() {
    return new CMApiClient(this.config['cmApi']['baseUrl'], this.config['cmApi']['apiKey']);
  }.bind(this));

  serviceLocator.register('http-client', function() {
    return new JanusHttpClient(this.config['janus']['httpAddress']);
  }.bind(this));

  serviceLocator.register('streams', function() {
    return new Streams();
  });

  serviceLocator.register('cm-application', function() {
    return new CmApplication(config['cmApplication']['path']);
  });
};

Application.prototype.start = function() {
  var proxy = new JanusProxy(this.config['webSocketServer']['port'], this.config['janus']['webSocketAddress']);
  proxy.start();

  var httpServer = new HttpServer(this.config['httpServer']['port'], this.config['httpServer']['apiKey']);
  httpServer.start();

  var jobManager = new JobManager(this.config['jobManager']['jobsPath'], []);
  jobManager.start();

  process.on('unhandledRejection', function(reason) {
    serviceLocator.get('logger').error('Unexpected rejection error. ' + reason.stack || reason.message);
  });

  process.on('uncaughtException', function(err) {
    serviceLocator.get('logger').error('Unexpected runtime error. Shutdown the process ' + err.stack || err.message);
    process.emit('close');
  });

  process.on('SIGINT', function() {
    process.emit('close');
  });

  process.on('close', function() {
    serviceLocator.get('logger').info('Closing application');
    Promise.all([jobManager.stop(), proxy.stop()]).finally(function() {
      process.nextTick(process.exit(1));
    });
  });
};

module.exports = Application;<commit_msg>intercept SIGTERM<commit_after>var fs = require('fs');
var path = require('path');
var serviceLocator = require('./service-locator');
var CMApiClient = require('./cm-api-client');
var JanusHttpClient = require('./janus/http-client');
var Streams = require('./streams');
var Logger = require('./logger');
var CmApplication = require('./cm-application');

var HttpServer = require('./http-server');
var JanusProxy = require('./janus/proxy');
var JobManager = require('./job/manager');
var Promise = require('bluebird');

function Application(config) {
  this.config = config;
}

Application.prototype.registerServices = function() {
  serviceLocator.register('logger', function() {
    var logger = new Logger('app');
    logger
      .pipe(Logger.backends.nodeConsole.formatMinilog)
      .pipe(Logger.backends.nodeConsole);

    var logFilePath = path.resolve(path.dirname(__dirname), this.config['logPath']);
    var stream = fs.createWriteStream(logFilePath, {flags: 'a', defaultEncoding: 'utf8'});
    logger
      .pipe(stream);
    return logger;
  }.bind(this));

  serviceLocator.register('cm-api-client', function() {
    return new CMApiClient(this.config['cmApi']['baseUrl'], this.config['cmApi']['apiKey']);
  }.bind(this));

  serviceLocator.register('http-client', function() {
    return new JanusHttpClient(this.config['janus']['httpAddress']);
  }.bind(this));

  serviceLocator.register('streams', function() {
    return new Streams();
  });

  serviceLocator.register('cm-application', function() {
    return new CmApplication(config['cmApplication']['path']);
  });
};

Application.prototype.start = function() {
  var proxy = new JanusProxy(this.config['webSocketServer']['port'], this.config['janus']['webSocketAddress']);
  proxy.start();

  var httpServer = new HttpServer(this.config['httpServer']['port'], this.config['httpServer']['apiKey']);
  httpServer.start();

  var jobManager = new JobManager(this.config['jobManager']['jobsPath'], []);
  jobManager.start();

  process.on('unhandledRejection', function(reason) {
    serviceLocator.get('logger').error('Unexpected rejection error. ' + reason.stack || reason.message);
  });

  process.on('uncaughtException', function(err) {
    serviceLocator.get('logger').error('Unexpected runtime error. Shutdown the process ' + err.stack || err.message);
    process.emit('close');
  });

  process.on('SIGINT', function() {
    process.emit('close');
  });

  process.on('SIGTERM', function() {
    process.emit('close');
  });

  process.on('close', function() {
    serviceLocator.get('logger').info('Closing application');
    Promise.all([jobManager.stop(), proxy.stop()]).finally(function() {
      process.nextTick(process.exit(1));
    });
  });
};

module.exports = Application;
{'fraction_non_alphanumeric': 0.1859739306040022, 'fraction_numerical': 0.0007343491830365339, 'mean_word_length': 5.066815144766147, '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': '6146246', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1569, 'n_words': 281}