pandora
Version:
A powerful and lightweight application manager for Node.js applications powered by TypeScript.
110 lines • 3.44 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const uuid = require("uuid");
const const_1 = require("../const");
const ProcfileReconciler_1 = require("./ProcfileReconciler");
const ProcessHandler_1 = require("./ProcessHandler");
const fs_1 = require("mz/fs");
class ApplicationHandler {
constructor(appRepresentation) {
this.appId = null;
this.appRepresentation = null;
this.state = const_1.State.pending;
this.appId = uuid.v4();
this.appRepresentation = appRepresentation;
}
get appName() {
return this.appRepresentation.appName;
}
get appDir() {
return this.appRepresentation.appDir;
}
get pids() {
const ret = [];
if (this.mountedProcesses) {
for (const app of this.mountedProcesses) {
if (app.pid) {
ret.push(app.pid);
}
}
}
return ret;
}
get startCount() {
let ret = 0;
if (this.mountedProcesses) {
for (const handler of this.mountedProcesses) {
ret += handler.startCount;
}
}
return ret;
}
get restartCount() {
let ret = this.startCount;
ret = Math.max(0, ret - this.mountedProcesses.length);
return ret;
}
get uptime() {
if (!this.startTime) {
return 0;
}
return (Date.now() - this.startTime) / 1000;
}
async getStructure() {
if (!this.structure) {
this.structure = await ProcfileReconciler_1.ProcfileReconciler.getStructureViaNewProcess(this.appRepresentation);
}
return this.structure;
}
async fillMounted() {
if (this.mountedProcesses) {
return;
}
this.mountedProcesses = [];
const { process: mountList } = await this.getStructure();
for (const mount of mountList) {
const appHandler = new ProcessHandler_1.ProcessHandler(mount);
this.mountedProcesses.push(appHandler);
}
}
async start() {
if (!await fs_1.exists(this.appDir)) {
throw new Error(`AppDir ${this.appDir} does not exist`);
}
await this.fillMounted();
if (!this.mountedProcesses.length) {
throw new Error('Start failed, looks like not a pandora project, in appDir ' + this.appDir);
}
const startedHandlers = [];
for (const handler of this.mountedProcesses) {
try {
await handler.start();
}
catch (error) {
for (const started of startedHandlers) {
await started.stop();
}
throw error;
}
startedHandlers.push(handler);
}
this.state = const_1.State.complete;
this.startTime = Date.now();
}
async stop() {
if (this.state === const_1.State.stopped) {
return;
}
for (const appHandler of this.mountedProcesses) {
await appHandler.stop();
}
this.state = const_1.State.stopped;
}
async reload(processName) {
for (const appHandler of this.mountedProcesses) {
await appHandler.reload(processName);
}
}
}
exports.ApplicationHandler = ApplicationHandler;
//# sourceMappingURL=ApplicationHandler.js.map