<commit_before>'use strict';

var _ = require('lodash'),
    async = require('async'),
    cloneDeep = require('clone'),
    config = require('./config'),
    ConfigVersion = require('./models/config_version'),
    DnsResolver = require('./config_reloader/dns_resolver').DnsResolver,
    events = require('events'),
    exec = require('child_process').exec,
    fs = require('fs'),
    handlebars = require('handlebars'),
    logger = require('./logger'),
    mongoConnect = require('./mongo_connect'),
    path = require('path'),
    traverse = require('traverse'),
    util = require('util');

var ConfigReloader = function() {
  this.initialize.apply(this, arguments);
};

util.inherits(ConfigReloader, events.EventEmitter);
_.extend(ConfigReloader.prototype, {
  initialize: function(readyCallback) {
    var templatePath = path.resolve(__dirname, '../templates/nginx.hbs');
    var templateContent = fs.readFileSync(templatePath);
    this.nginxTemplate = handlebars.compile(templateContent.toString());

    if(readyCallback) {
      this.once('reloaded', readyCallback);
    }

    async.parallel([
      this.connectMongo.bind(this),
      this.setupDnsResolver.bind(this),
    ], this.handleStartup.bind(this));
  },

  connectMongo: function(asyncReadyCallback) {
    mongoConnect(asyncReadyCallback);
  },

  setupDnsResolver: function(asyncReadyCallback) {
    this.resolver = new DnsResolver(this, asyncReadyCallback);
  },

  handleStartup: function(error) {
    if(error) {
      logger.error('Config reloader startup error: ', error);
      process.exit(1);
      return false;
    }

    // Force a reload on initial boot (in case internal changes have been made
    // that would affect the output even if the configuration values haven't
    // changed).
    this.reload({ force: true });
  },

  reload: function(options) {
    ConfigVersion
      .find()
      .sort({ version: -1 })
      .limit(1)
      .exec(this.handleFetchConfigVersion.bind(this, options));
  },

  handleFetchConfigVersion: function(options, error, configVersions) {
    if(configVersions) {
      this.lastConfig = configVersions[0];
    }

    this.apis = [];
    if(config.getOriginal('apis')) {
      this.apis.concat(config.getOriginal('apis'));
    }

    if(this.lastConfig && this.lastConfig.config.apis) {
      this.apis = this.lastConfig.config.apis;
    }

    var lastId = null;
    if(this.lastConfig && this.lastConfig._id) {
      lastId = this.lastConfig._id.toHexString();
    }

    var activeId = config.get('version_id');

    if((options && options.force) || (this.lastConfig && lastId !== activeId)) {
      logger.info('Writing new config...');
      this.writeConfigs();
    } else {
      this.poll();
    }
  },

  writeConfigs: function() {
    // Write nginx config's first.
    async.series([
      this.resolveHosts.bind(this),
      this.writeNginxConfig.bind(this),
      this.writeGatekeeperConfig.bind(this),
    ], this.handleWriteConfigs.bind(this));
  },
<commit_msg>Fix to combine APIs when the data comes from config files.<commit_after>'use strict';

var _ = require('lodash'),
    async = require('async'),
    cloneDeep = require('clone'),
    config = require('./config'),
    ConfigVersion = require('./models/config_version'),
    DnsResolver = require('./config_reloader/dns_resolver').DnsResolver,
    events = require('events'),
    exec = require('child_process').exec,
    fs = require('fs'),
    handlebars = require('handlebars'),
    logger = require('./logger'),
    mongoConnect = require('./mongo_connect'),
    path = require('path'),
    traverse = require('traverse'),
    util = require('util');

var ConfigReloader = function() {
  this.initialize.apply(this, arguments);
};

util.inherits(ConfigReloader, events.EventEmitter);
_.extend(ConfigReloader.prototype, {
  initialize: function(readyCallback) {
    var templatePath = path.resolve(__dirname, '../templates/nginx.hbs');
    var templateContent = fs.readFileSync(templatePath);
    this.nginxTemplate = handlebars.compile(templateContent.toString());

    if(readyCallback) {
      this.once('reloaded', readyCallback);
    }

    async.parallel([
      this.connectMongo.bind(this),
      this.setupDnsResolver.bind(this),
    ], this.handleStartup.bind(this));
  },

  connectMongo: function(asyncReadyCallback) {
    mongoConnect(asyncReadyCallback);
  },

  setupDnsResolver: function(asyncReadyCallback) {
    this.resolver = new DnsResolver(this, asyncReadyCallback);
  },

  handleStartup: function(error) {
    if(error) {
      logger.error('Config reloader startup error: ', error);
      process.exit(1);
      return false;
    }

    // Force a reload on initial boot (in case internal changes have been made
    // that would affect the output even if the configuration values haven't
    // changed).
    this.reload({ force: true });
  },

  reload: function(options) {
    ConfigVersion
      .find()
      .sort({ version: -1 })
      .limit(1)
      .exec(this.handleFetchConfigVersion.bind(this, options));
  },

  handleFetchConfigVersion: function(options, error, configVersions) {
    if(configVersions) {
      this.lastConfig = configVersions[0];
    }

    this.apis = [];
    if(config.getOriginal('apis')) {
      this.apis = this.apis.concat(config.getOriginal('apis'));
    }

    if(this.lastConfig && this.lastConfig.config.apis) {
      this.apis = this.apis.concat(this.lastConfig.config.apis);
    }

    var lastId = null;
    if(this.lastConfig && this.lastConfig._id) {
      lastId = this.lastConfig._id.toHexString();
    }

    var activeId = config.get('version_id');

    if((options && options.force) || (this.lastConfig && lastId !== activeId)) {
      logger.info('Writing new config...');
      this.writeConfigs();
    } else {
      this.poll();
    }
  },

  writeConfigs: function() {
    // Write nginx config's first.
    async.series([
      this.resolveHosts.bind(this),
      this.writeNginxConfig.bind(this),
      this.writeGatekeeperConfig.bind(this),
    ], this.handleWriteConfigs.bind(this));
  },

{'fraction_non_alphanumeric': 0.14699588477366254, 'fraction_numerical': 0.0013168724279835392, 'mean_word_length': 3.3493199713672155, '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': '5487146', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1796, 'n_words': 335}