container.ts
Version:
Modular application framework
68 lines • 2.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/// <reference types="node" />
const process = require("process");
const Observable_1 = require("rxjs/Observable");
require("rxjs/add/observable/of");
require("rxjs/add/operator/switchMap");
const container_1 = require("../../container");
const Assets_1 = require("../assets/Assets");
// TODO: Env/definition clean up.
exports.ASSET_PROCESS_JSON = "process.json";
/** Node.js process interface. */
class Process extends container_1.ContainerModule {
/** Get Node.js process title. */
static get title() { return process.title; }
/** Set Node.js process title. */
static setTitle(name) {
if (name != null) {
const untyped = process;
untyped.title = name;
}
return process.title;
}
get title() { return Process.title; }
get version() { return this._version; }
get nodeEnvironment() {
const parts = this.version.split("-");
return parts[1] || "unknown";
}
constructor(name, opts) {
super(name, opts, { _assets: Assets_1.Assets.name });
// Default unknown version value.
this._version = "0.0.0-unknown";
}
/** Read process information assets file, handle process events. */
start() {
return this.container.waitStarted(Assets_1.Assets.name)
.switchMap(() => this._assets.readJson(exports.ASSET_PROCESS_JSON))
.switchMap((data) => {
// Set process title.
Process.setTitle(data.name);
this.debug(`title '${this.title}'`);
// Read process verion string.
this._version = data.version || this._version;
this.debug(`version '${this.version}'`);
// Process stop event handlers.
process.on("SIGTERM", this.handleStop.bind(this, "SIGTERM"));
process.on("SIGINT", this.handleStop.bind(this, "SIGINT"));
return Observable_1.Observable.of(undefined);
});
}
/** Stop container when process termination event received. */
handleStop(event) {
this.debug(`stop '${event}'`);
this.container.stop()
.subscribe({
next: () => process.exit(0),
error: (error) => {
// Try to log error and exit with error code.
this.log.error(error);
process.stderr.write(String(error));
process.exit(1);
},
});
}
}
exports.Process = Process;
//# sourceMappingURL=Process.js.map