UNPKG

1.79 kBJavaScriptView Raw
1const fs = require('fs');
2const forEachLimit = require('async/forEachLimit');
3
4var cst = require('../../../constants.js');
5var Common = require('../../Common.js');
6
7module.exports = function(CLI) {
8 /**
9 * Monitor Selectively Processes (auto filter in interaction)
10 * @param String state 'monitor' or 'unmonitor'
11 * @param String target <pm_id|name|all>
12 * @param Function cb callback
13 */
14 CLI.prototype.monitorState = function(state, target, cb) {
15 var that = this;
16
17 if (!target) {
18 Common.printError(cst.PREFIX_MSG_ERR + 'Please specify an <app_name|pm_id>');
19 return cb ? cb(new Error('argument missing')) : that.exitCli(cst.ERROR_EXIT);
20 }
21
22 function monitor (pm_id, cb) {
23 // State can be monitor or unmonitor
24 that.Client.executeRemote(state, pm_id, cb);
25 }
26 if (target === 'all') {
27 that.Client.getAllProcessId(function (err, procs) {
28 if (err) {
29 Common.printError(err);
30 return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
31 }
32 forEachLimit(procs, 1, monitor, function (err, res) {
33 return typeof cb === 'function' ? cb(err, res) : that.speedList();
34 });
35 });
36 } else if (!Number.isInteger(parseInt(target))) {
37 this.Client.getProcessIdByName(target, true, function (err, procs) {
38 if (err) {
39 Common.printError(err);
40 return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
41 }
42 forEachLimit(procs, 1, monitor, function (err, res) {
43 return typeof cb === 'function' ? cb(err, res) : that.speedList();
44 });
45 });
46 } else {
47 monitor(parseInt(target), function (err, res) {
48 return typeof cb === 'function' ? cb(err, res) : that.speedList();
49 });
50 }
51 };
52}