pandora
Version:
A powerful and lightweight application manager for Node.js applications powered by TypeScript.
147 lines • 5.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
require('source-map-support').install();
const program = require("commander");
const const_1 = require("../const");
const assert = require("assert");
const pandora_dollar_1 = require("pandora-dollar");
const ScalableMaster_1 = require("./ScalableMaster");
const SpawnWrapperUtils_1 = require("./SpawnWrapperUtils");
const pandora_dollar_2 = require("pandora-dollar");
const CoreSDKWithExtendedConfig_1 = require("../util/CoreSDKWithExtendedConfig");
/**
* class ProcessBootstrap
* Bootstrap a worker process, handing all phases of an application stating
*/
class ProcessBootstrap {
constructor(processRepresentation) {
this.processRepresentation = processRepresentation;
if (this.processRepresentation.scale > 1) {
this.master = new ScalableMaster_1.ScalableMaster(processRepresentation);
return;
}
else {
const { appName, appDir } = this.processRepresentation;
this.coreSdk = new CoreSDKWithExtendedConfig_1.CoreSDKWithExtendedConfig({
mode: 'worker',
appName, appDir
});
}
}
async start() {
if (this.master) {
await this.startAsMaster();
return;
}
await this.startAsWorker();
}
async stop() {
if (this.master) {
await this.master.stop();
return;
}
try {
await this.coreSdk.stop();
}
catch (err) {
// ignore
}
SpawnWrapperUtils_1.SpawnWrapperUtils.unwrap();
}
async startAsMaster() {
await this.master.start();
}
async startAsWorker() {
SpawnWrapperUtils_1.SpawnWrapperUtils.setAsFirstLevel();
SpawnWrapperUtils_1.SpawnWrapperUtils.wrap();
process.env[const_1.PANDORA_PROCESS] = JSON.stringify(this.processRepresentation);
await this.coreSdk.start();
// Require the entryFile if there given it, pandora.fork() dep on it
if (this.processRepresentation.entryFile) {
const entryFileBaseDir = this.processRepresentation.entryFileBaseDir;
const ownRequire = entryFileBaseDir ? pandora_dollar_1.makeRequire(entryFileBaseDir) : require;
ownRequire(this.processRepresentation.entryFile);
}
}
/**
* A static method to handing the CLI
*/
static cmd() {
program
.allowUnknownOption()
.option('--params [params]')
.parse(process.argv);
let options;
try {
options = JSON.parse(program.params);
assert(options.appName, 'The field appName required by ProcessBootstrap');
assert(options.appDir, 'The field appDir required by ProcessBootstrap');
assert(options.processName, 'The field processName required by ProcessBootstrap');
}
catch (err) {
err.message = `Invalid options "${program.params}", ${err.message}`;
pandora_dollar_2.consoleLogger.error(err);
if (process.send) {
process.send({ action: const_1.PROCESS_ERROR, error: err });
}
return;
}
process.__pandoraOriginArgv = Array.from(process.argv);
Array.prototype.splice.apply(process.argv, [
2, process.argv.length - 2,
...(options.args || [])
]);
if (options.entryFile) {
process.argv[1] = options.entryFile;
}
const processBootstrap = new ProcessBootstrap(options);
process.on('message', (message) => {
if (message.action === const_1.SHUTDOWN) {
processBootstrap.stop().then(() => {
if (process.send) {
process.send({ action: const_1.FINISH_SHUTDOWN });
}
}).catch(pandora_dollar_2.consoleLogger.error);
}
});
const onProcessTerm = (sig) => {
pandora_dollar_2.consoleLogger.info(`Process [name = ${options.processName}, pid = ${process.pid}] Receive a signal ${sig}, start to stopping, pid ${process.pid}`);
processBootstrap.stop().then(() => {
process.exit(0);
}).catch((err) => {
pandora_dollar_2.consoleLogger.error(err);
process.exit(1);
});
};
process.once('SIGQUIT', onProcessTerm.bind(null, 'SIGQUIT'));
process.once('SIGTERM', onProcessTerm.bind(null, 'SIGTERM'));
process.once('SIGINT', onProcessTerm.bind(null, 'SIGINT'));
processBootstrap.start().then(() => {
if (process.send) {
process.send({ action: const_1.PROCESS_READY });
}
}).catch((err) => {
pandora_dollar_2.consoleLogger.error(err);
pandora_dollar_2.consoleLogger.error('An error occurred during the start of ProcessBootstrap.');
if (process.send) {
process.send({ action: const_1.PROCESS_ERROR, error: err });
}
});
}
}
exports.ProcessBootstrap = ProcessBootstrap;
let cmdDid = false;
function cmd() {
if (cmdDid) {
return;
}
cmdDid = true;
ProcessBootstrap.cmd();
}
exports.cmd = cmd;
// Handing CLI if this module be the main module
if (require.main === module || process.env.RUN_PROCESS_BOOTSTRAP_BY_FORCE) {
delete process.env.RUN_PROCESS_BOOTSTRAP_BY_FORCE;
cmd();
}
//# sourceMappingURL=ProcessBootstrap.js.map