<commit_before>"use strict";
var EventDispatcher     = require('appolo').EventDispatcher,
    child_process       = require('child_process'),
    path                = require('path'),
    moment              = require('moment'),
    Q                   = require('q');

module.exports = EventDispatcher.define({

    /*
     *  swampService EventDispatcher
     *  configurations and dependency injection
     */
    $config: {
        id: 'swampService',
        singleton: false,
        properties: [
            {
                name: '_createWatcher',
                factoryMethod: 'watcher'
            },
            {
                name: '_createLogger',
                factoryMethod: 'swampLogger'
            },
            {
                name: '_createMonitor',
                factoryMethod: 'monitor'
            }
        ],
        inject: ['env',
            'utils',
            'environmentsManager',
            'optionsManager',
            'mainLoggersManager',
            'logOptionsParser',
            'userResolverService'],

        initMethod: 'initialize'
    },

    /*
     *  swampService constructor
     */
    constructor: function(service){

        EventDispatcher.call(this);

        this._environments = {};
        this._options = service.options || {};
        this._args = service.args || [];

        this.name = service.name;
        this.description = service.description;
        this.path = service.path;
        this.script = service.script;
        this.command = service.command;

        this._loggers = null;
        this._pathWatcher = null;
        this._monitor = null;
        this._monitorMemory = false;
        this._monitorCpu = false;

        this.isRunning = false;
        this.runningEnvironment = null;
        this.process = null;
        this.pid = null;
        this.runRetries = 0;
        this.startTime = null;

        this.serviceParams = service;
    },

    /*
     *  Swamp service initializer
     *
     *  Initializing the logger files, environments, memory and cpu monitors
     *  and parsing the options configured in the Swampfile
     */
    initialize: function() {

        this._initializeLoggers(this.serviceParams.logs);

        this._environments = this._initializeEnvironments(this.serviceParams.environments || []);

        this._monitorMemory = this.optionsManager.getOptions().monitor.memory;
        this._monitorCpu = this.optionsManager.getOptions().monitor.cpu;

        if(this._options.restartOnChange) {
            this._pathWatcher = this._createWatcher(this.path, this._options.restartOnChange, this._onWatcherEvent.bind(this));
        }

        if(this._monitorMemory || this._monitorCpu) {
            this._monitor = this._createMonitor(this._monitorMemory, this._monitorCpu);
        }

    },

    /*
     *  Serialize this service options for connected dashboard and CLI clients
     */
    serialize: function(includeLogs) {

        var serialized = {
            name: this.name,
            description: this.description,
            path: this.path,
            script: this.script,
            command: this.command,
            isRunning: this.isRunning,
            runningEnvironment: this.runningEnvironment,
            pid: this.pid,
            startTime: this.startTime,<commit_msg>start monitor process before files monitor<commit_after>"use strict";
var EventDispatcher     = require('appolo').EventDispatcher,
    child_process       = require('child_process'),
    path                = require('path'),
    moment              = require('moment'),
    Q                   = require('q');

module.exports = EventDispatcher.define({

    /*
     *  swampService EventDispatcher
     *  configurations and dependency injection
     */
    $config: {
        id: 'swampService',
        singleton: false,
        properties: [
            {
                name: '_createWatcher',
                factoryMethod: 'watcher'
            },
            {
                name: '_createLogger',
                factoryMethod: 'swampLogger'
            },
            {
                name: '_createMonitor',
                factoryMethod: 'monitor'
            }
        ],
        inject: ['env',
            'utils',
            'environmentsManager',
            'optionsManager',
            'mainLoggersManager',
            'logOptionsParser',
            'userResolverService'],

        initMethod: 'initialize'
    },

    /*
     *  swampService constructor
     */
    constructor: function(service){

        EventDispatcher.call(this);

        this._environments = {};
        this._options = service.options || {};
        this._args = service.args || [];

        this.name = service.name;
        this.description = service.description;
        this.path = service.path;
        this.script = service.script;
        this.command = service.command;

        this._loggers = null;
        this._pathWatcher = null;
        this._monitor = null;
        this._monitorMemory = false;
        this._monitorCpu = false;

        this.isRunning = false;
        this.runningEnvironment = null;
        this.process = null;
        this.pid = null;
        this.runRetries = 0;
        this.startTime = null;

        this.serviceParams = service;
    },

    /*
     *  Swamp service initializer
     *
     *  Initializing the logger files, environments, memory and cpu monitors
     *  and parsing the options configured in the Swampfile
     */
    initialize: function() {

        this._initializeLoggers(this.serviceParams.logs);

        this._environments = this._initializeEnvironments(this.serviceParams.environments || []);

        this._monitorMemory = this.optionsManager.getOptions().monitor.memory;
        this._monitorCpu = this.optionsManager.getOptions().monitor.cpu;

        if(this._monitorMemory || this._monitorCpu) {
            this._monitor = this._createMonitor(this._monitorMemory, this._monitorCpu);
        }

        if(this._options.restartOnChange) {
            this._pathWatcher = this._createWatcher(this.path, this._options.restartOnChange, this._onWatcherEvent.bind(this));
        }
    },

    /*
     *  Serialize this service options for connected dashboard and CLI clients
     */
    serialize: function(includeLogs) {

        var serialized = {
            name: this.name,
            description: this.description,
            path: this.path,
            script: this.script,
            command: this.command,
            isRunning: this.isRunning,
            runningEnvironment: this.runningEnvironment,
            pid: this.pid,
            startTime: this.startTime,
{'fraction_non_alphanumeric': 0.10164917541229385, 'fraction_numerical': 0.0002998500749625187, 'mean_word_length': 1.8207188160676533, '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': '6171897', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1659, 'n_words': 332}