UNPKG

container.ts

Version:
178 lines 6.93 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var os = require("os"); var process = require("process"); var container_1 = require("../../container"); var RxJS_1 = require("../../container/RxJS"); var error_1 = require("../error"); var validate_1 = require("../validate"); /** Process error class. */ var ProcessError = /** @class */ (function (_super) { __extends(ProcessError, _super); function ProcessError(cause) { return _super.call(this, { name: "ProcessError" }, cause) || this; } return ProcessError; }(error_1.ErrorChain)); exports.ProcessError = ProcessError; /** Node.js process interface. */ var Process = /** @class */ (function (_super) { __extends(Process, _super); function Process(options) { var _this = _super.call(this, options) || this; _this.version = _this.envVersion; _this.nodeEnvironment = _this.envNodeEnv; // Set process title. Process.setTitle(_this.envName); // Debug environment variables. _this.debug(Process.ENV.NAME + "=\"" + _this.title + "\""); _this.debug(Process.ENV.VERSION + "=\"" + _this.version + "\""); _this.debug(Process.ENV.NODE_ENV + "=\"" + _this.nodeEnvironment + "\""); // Process metrics on interval. RxJS_1.Observable.interval(_this.metricInterval) .subscribe(function () { return _this.getProcessMetrics(_this.status); }); return _this; } Object.defineProperty(Process, "title", { /** Get Node.js process title. */ get: function () { return process.title; }, enumerable: true, configurable: true }); /** Set Node.js process title. */ Process.setTitle = function (name) { if (name != null) { var untyped = process; untyped.title = name; } return process.title; }; Object.defineProperty(Process.prototype, "title", { get: function () { return Process.title; }, enumerable: true, configurable: true }); Object.defineProperty(Process.prototype, "metricInterval", { /** Override in subclass to change metric interval. */ get: function () { return 60000; }, enumerable: true, configurable: true }); Object.defineProperty(Process.prototype, "information", { get: function () { return { name: this.container.name, title: this.title, version: this.version, environment: this.nodeEnvironment, arch: process.arch, platform: process.platform, nodeVersion: process.version, pid: process.pid, type: os.type(), release: os.release(), endianness: os.endianness(), hostname: os.hostname(), }; }, enumerable: true, configurable: true }); Object.defineProperty(Process.prototype, "status", { get: function () { return { uptime: process.uptime(), cpuUsage: process.cpuUsage(), memoryUsage: process.memoryUsage(), }; }, enumerable: true, configurable: true }); /** Try to read process information asset file, handle process events. */ Process.prototype.moduleUp = function () { // Log process information. this.log.info(Process.LOG.INFORMATION, this.information); // Process end signal handlers. process.on("SIGTERM", this.onSignal.bind(this, "SIGTERM")); process.on("SIGINT", this.onSignal.bind(this, "SIGINT")); }; Object.defineProperty(Process.prototype, "envName", { get: function () { return validate_1.Validate.isString(this.environment.get(Process.ENV.NAME) || "node"); }, enumerable: true, configurable: true }); Object.defineProperty(Process.prototype, "envVersion", { get: function () { return validate_1.Validate.isString(this.environment.get(Process.ENV.VERSION) || "1.0.0"); }, enumerable: true, configurable: true }); Object.defineProperty(Process.prototype, "envNodeEnv", { get: function () { return validate_1.Validate.isString(this.environment.get(Process.ENV.NODE_ENV) || "production"); }, enumerable: true, configurable: true }); /** Container down when process termination signal received. */ Process.prototype.onSignal = function (signal) { var _this = this; this.log.info(Process.LOG.SIGNAL, { signal: signal }); this.container.down() .subscribe({ next: function () { return process.exit(0); }, error: function (error) { // Try to log error and exit with error code. error = new ProcessError(error); _this.log.error(error); process.stderr.write(error + "\n"); process.exit(1); }, }); }; Process.prototype.getProcessMetrics = function (status) { this.metric.gauge(Process.METRIC.USER_CPU_USAGE, status.cpuUsage.user); this.metric.gauge(Process.METRIC.SYSTEM_CPU_USAGE, status.cpuUsage.system); this.metric.gauge(Process.METRIC.RSS_MEMORY_USAGE, status.memoryUsage.rss); this.metric.gauge(Process.METRIC.HEAP_TOTAL_MEMORY_USAGE, status.memoryUsage.heapTotal); this.metric.gauge(Process.METRIC.HEAP_USED_MEMORY_USAGE, status.memoryUsage.heapUsed); }; /** Default module name. */ Process.moduleName = "Process"; /** Environment variable names. */ Process.ENV = { NAME: "PROCESS_NAME", VERSION: "PROCESS_VERSION", NODE_ENV: "PROCESS_NODE_ENV", }; /** Log names. */ Process.LOG = { INFORMATION: "ProcessInformation", SIGNAL: "ProcessSignal", }; /** Metric names. */ Process.METRIC = { USER_CPU_USAGE: "ProcessUserCpuUsage", SYSTEM_CPU_USAGE: "ProcessSystemCpuUsage", RSS_MEMORY_USAGE: "ProcessRssMemoryUsage", HEAP_TOTAL_MEMORY_USAGE: "ProcessHeapTotalMemoryUsage", HEAP_USED_MEMORY_USAGE: "ProcessHeapUsedMemoryUsage", }; return Process; }(container_1.Module)); exports.Process = Process; //# sourceMappingURL=Process.js.map