process-selector.js 1.8 KB

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