nodejs:检查URL到达在线状态

时间:2017-02-09 19:43:49

标签: node.js infinite-loop

我需要一个“forever-watchdog”来检查URL是否可以访问。我的情况是我必须查看媒体流是否上线,如果,我需要运行shell脚本。如果无法访问(404'd),我会在x秒内再次尝试。

有人可以引导我进入正确的方向,模块或流动吗?我在使用

解析目的地时遇到了问题
isReachable = require('is-reachable');

模块。

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

您可以重复向URL发送请求。像这样:

//I don't know how you create nodejs server, I just leave out that part.
//I use request module.  sudo npm install --save request
var request = require('request');
var URL = 'https://www.google.com';
var X = 5000; //5 seconds
var sendRequest = function(){
  request(URL, function(error, response, body){
    if(response.statusCode === 200){//If good
      runScript(); //This is the function you write to run sh
    }
  setTimetout(sendRequest, X);//run the function again no matter what
  });       
}
sendRequest();

如果你想要一个更好的永久循环,我建议你使用诺言。

var rp = require('request-promise');
var options = {
  uri : 'https://www.google.com',
  json : true
};
var X = 5000; //5 seconds, the actual time is 5 seconds plus the time to resolve
var sendRequest_promise = function(){
  //return a request promise
  return rp(options).then(function(response){
    return response;
  });
};
var sendRequest = function(){
  sendRequest_promise().then(function(response){
    if(response.statusCode === 200){
      runScript();
    }
    setTimeout(sendRequest(), X);
  });
};

答案 1 :(得分:0)

试试这个,应该解决你的任务



'use strict';
const co = require('co');
const request = require('request');
const exec = require('child_process').exec;

let getRemoteSourceStatusPromise = function(urlForCheck){
  return new Promise(function (resolve, reject) {
      request(urlForCheck, function(error, response){
          console.log('send request...');
          if(error)
              return reject(error);

          let result = false;
          console.log('Resource response statusCode ' + response.statusCode);
          if(200 === response.statusCode){
            result = true;
          }
          return resolve(result);
      });
  });  
};
let callShScriptPromise = function(){
    let shScriptPath = './script.sh';
    return new Promise(function (resolve, reject) {
        console.log('Calling sh...');
        exec(shScriptPath,
            function (error) {
                if (error)
                   return reject(error);
                resolve(true);
            });
    });
};
let sleep = function (sleepInterval) {
    return new Promise(function (resolve) {
        setTimeout(function () {
            console.log('sleep...');
            resolve();
        }, sleepInterval);
    });
};
let loopGenerator = function* (urlForCheck, sleepInterval){
    let bShScriptStarted = false;
    while(true){
        let bSourceStatus = yield getRemoteSourceStatusPromise(urlForCheck);
        if(bSourceStatus === true){
            if(!bShScriptStarted)
                bShScriptStarted =  yield callShScriptPromise();
        }else{
            bShScriptStarted = false;
        }
        yield sleep(sleepInterval);
    }
};


//Starting loop
co(loopGenerator('http://google.com', 30000))
    .catch(function (error) {
        console.log(error);
    })




答案 2 :(得分:0)

const rp = require('request-promise');

const checkIfOnline = (() -> { 
    return new Promise((resolve, reject)=>{
        rp( url ).then( response => {
            // this is where you run your script
            resolve();
        }).catch(error=>{
            setTimeout( resolve(checkIfOnline()), 5000 );
        });
    });
})();