繁体   English   中英

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

[英]create a website uptime monitor in Node.js

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM