【问题标题】:create a website uptime monitor in Node.js在 Node.js 中创建网站正常运行时间监视器
【发布时间】:2018-08-27 23:19:09
【问题描述】:

我想使用 NodeJS 和 MongoDB 创建一个正常运行时间监视器。我想在 NodeJS 中运行一个 cron 作业并将数据存储到 MongoDB 中。如果网站响应状态码不等于200,则将其保存在数据库中。我想做一个这样的数据库条目,

url : http://www.google.com
status_code : 500
start_time :- start time
end_time :- end time

我可以运行 cron 作业,但不确定如何在数据库中保存停机时间。因为,我不想将每个响应都存储到数据库中。只有当响应状态码不是200时,才会开始跟踪(start_time)URL,并将网站返回200的时间保持为end_time

cron.js:-

var async=require('async');
const Entry = require('../models/health.model.js');

var https = require('https');
var request = require('request');

module.exports = function getHttpsRequests () {

    Entry.find({},function(err,entrys){
      console.log(err);
      if(!err && entrys){

         async.each(entrys,function(entry,callback){


           request(entry.url, function (error, response, body) {
            entry.statuscheck=response.statusCode;
            entry.save();
            callback();
            });


         },function (error) {

         });

      }
    });

}

health.model.js:-

const mongoose = require('mongoose');

const EntrySchema = mongoose.Schema({
    url: String,
    statuscheck: String
}, {
    timestamps: true
});

module.exports = mongoose.model('Entry', EntrySchema);

【问题讨论】:

  • 您为什么要这样做而不是使用现有的正常运行时间监视器?我的意思不是对抗性的。我的意思是尝试从现有解决方案中提取您正在尝试做的事情的不同之处。

标签: javascript node.js mongodb mongoose


【解决方案1】:

我会做这样的事情来处理更新数据库。我继续将标准箭头函数放入其中,因为那样对我来说更容易。我放了一些cmets,这样可以解决大多数问题。它可能不是最优雅的解决方案,因为我在 5 分钟内写了它,但如果您遵循这个一般逻辑流程,您应该更接近您的解决方案(请注意,它完全未经测试。)

var async=require('async');
const Entry = require('../models/health.model.js');

var https = require('https');
var request = require('request');

module.exports = function getHttpsRequests () {
  Entry.find({}, (err,entrys) => {
    console.log(err);
    if (!err && entrys) {
      async.each(entrys, (entry,callback) => {
        request(entry.url, (error, response, body) => {
          //first check if the url has a document in the db.
          Entry.find({ url: entry.url }, (err, entry) => {
            if(!entry) {
              //since the document does not exist, check the statusCode.
              if(response.statusCode===200) { //if the statusCode is 200, continue the loop.
                callback();
              } else { //if the status code is not 200, lets save this to the db.
                console.log("Saving object: " + entry)
                entry.status_code = response.statusCode;
                entry.start_time = new Date();
                entry.save();
                callback();
              }
            } else if (entry) {
              //since the document exists, lets check the statusCode.
              if(response.statusCode===200) { //if the statusCode is 200, update the stop_time.
                entry.end_time = new Date();
                Entry.findOneAndUpdate({ url: entry.url }, entry, (err, object) => { //this returns the entry after update, so we can put that in the console for easy debug.
                  if (err) {
                    console.log(err);
                    callback();
                  } else {
                    console.log("Object saved: " + object);
                    callback();
                  }
                });
              }
            } else { //there was an error finding the document in the db, just go to the next one.
              callback();
          });
        });
      });
    }
  });
}

【讨论】:

  • 谢谢。我明白了。
猜你喜欢
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多