<commit_before>/**
 * Created by elad.benedict on 1/28/2016.
 */

var usage = require('usage');
var pid = process.pid;
var Q = require('q');
var logger = require('./logger/logger')(module);
var config = require('../common/Configuration');
var log4js = require('log4js');

var lookupP = Q.nbind(usage.lookup, usage);
module.exports = (function(){

    var highCpuThreshold = config.get('highCpuHandlingParams:highCpuThreshold');
    var allowedConsecutiveHighCpuUsageSamples = config.get('highCpuHandlingParams:allowedConsecutiveHighCpuUsageSamples');
    var cpuSamplingInterval = config.get('highCpuHandlingParams:cpuSamplingInterval');
    var options = { keepHistory: true }

    var numOfConsecutiveHighCpuSamples = 0;

    var sample = function() {
        lookupP(pid, options)
            .then(function(usageStats){
                if (usageStats.cpu > highCpuThreshold)
                {
                    logger.error('high CPU usage: ' + usageStats.cpu);
                    numOfConsecutiveHighCpuSamples++;
                }
                else
                {
                    if (numOfConsecutiveHighCpuSamples > 0)
                    {
                        logger.debug('CPU usage is below highCpuThreshold - clearing counter');
                    }

                    numOfConsecutiveHighCpuSamples = 0;
                }

                if (numOfConsecutiveHighCpuSamples === allowedConsecutiveHighCpuUsageSamples)
                {
                    logger.error('CPU usage was too high during the last ' + allowedConsecutiveHighCpuUsageSamples + ' samples - exiting the application');
                }
            })
            .then(function(){
                log4js.shutdown(function() { process.exit(1); });
            })
            .catch(function(err){
                logger.error('Error sampling the process CPU usage: ' + err);
            });
    }

    var start = function(){
        setInterval(sample, cpuSamplingInterval)
    }

    return {
        start : start
    }
})()<commit_msg>exit after logs are written<commit_after>/**
 * Created by elad.benedict on 1/28/2016.
 */

var usage = require('usage');
var pid = process.pid;
var Q = require('q');
var logger = require('./logger/logger')(module);
var config = require('../common/Configuration');
var log4js = require('log4js');

var lookupP = Q.nbind(usage.lookup, usage);
module.exports = (function(){

    var highCpuThreshold = config.get('highCpuHandlingParams:highCpuThreshold');
    var allowedConsecutiveHighCpuUsageSamples = config.get('highCpuHandlingParams:allowedConsecutiveHighCpuUsageSamples');
    var cpuSamplingInterval = config.get('highCpuHandlingParams:cpuSamplingInterval');
    var options = { keepHistory: true }

    var numOfConsecutiveHighCpuSamples = 0;

    var sample = function() {
        lookupP(pid, options)
            .then(function(usageStats){
                if (usageStats.cpu > highCpuThreshold)
                {
                    logger.error('high CPU usage: ' + usageStats.cpu);
                    numOfConsecutiveHighCpuSamples++;
                }
                else
                {
                    if (numOfConsecutiveHighCpuSamples > 0)
                    {
                        logger.debug('CPU usage is below highCpuThreshold - clearing counter');
                    }

                    numOfConsecutiveHighCpuSamples = 0;
                }

                if (numOfConsecutiveHighCpuSamples === allowedConsecutiveHighCpuUsageSamples)
                {
                    logger.error('CPU usage was too high during the last ' + allowedConsecutiveHighCpuUsageSamples + ' samples - exiting the application');
                    log4js.shutdown(function() { process.exit(1); });
                }
            })
            .catch(function(err){
                logger.error('Error sampling the process CPU usage: ' + err);
            });
    }

    var start = function(){
        setInterval(sample, cpuSamplingInterval)
    }

    return {
        start : start
    }
})()
{'fraction_non_alphanumeric': 0.10421286031042129, 'fraction_numerical': 0.006898250800689825, 'mean_word_length': 2.0074074074074075, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5495917', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1044, 'n_words': 219}