【问题标题】:How to make an action for all list(array) items?如何对所有列表(数组)项目进行操作?
【发布时间】:2021-05-08 17:04:04
【问题描述】:

我尝试创建简单的监控工具,用于 ping 配置文件中的所有服务。对所有项目同时运行 ping 命令。目前我可以按索引 ping 单个元素,如何为每个人执行此操作?

config/host.yml

    default:
      server:
        port: 3000
      hosts:
        type: 'CaP AC'
    development:
      hosts:
      type: 'CaP AC'
      model: 'RBcAPGi-5acD2nD'
      location: 'Office'
      list:
        - host: '10.10.10.10'
          user: 'admin'
          password: 'password'
          name: 'cap-1'
        - host: '10.10.4.5'
          user: 'admin'
          password: 'password'
          name: 'cap-2'

控制器/update.js

const Hosts = require('./hosts');
const Monitor = require('ping-monitor');

module.exports = {
  Ping: function (config) {
      return new Promise(function (resolve, reject) {
          config.hosts.list.forEach(cap => {
              let pong = new Monitor({
                  website: "http://"+config.host,
                  interval: 1,
                  config: {
                      intervalUnits: "seconds"
                  },
                  ignoreSSL: true
              })
              pong.on('up', function (res, state) {
                  console.log('Service up!');
              });

              pong.on('error', function (error, res) {
                  console.error(error);
              });
          })
      })

  }

控制器/hosts.js

var hosts = [];
module.exports = hosts;

index.js

const config = yaml_config.load(__dirname + '/config/host.yml');
const Update = require('./controllers/update');
const Hosts = require('./controllers/hosts');

Update.Ping(config).then(result => {
  result.forEach(cap => {
      console.log(cap +result)
  }).catch((err) => {
      console.log(err)
  });
})

【问题讨论】:

  • 你的问题不是很清楚。如果我理解正确,您想用一个 ping-monitor 实例 ping 所有主机。这是不可能的,因为包不支持那种接口。无论哪种方式,包都需要为每个主机发送单独的请求。

标签: javascript node.js arrays list


【解决方案1】:

您可以将每个监视器包装在一个 Promise 中,并在打印收到的状态之前等待所有这些 Promise。以下是一些可以帮助您的示例代码:

module.exports = {
  Ping: function (config) {
        const monitorPromises = config.hosts.list.map(currConfig => {
            return new Promise((resolve) => {
                const currMonitor = new Monitor({
                    website: "http://" + currConfig.host,
                    interval: 1,
                    config: {
                        intervalUnits: "seconds"
                    },
                    ignoreSSL: true
                });
                currMonitor.on('up', function (res, state) {
                    resolve(`${res.website}:${res.port} is up.`);
                });

                currMonitor.on('down', function (res) {
                    resolve(`${res.website} is down!, statusMessage:  ${res.statusMessage}`);
                });

                currMonitor.on('stop', function (res, state) {
                    resolve(`${res.website} monitor has stopped.`);
                });

                currMonitor.on('error', function (error, res) {
                    resolve(error);
                });

                currMonitor.on('timeout', function (error, res) {
                    resolve(error);
                });
            });
        });
        console.log("done");
        return Promise.all(monitorPromises);
    }
};

然后在你的调用者中,你可以这样做:

Update.Ping(config).then(monitors => {
  monitors.forEach(result => {
      console.log(result);
  }).catch((err) => {
      console.log(err)
  });
})

【讨论】:

  • @sdvjke:有什么反馈吗?
  • 非常感谢您的示例。很有意思。一个奇怪的故事真的出现了。我还在测试。因此,返回状态为关闭的主机。
  • 那么,您可以通过终端上的 ping 命令访问主机吗?您也可以尝试将主机更改为例如google.com?如果它适用于这些,那么问题肯定与上面的 nodejs 代码有关,而不是与基础设施有关。
猜你喜欢
  • 2013-06-16
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
相关资源
最近更新 更多