在Node.js中创建网站正常运行时间监控器

时间:2018-08-27 23:19:09

标签: javascript node.js mongodb mongoose

我想使用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);

1 个答案:

答案 0 :(得分:0)

我会做这样的事情来处理数据库的更新。我继续使用标准箭头功能,因为那样对我来说更容易。我提出了一些意见,这样可以解决大多数问题。也许这不是最优雅的解决方案,因为我在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();
          });
        });
      });
    }
  });
}