<commit_before>'use strict';

var assert = require('chai').assert;
var Stopwatch = require('statman-stopwatch');
var E = require('linq');

// 
// A higher level manager for tasks. Runs tasks and coordinates error handling and logging.
//

var JobRunner = function (taskRunner, log, callbacks) {

	var self = this;

    assert.isObject(taskRunner);
    assert.isFunction(log.info);
    assert.isObject(callbacks);
    if (callbacks.unhandledExceptionCallback) {
        assert.isFunction(callbacks.unhandledException);
    }

    //
    // Add a task.
    //
	self.addTask = function (task) {

		assert.isObject(task);

        taskrunner.addTask(task);
	};

	//
	// Get a task by name, throws exception if task doesn't exist.
	//
	self.getTask = function (taskName) {

		assert.isString(taskName);

        return taskRunner.getTask(taskName);
	};

	//
	// Run a named task with a particular config.
	//
	self.runTask = function (taskName, config, configOverride) {

		assert.isString(taskName);
		assert.isObject(config);
        assert.isObject(configOverride);

        var stopwatch = new Stopwatch();
        stopwatch.start();

        if (callbacks.taskStarted) {
            callbacks.taskStarted({
                name: taskName 
            });
        }

        var uncaughtExceptionCount = 0;
        var uncaughtExceptionHandler = function (err) {
            ++uncaughtExceptionCount;

            if (callbacks.unhandledException) {
                callbacks.unhandledException(err);
            }
            else {
                log.error("Unhandled exception occurred.");
                log.error(err.message);
                log.info(err.stack);
            }            
        };

        process.on('uncaughtException', uncaughtExceptionHandler);

        return taskRunner.runTask(taskName, config, configOverride)
            .then(function () {
                stopwatch.stop();

                if (callbacks.taskSuccess) {
                    var elapsedTimeMins = stopwatch.read()/1000.0/60.0; 
                    callbacks.taskSuccess({ name: taskName }, elapsedTimeMins);
                }
            })
            .catch(function (err) {
                stopwatch.stop();

                if (callbacks.taskFailure) {
                    var elapsedTimeMins = stopwatch.read()/1000.0/60.0; 
                    callbacks.taskFailure({ name: taskName }, elapsedTimeMins, err);
                } 

                process.removeListener('uncaughtException', uncaughtExceptionHandler);
                throw err;
            })<commit_msg>Small modification to error reporting.<commit_after>'use strict';

var assert = require('chai').assert;
var Stopwatch = require('statman-stopwatch');
var E = require('linq');

// 
// A higher level manager for tasks. Runs tasks and coordinates error handling and logging.
//

var JobRunner = function (taskRunner, log, callbacks) {

	var self = this;

    assert.isObject(taskRunner);
    assert.isFunction(log.info);
    assert.isObject(callbacks);
    if (callbacks.unhandledExceptionCallback) {
        assert.isFunction(callbacks.unhandledException);
    }

    //
    // Add a task.
    //
	self.addTask = function (task) {

		assert.isObject(task);

        taskrunner.addTask(task);
	};

	//
	// Get a task by name, throws exception if task doesn't exist.
	//
	self.getTask = function (taskName) {

		assert.isString(taskName);

        return taskRunner.getTask(taskName);
	};

	//
	// Run a named task with a particular config.
	//
	self.runTask = function (taskName, config, configOverride) {

		assert.isString(taskName);
		assert.isObject(config);
        assert.isObject(configOverride);

        var stopwatch = new Stopwatch();
        stopwatch.start();

        if (callbacks.taskStarted) {
            callbacks.taskStarted({
                name: taskName 
            });
        }

        var uncaughtExceptionCount = 0;
        var uncaughtExceptionHandler = function (err) {
            ++uncaughtExceptionCount;

            if (callbacks.unhandledException) {
                callbacks.unhandledException(err);
            }
            else {
                log.error("Unhandled exception occurred.");
                log.error(err && err.stack || err);
            }            
        };

        process.on('uncaughtException', uncaughtExceptionHandler);

        return taskRunner.runTask(taskName, config, configOverride)
            .then(function () {
                stopwatch.stop();

                if (callbacks.taskSuccess) {
                    var elapsedTimeMins = stopwatch.read()/1000.0/60.0; 
                    callbacks.taskSuccess({ name: taskName }, elapsedTimeMins);
                }
            })
            .catch(function (err) {
                stopwatch.stop();

                if (callbacks.taskFailure) {
                    var elapsedTimeMins = stopwatch.read()/1000.0/60.0; 
                    callbacks.taskFailure({ name: taskName }, elapsedTimeMins, err);
                } 

                process.removeListener('uncaughtException', uncaughtExceptionHandler);
                throw err;
            })
{'fraction_non_alphanumeric': 0.10986024844720498, 'fraction_numerical': 0.006599378881987578, 'mean_word_length': 1.9129451667608819, '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': '3569839', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1364, 'n_words': 289}