UNPKG

the-shepherd

Version:
152 lines (143 loc) 5.18 kB
// Generated by CoffeeScript 2.5.1 (function() { var $, Chalk, Groups, SlimProcess, echo, formatUptime, healthSymbol; ({$, echo} = require('../common')); Chalk = require('chalk'); ({Groups} = require('../daemon/groups')); SlimProcess = require('../util/process-slim'); ({formatUptime} = require('../util/format')); healthSymbol = function(v) { switch (v) { case void 0: return Chalk.yellow("?"); case true: return Chalk.green("\u2713"); case false: return Chalk.red("x"); } }; Object.assign(module.exports, { options: [["--group <group>", "Only show status of one group."], ["--instance <id>", "Only show status of one instance."]], toMessage: function(cmd) { return { c: 'status', i: cmd.instance, g: cmd.group }; }, onResponse: function(resp, socket) { var g, group, has_pid, i, j, len, line, pad_columns, ref, results; try { if (socket == null) { return console.log("Socket: null."); } if (!((resp != null) && ('object' === typeof resp) && ('groups' in resp))) { return console.log("Response:", resp); } console.log(`Status: online, pid: ${resp.pid} net: (${resp.send - resp.start}ms, ${Date.now() - resp.send}ms)`); console.log(`Groups: ${resp.groups.length}`); pad_columns = function(a, w = [19, 7, 7, 10, 8, 8, 14, 7, 7]) { var i, item; return ((function() { var j, len, results; results = []; for (i = j = 0, len = a.length; j < len; i = ++j) { item = a[i]; results.push($.padLeft(String(item != null ? item : ''), w[i])); } return results; })()).join(''); }; ref = resp.groups; results = []; for (g = j = 0, len = ref.length; j < len; g = ++j) { group = ref[g]; if (g === 0) { console.log(pad_columns(["Instance", "PID", "Port", "Uptime", "Healthy", "Enabled", "Status", "CPU", "RAM"])); console.log(pad_columns(["--------", "---", "----", "------", "-------", "-------", "------", "---", "---"])); } else { console.log(''); } results.push((function() { var k, len1, ref1, results1; ref1 = group.procs; results1 = []; for (i = k = 0, len1 = ref1.length; k < len1; i = ++k) { line = ref1[i]; has_pid = line[1] != null; if (line[1] == null) { line[1] = Chalk.red("-"); } if (line[2] == null) { line[2] = Chalk.red("-"); } line[3] = formatUptime(line[3]); line[4] = has_pid && (healthSymbol(line[4])) || line[1]; line[5] = healthSymbol(line[5]); line[7] = has_pid && (parseFloat(line[7]).toFixed(1) + "%") || line[1]; line[8] = has_pid && ($.commaize(Math.round(line[8] / 1024)) + "mb") || line[1]; results1.push(console.log(pad_columns(line))); } return results1; })()); } return results; } finally { if (socket != null) { socket.end(); } } }, onMessage: function(msg, client, cb) { var output; output = { start: Date.now(), pid: process.pid, groups: [] }; SlimProcess.getProcessTable((err, procs) => { // force the cache to be fresh if (err) { return typeof cb === "function" ? cb(err, false) : void 0; } Groups.forEach(function(group) { var _group, j, len, pcpu, pid, proc, ref, ref1, ref2, ref3, results, rss; if ((ref = msg.g) !== null && ref !== (void 0) && ref !== group.name) { return; } output.groups.push(_group = { name: group.name, cd: group.cd, exec: group.exec, n: group.n, port: group.port, grace: group.grace, procs: [] }); results = []; for (j = 0, len = group.length; j < len; j++) { proc = group[j]; if ((ref1 = msg.i) !== null && ref1 !== (void 0) && ref1 !== proc.id) { continue; } pid = (ref2 = proc.proc) != null ? ref2.pid : void 0; pcpu = rss = 0; if (pid != null) { SlimProcess.visitProcessTree(pid, function(_p) { pcpu += _p.pcpu; return rss += _p.rss; }); } results.push(_group.procs.push([proc.id, (ref3 = proc.proc) != null ? ref3.pid : void 0, proc.port, proc.uptime, proc.healthy, proc.enabled, proc.statusString, pcpu, rss])); } return results; }); output.send = Date.now(); if (client != null) { client.write($.TNET.stringify(output)); } return typeof cb === "function" ? cb(null, true) : void 0; }); return false; } }); }).call(this);