UNPKG

node-process-status

Version:

Receive process status info

96 lines (81 loc) 2.46 kB
'use strict'; const NO_INFO = 'No Info'; function ProcessStatus(options) { this.options = options || {}; this.process = this.options.process || process; this.nodeVersion = this.process.version.substring(1); } ProcessStatus.prototype.getUpTime = function() { if (typeof this.process.uptime === 'function') { const upTime = parseInt(this.process.uptime()); if (!isNaN(upTime)) { const hours = parseInt(upTime / 3600); const minutes = parseInt((upTime % 3600) / 60); const seconds = (upTime % 3600) % 60; let timeString = ''; if (hours) { timeString += hours + 'h '; } if (minutes) { timeString += minutes + 'm '; } timeString += seconds + 's'; return timeString; } } return null; }; ProcessStatus.prototype.getMemoryUsage = function() { if (typeof this.process.memoryUsage === 'function') { const memory = this.process.memoryUsage(); const rssInMb = (memory.rss / 1024) / 1024; const heapTotalInMb = (memory.heapTotal / 1024) / 1024; const heapUsedInMb = (memory.heapUsed / 1024) / 1024; const memoryUsage = {}; if (!isNaN(rssInMb)) { memoryUsage.rss = rssInMb.toFixed(2) + 'mb'; } if (!isNaN(heapTotalInMb)) { memoryUsage.heapTotal = heapTotalInMb.toFixed(2) + 'mb'; } if (!isNaN(heapUsedInMb)) { memoryUsage.heapUsed = heapUsedInMb.toFixed(2) + 'mb'; } if (!Object.getOwnPropertyNames(memoryUsage).length) { return null; } return memoryUsage; } return null; }; ProcessStatus.prototype.getCpuUsage = function() { if (typeof this.process.cpuUsage === 'function') { const cpu = this.process.cpuUsage(); const userCpu = cpu.user / 1000000; const systemCpu = cpu.system / 1000000; const cpuUsage = {}; if (!isNaN(userCpu)) { cpuUsage.user = userCpu + 's'; } if (!isNaN(systemCpu)) { cpuUsage.system = systemCpu + 's'; } if (!Object.getOwnPropertyNames(cpuUsage).length) { return null; } return cpuUsage; } return null; }; ProcessStatus.prototype.getStatus = function() { this.upTime = this.getUpTime() || NO_INFO; this.memoryUsage = this.getMemoryUsage() || NO_INFO; this.cpuUsage = this.getCpuUsage() || NO_INFO; return { nodeVersion: this.nodeVersion, upTime: this.upTime, memoryUsage: this.memoryUsage, cpuUsage: this.cpuUsage }; }; module.exports = ProcessStatus;