<commit_before>/*globals requireJS*/
/*jshint node: true*/
/**
 * @module Server:ServerWorkerManager
 * @author kecso / https://github.com/kecso
 */
'use strict';

var Child = require('child_process'),
    process = require('process'),
    path = require('path'),
    CONSTANTS = require('./constants'),
    GUID = requireJS('common/util/guid'),
    SIMPLE_WORKER_JS = path.join(__dirname, 'simpleworker.js'),
    CONNECTED_WORKER_JS = path.join(__dirname, 'connectedworker.js');


function ServerWorkerManager(_parameters) {
    var self = this,
        _managerId = null,
        _workers = {},
        _idToPid = {},
        _waitingRequests = [],
        debugPort = 5859,
        gmeConfig = _parameters.globConf,
        logger = _parameters.logger.fork('serverworkermanager');

    logger.debug('SIMPLE_WORKER_JS:', SIMPLE_WORKER_JS);
    logger.debug('CONNECTED_WORKER_JS:', CONNECTED_WORKER_JS);

    //helping functions
    function reserveWorker(workerType) {
        var debug = false,
            execArgv = process.execArgv.filter(function (arg) {
                if (arg.indexOf('--debug-brk') === 0) {
                    logger.info('Main process is in debug mode', arg);
                    debug = '--debug-brk';
                    return false;
                } else if (arg.indexOf('--debug') === 0) {
                    logger.info('Main process is in debug mode', arg);
                    debug = '--debug';
                    return false;
                }
                return true;
            });
        // http://stackoverflow.com/questions/16840623/how-to-debug-node-js-child-forked-process
        // For some reason --debug-brk is given here..
        if (debug) {
            execArgv.push(debug + '=' + debugPort.toString());
            logger.info('Child debug port: ' + debugPort.toString());
            debugPort += 1;
        }

        logger.debug('execArgv for main process', process.execArgv);
        logger.debug('execArgv for new child process', execArgv);

        if (Object.keys(_workers || {}).length < gmeConfig.server.maxWorkers) {
            var childProcess;

            if (workerType === CONSTANTS.workerTypes.connected) {
                childProcess = Child.fork(CONNECTED_WORKER_JS, [], {execArgv: execArgv});
            } else {
                childProcess = Child.fork(SIMPLE_WORKER_JS, [], {execArgv: execArgv});
            }

            _workers[childProcess.pid] = {
                childProcess: childProcess,
                state: CONSTANTS.workerStates.initializing,
                type: workerType,
                cb: null
            };

            logger.debug('workerPid forked ' + childProcess.pid);
            childProcess.on('message', messageHandling);
            childProcess.on('exit', function (code, signal) {
                logger.debug('worker has exited: ' + childProcess.pid);
                if (code !== null && !signal) {
                    logger.warn('worker ' + childProcess.pid + ' has exited abnormally with code ' + code);
                }

                if (workerType === CONSTANTS.workerTypes.connected) {<commit_msg>WIP Dont print warn on exitcode 0 from workers<commit_after>/*globals requireJS*/
/*jshint node: true*/
/**
 * @module Server:ServerWorkerManager
 * @author kecso / https://github.com/kecso
 */
'use strict';

var Child = require('child_process'),
    process = require('process'),
    path = require('path'),
    CONSTANTS = require('./constants'),
    GUID = requireJS('common/util/guid'),
    SIMPLE_WORKER_JS = path.join(__dirname, 'simpleworker.js'),
    CONNECTED_WORKER_JS = path.join(__dirname, 'connectedworker.js');


function ServerWorkerManager(_parameters) {
    var self = this,
        _managerId = null,
        _workers = {},
        _idToPid = {},
        _waitingRequests = [],
        debugPort = 5859,
        gmeConfig = _parameters.globConf,
        logger = _parameters.logger.fork('serverworkermanager');

    logger.debug('SIMPLE_WORKER_JS:', SIMPLE_WORKER_JS);
    logger.debug('CONNECTED_WORKER_JS:', CONNECTED_WORKER_JS);

    //helping functions
    function reserveWorker(workerType) {
        var debug = false,
            execArgv = process.execArgv.filter(function (arg) {
                if (arg.indexOf('--debug-brk') === 0) {
                    logger.info('Main process is in debug mode', arg);
                    debug = '--debug-brk';
                    return false;
                } else if (arg.indexOf('--debug') === 0) {
                    logger.info('Main process is in debug mode', arg);
                    debug = '--debug';
                    return false;
                }
                return true;
            });
        // http://stackoverflow.com/questions/16840623/how-to-debug-node-js-child-forked-process
        // For some reason --debug-brk is given here..
        if (debug) {
            execArgv.push(debug + '=' + debugPort.toString());
            logger.info('Child debug port: ' + debugPort.toString());
            debugPort += 1;
        }

        logger.debug('execArgv for main process', process.execArgv);
        logger.debug('execArgv for new child process', execArgv);

        if (Object.keys(_workers || {}).length < gmeConfig.server.maxWorkers) {
            var childProcess;

            if (workerType === CONSTANTS.workerTypes.connected) {
                childProcess = Child.fork(CONNECTED_WORKER_JS, [], {execArgv: execArgv});
            } else {
                childProcess = Child.fork(SIMPLE_WORKER_JS, [], {execArgv: execArgv});
            }

            _workers[childProcess.pid] = {
                childProcess: childProcess,
                state: CONSTANTS.workerStates.initializing,
                type: workerType,
                cb: null
            };

            logger.debug('workerPid forked ' + childProcess.pid);
            childProcess.on('message', messageHandling);
            childProcess.on('exit', function (code, signal) {
                logger.debug('worker has exited: ' + childProcess.pid);
                if (code !== 0) {
                    logger.warn('worker ' + childProcess.pid + ' has exited abnormally with code ' + code +
                        ', signal', signal);
                }

                if (workerType === CONSTANTS.workerTypes.connected) {
{'fraction_non_alphanumeric': 0.1277602523659306, 'fraction_numerical': 0.005047318611987382, 'mean_word_length': 1.9589360709286048, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5988534', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1725, 'n_words': 357}