UNPKG

pandora

Version:

A powerful and lightweight application manager for Node.js applications powered by TypeScript.

124 lines 5.2 kB
'use strict'; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); require('source-map-support').install(); const const_1 = require("../const"); const assert = require("assert"); const LoggerBroker_1 = require("../universal/LoggerBroker"); const pandora_dollar_1 = require("pandora-dollar"); const SpawnWrapperUtils_1 = require("../daemon/SpawnWrapperUtils"); const MonitorManager_1 = require("../monitor/MonitorManager"); const pandora_env_1 = require("pandora-env"); const program = require('commander'); /** * Class ProcessBootstrap */ class ProcessBootstrap { constructor(entry, options) { this.entry = entry; this.applicationRepresentation = options; } /** * start process * @returns {Promise<void>} */ start() { return __awaiter(this, void 0, void 0, function* () { this.injectMonitor(); if ('fork' === this.applicationRepresentation.mode) { process.env[const_1.PANDORA_APPLICATION] = JSON.stringify(this.applicationRepresentation); SpawnWrapperUtils_1.SpawnWrapperUtils.wrap(); } const entryFileBaseDir = this.applicationRepresentation.entryFileBaseDir; const ownRequire = entryFileBaseDir ? pandora_dollar_1.makeRequire(entryFileBaseDir) : require; const entryMod = ownRequire(this.entry); // Only require that entry if the mode be fork if ('fork' === this.applicationRepresentation.mode) { return; } // Otherwise it needs the entire procedures. const entryFn = 'function' === typeof entryMod ? entryMod : entryMod.default; assert('function' === typeof entryFn, 'The entry should export a function, during loading ' + this.entry); yield new Promise((resolve, reject) => { const options = Object.assign({}, this.applicationRepresentation || {}); if (entryFn.length >= 2) { entryFn(options, (err) => { if (err) { return reject(err); } resolve(); }); } else { entryFn(options); resolve(); } }); }); } injectMonitor() { if (!pandora_env_1.EnvironmentUtil.getInstance().isReady()) { // Handing the environment object injecting const environment = new pandora_env_1.DefaultEnvironment({ appDir: this.applicationRepresentation.appDir, appName: this.applicationRepresentation.appName, processName: this.applicationRepresentation.processName || ProcessBootstrap.processName, pandoraLogsDir: LoggerBroker_1.getPandoraLogsDir() }); pandora_env_1.EnvironmentUtil.getInstance().setCurrentEnvironment(environment); } // To start worker process monitoring MonitorManager_1.MonitorManager.injectProcessMonitor(); } static cmd() { program .option('--entry [entry]') .option('--params [params]') .parse(process.argv); const entry = program.entry; let options = program.params; try { options = JSON.parse(options); } catch (err) { err.message = `Invalid options "${options}", ${err.message}`; throw err; } const processBootstrap = new ProcessBootstrap(entry, options); processBootstrap.start().then(() => { if (process.send) { process.send({ action: const_1.APP_START_SUCCESS }); } }).catch((err) => { LoggerBroker_1.consoleLogger.error(err); LoggerBroker_1.consoleLogger.error('An error occurred during the start of ProcessBootstrap.'); if (process.send) { process.send({ action: const_1.APP_START_ERROR, error: err }); } }); } } ProcessBootstrap.processName = 'scalableMaster'; exports.ProcessBootstrap = ProcessBootstrap; let cmdDid = false; function cmd() { if (cmdDid) { return; } cmdDid = true; ProcessBootstrap.cmd(); } exports.cmd = cmd; // require.main === module maybe be 'false' after patched spawn wrap if (require.main === module || process.env.RUN_PROCESS_BOOTSTRAP_BY_FORCE) { delete process.env.RUN_PROCESS_BOOTSTRAP_BY_FORCE; cmd(); } //# sourceMappingURL=ProcessBootstrap.js.map