pandora
Version:
A powerful and lightweight application manager for Node.js applications powered by TypeScript.
136 lines • 4.97 kB
JavaScript
"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 });
const uuid = require("uuid");
const const_1 = require("../const");
const ProcfileReconciler_1 = require("../application/ProcfileReconciler");
const ApplicationHandler_1 = require("../application/ApplicationHandler");
const fs_1 = require("mz/fs");
const LoggerBroker_1 = require("../universal/LoggerBroker");
class ComplexHandler {
constructor(appRepresentation) {
this.appId = null;
this.appRepresentation = null;
this.state = const_1.State.pending;
this.appId = uuid.v4();
this.appRepresentation = appRepresentation;
}
get name() {
return this.appRepresentation.appName;
}
get appDir() {
return this.appRepresentation.appDir;
}
get mode() {
return this.appRepresentation.mode;
}
get pids() {
const ret = [];
if (this.mountedApplications) {
for (const app of this.mountedApplications) {
if (app.pid) {
ret.push(app.pid);
}
}
}
return ret;
}
get startCount() {
let ret = 0;
if (this.mountedApplications) {
for (const app of this.mountedApplications) {
ret += app.startCount;
}
}
return ret;
}
get uptime() {
if (!this.startTime) {
return 0;
}
return (Date.now() - this.startTime) / 1000;
}
getComplex() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.complexStructure) {
this.complexStructure = yield ProcfileReconciler_1.ProcfileReconciler.getComplexViaNewProcess(this.appRepresentation);
}
return this.complexStructure;
});
}
fillMounted() {
return __awaiter(this, void 0, void 0, function* () {
if (this.mountedApplications) {
return;
}
this.mountedApplications = [];
const { mount: mountList } = yield this.getComplex();
for (const mount of mountList) {
const appHandler = new ApplicationHandler_1.ApplicationHandler(mount);
this.mountedApplications.push(appHandler);
}
});
}
start() {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield fs_1.exists(this.appDir))) {
new Error(`AppDir ${this.appDir} does not exist`);
}
yield this.backupStdoutLogFile();
yield this.fillMounted();
if (!this.mountedApplications.length) {
throw new Error('Start failed, looks like not a pandora project, in appDir ' + this.appDir);
}
const startedHandlers = [];
for (const appHandler of this.mountedApplications) {
try {
yield appHandler.start();
}
catch (error) {
for (const started of startedHandlers) {
yield started.stop();
}
throw error;
}
startedHandlers.push(appHandler);
}
this.state = const_1.State.complete;
this.startTime = Date.now();
});
}
stop() {
return __awaiter(this, void 0, void 0, function* () {
if (this.state === const_1.State.stopped) {
return;
}
for (const appHandler of this.mountedApplications) {
yield appHandler.stop();
}
this.state = const_1.State.stopped;
});
}
reload(processName) {
return __awaiter(this, void 0, void 0, function* () {
for (const appHandler of this.mountedApplications) {
yield appHandler.reload(processName);
}
});
}
backupStdoutLogFile() {
return __awaiter(this, void 0, void 0, function* () {
const targetPath = LoggerBroker_1.getAppLogPath(this.name, 'nodejs_stdout');
if (yield fs_1.exists(targetPath)) {
yield LoggerBroker_1.backupLog(targetPath);
}
});
}
}
exports.ComplexHandler = ComplexHandler;
//# sourceMappingURL=ComplexHandler.js.map