{"size": 4099, "ext": "js", "max_stars_repo_path": "dist/commonjs/DynaProcessManager.js", "max_stars_repo_name": "aneldev/dyna-process-manager", "max_stars_repo_stars_event_min_datetime": "2020-02-05T09:01:39.000Z", "max_stars_repo_stars_event_max_datetime": "2020-02-05T09:01:39.000Z", "max_issues_repo_path": "dist/commonjs/DynaProcessManager.js", "max_issues_repo_name": "aneldev/dyna-process-manager", "max_issues_repo_issues_event_min_datetime": "2019-12-28T00:02:49.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T09:55:21.000Z", "max_forks_repo_path": "dist/commonjs/DynaProcessManager.js", "max_forks_repo_name": "aneldev/dyna-process-manager", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 12, "max_forks_count": null, "avg_line_length": 38.308411215, "max_line_length": 140, "alphanum_fraction": 0.5352525006}
"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var dyna_logger_1 = require("dyna-logger");
var DynaProcess_1 = require("./DynaProcess");
var DynaProcessManager = /** @class */ (function () {
    function DynaProcessManager(_config) {
        if (_config === void 0) { _config = {}; }
        this._config = _config;
        this._processes = {};
        this._config = __assign({}, this._config, { loggerSettings: __assign({ bufferLimit: 2000 }, (this._config.loggerSettings || {})) });
        new dyna_logger_1.DynaLogger(this._config.loggerSettings);
    }
    DynaProcessManager.prototype.addProcess = function (processSetup) {
        if (!processSetup.loggerSettings)
            processSetup.loggerSettings = this._config.loggerSettings;
        var newProcess = new DynaProcess_1.DynaProcess(processSetup);
        this._processes[newProcess.id] = newProcess;
        return newProcess;
    };
    DynaProcessManager.prototype.removeProcess = function (id) {
        var _this = this;
        return new Promise(function (resolve, reject) {
            var process = _this.getProcess(id);
            if (!process) {
                reject({
                    section: 'ProcessManager/removeProcess',
                    code: 3598644,
                    message: 'Process not found with this id',
                    data: { id: id },
                });
                return;
            }
            if (process.active) {
                reject({
                    section: 'ProcessManager/removeProcess',
                    code: 3598645,
                    message: 'Process is working',
                    data: { id: id },
                });
                return;
            }
            delete _this._processes[id];
            resolve();
        });
    };
    DynaProcessManager.prototype.getProcess = function (id) {
        return this._processes[id];
    };
    Object.defineProperty(DynaProcessManager.prototype, "count", {
        get: function () {
            return Object.keys(this._processes).length;
        },
        enumerable: true,
        configurable: true
    });
    DynaProcessManager.prototype.stop = function (id) {
        var _this = this;
        return new Promise(function (resolve, reject) {
            var process = _this.getProcess(id);
            if (!process) {
                reject({
                    section: 'ProcessManager/stop',
                    code: 3598643,
                    message: 'Process not found with this id',
                    data: { id: id },
                });
                return; // exit
            }
            if (!process.active) {
                resolve();
                return; // exit
            }
            var handleStop = function () {
                process.off(DynaProcess_1.EDynaProcessEvent.STOP, handleStop);
                resolve();
            };
            var handleCrash = function () {
                process.off(DynaProcess_1.EDynaProcessEvent.CRASH, handleCrash);
                resolve();
            };
            process.on(DynaProcess_1.EDynaProcessEvent.STOP, handleStop);
            process.on(DynaProcess_1.EDynaProcessEvent.CRASH, handleCrash);
            setTimeout(function () {
                process.stop();
            }, 10);
        });
    };
    DynaProcessManager.prototype.stopAll = function () {
        var _this = this;
        return Promise.all(Object.keys(this._processes)
            .map(function (id) { return _this.stop(id); })).then(function () { return undefined; });
    };
    return DynaProcessManager;
}());
exports.DynaProcessManager = DynaProcessManager;
//# sourceMappingURL=DynaProcessManager.js.map