<commit_before>'use strict';

module.exports = function(config) {
    var domain = require('domain');
    var primary = domain.create();

    var logger = require('./lib/logging')(config.name);

    function errorHandler(err) {
        logger.error("Worker crashed");
        if (err.message) {
            logger.error(err.message);
        }

        if (err.stack) {
            logger.error(err.stack);
        }

        process.exit(-1);
    }

    // Domain emits 'error' when it's given an unhandled error
    primary.on('error', errorHandler);
    process.on('uncaughtException', errorHandler);

    primary.run(function() {
        var argv = require('yargs')
            .alias('c', 'configfile')
            .alias('b', 'bootstrap').argv;

        /*
         * Service configuration context
         */
        var context = {};
        context.logger = logger;
        context.configfile = argv.configfile;
        context.sysconfig = require('./lib/sysconfig')(context);
        if (! context.sysconfig) {
            throw "No system configuration. Can not continue."
        }

        context.cluster = require('cluster');

        loadModule('elasticsearch', config, context);
        loadModule('mongodb', config, context);
        loadModule('statsd', config, context);
        loadModule('redis', config, context);

        // The master shouldn't need these connections.
        if (! context.cluster.isMaster) {
            // We have to load this here so it uses the same mongoose instance
            // This is not really where this belongs.
            if (config.baucis) {
                logger.info("Loading module Baucis");
                context.baucis = require('baucis');
            }

            if (config.worker && typeof config.worker === 'function') {
                context.worker = config.worker;
            }
            else {
                logger.info("No worker function provided. Loading default.")
                context.worker = require('./lib/worker');
            }
        }

        /**
         * If the bootstrap option is provided we run the bootstrap function to 
         * do any initial application setup. 
         **/
        if (argv.bootstrap) {
            if (config.bootstrap && typeof config.bootstrap === 'function') {
                config.bootstrap(context)
            }
        }

        /**
         * Use cluster to start multiple workers<commit_msg>Adding bootstrap functionality<commit_after>'use strict';

module.exports = function(config) {
    var domain = require('domain');
    var primary = domain.create();

    var logger = require('./lib/logging')(config.name);

    function errorHandler(err) {
        logger.error("Worker crashed");
        if (err.message) {
            logger.error(err.message);
        }

        if (err.stack) {
            logger.error(err.stack);
        }

        process.exit(-1);
    }

    // Domain emits 'error' when it's given an unhandled error
    primary.on('error', errorHandler);
    process.on('uncaughtException', errorHandler);

    primary.run(function() {
        var argv = require('yargs')
            .alias('c', 'configfile')
            .alias('b', 'bootstrap').argv;

        /*
         * Service configuration context
         */
        var context = {};
        context.logger = logger;
        context.configfile = argv.configfile;
        context.sysconfig = require('./lib/sysconfig')(context);
        if (! context.sysconfig) {
            throw "No system configuration. Can not continue."
        }

        context.cluster = require('cluster');

        loadModule('elasticsearch', config, context);
        loadModule('mongodb', config, context);
        loadModule('statsd', config, context);
        loadModule('redis', config, context);

        // The master shouldn't need these connections.
        if (! context.cluster.isMaster) {
            // We have to load this here so it uses the same mongoose instance
            // This is not really where this belongs.
            if (config.baucis) {
                logger.info("Loading module Baucis");
                context.baucis = require('baucis');
            }

            if (config.worker && typeof config.worker === 'function') {
                context.worker = config.worker;
            }
            else {
                logger.info("No worker function provided. Loading default.")
                context.worker = require('./lib/worker');
            }
        }

        /**
         * If the bootstrap option is provided we run the bootstrap function to 
         * do any initial application setup. 
         **/
        if (argv.bootstrap) {
            if (config.bootstrap && typeof config.bootstrap === 'function') {
                config.bootstrap(context, function() {
                    //process.exit(0);
                });
            }
            else {
                logger.error("No bootstrap function provided. Nothing to do.");
                //process.exit(0);
            }
        }

        /**
         * Use cluster to start multiple workers
{'fraction_non_alphanumeric': 0.1202271392206775, 'fraction_numerical': 0.0007832386919913843, 'mean_word_length': 1.7388739946380698, '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': '2114573', 'n_tokens_mistral': 1292, 'n_tokens_neox': 1232, 'n_words': 334}