每10秒运行一次node.js脚本

 mobiledu2502925453 发布于 2023-01-30 12:57

我刚刚开始使用Node.js,我现在正试图让我的脚本每隔10秒在后台运行,就像一个守护进程等待某事做,当有从数据库运行的东西它从程序读取输出和某些任务取决于输出.

这是我迄今为止所能做到的,它的工作方式与我的预期相同,但即使在后台也只能运行一次.如何让它每隔10秒就像守护进程一样运行?

码:

var spawn = require('child_process').spawn;
var mysql = require('mysql');
var JSFtp = require('jsftp');
var check = require('node-validator').check;
var sanitize = require('node-validator').sanitize;

//Setup the db connection
var db = mysql.createConnection({
  host : 'db',
  port : 3306,
  database: 'db',
  user : 'db',
  password : 'db'
});

//Make the connection
db.connect(function(err){
    if(err != null) {
        res.end('Error connecting to mysql:' + err+'\n');
    }
});

var die = function(msg){
    console.error(msg);
    process.exit(1);
}

function ip2long ( ip_address ) {
    var output = false;
    var parts = [];
    if (ip_address.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) {
        parts  = ip_address.split('.');
        output = ( parts[0] * 16777216 +
        ( parts[1] * 65536 ) +
        ( parts[2] * 256 ) +
        ( parts[3] * 1 ) );
    }
    return output;
}


db.query("SELECT * FROM queue WHERE cooldown > UNIX_TIMESTAMP(NOW()) AND simulated=0 ORDER BY cooldown DESC LIMIT 1", function(err, rows){
    if(err != null){
        die("Query error: " + err);
    }

    if(rows < 1){
        die("No rows");
    }

    //Set the vars from the query
    var name = rows[0]['name'];
    var ip = rows[0]['ip'];
    var iterations = rows[0]['runs'];

    var bin = "/home/hoar/sum/run"
    var args = ['arg='+name, 'arg2='+iterations, 'path=/var/www/upload/'+name+'.html', 'output=log.log'];
    var proc = spawn(bin, args);
    var time = "/.*/";
    var pct = "/^\d/";
    var name = rows[0]['name'];
    var ip = rows[0]['ip'];
    var iterations = rows[0]['runs'];

    proc.stdout.setEncoding('utf8');
    proc.stdout.on('data', function(data) {
        var str = data.toString();
        var s = str.split("|");
        var p = s[0].split("/");
        var t = (s[1] == null) ? "" : s[1];

        if(p != null && s[0] != "@"){ //Needed to check for @ because the program prints this as first line, which is good then we can do the query further done only once.
            //Check the return numbers from simc to see how many sims it has done
            if(parseInt(p[0]) < parseInt(p[1])){ 
                //Check if the 6th match is a number and the 7th only contains letters
                if(t != null){ 
                    var time = t.replace(/(\r\n|\n|\r)/gm,""); //Remove any line disturbers for db

                    //Update the database with the amount of time left on the simulation
                    db.query("UPDATE `queue` SET `status`=" + db.escape(time) + " WHERE (`name`=" + name + ")");
                    //console.log(p[0]+"/"+p[1] + " - " + t + " left");
                }
                //console.log(p[0]+"/"+p[1] + " iterations done");
            }
        }else{
            //If the stdout is null run this query since we don't want to run this more than once.
            db.query("UPDATE `queue` SET `simulated`='2' WHERE (`name`=" + name + " AND simulated!='2')");
            //console.log("Updated db to 2");
        }
    });

    proc.stderr.on('data', function (data) {
        var str = data.toString();
        //If the program returns stderr we want to make sure it stops and we update the database to let the user know.
        if(str.indexOf("ERROR! Setup failure...")){ 

            //Update the database with the amount of time left on the simulation
            db.query("UPDATE `queue` SET `simulated`='3' WHERE (`name`=" + name + ")");

            //Kill the DB connection
            db.destroy(); 
            die("There was an error: " + data);
        }
    });

    proc.on('exit', function (code) {
    //Setup the ftp connection
    var ftp = new JSFtp({
      host: "ftp",
      port: 21,
      user: "ftp",
      pass: "ftp"
    });

        //Simulation ended with success update the database and kill.
        db.query("UPDATE `queue` SET `simulated`='1' WHERE (`name`=" + name + " AND simulated='2')");

        ftp.put('/var/www/upload/'+rows[0]['name']+'.html', 'public_html/mysite/'+ip2long(rows[0]['ip'])+'/'+rows[0]['name']+'.html', function(hadError) {
          if (!hadError)
            console.log("FTP error");

            ftp.raw.quit();
        });
        db.destroy(); 
        //die("Simulation is done");
    });

});//end sql

DrakaSAN.. 8

将您的数据库查询放入带回调的函数中,并使回调在10秒后再次触发该函数:

function mydbquery(callback) {
    db.query("SELECT * FROM queue WHERE cooldown > UNIX_TIMESTAMP(NOW()) AND simulated=0 ORDER BY cooldown DESC LIMIT 1", function(err, rows){
    if(err != null){
        die("Query error: " + err);
    }

    if(rows < 1){
        die("No rows");
    }

    //Set the vars from the query
    var name = rows[0]['name'];
    var ip = rows[0]['ip'];
    var iterations = rows[0]['runs'];

    var bin = "/home/hoar/sum/run"
    var args = ['arg='+name, 'arg2='+iterations, 'path=/var/www/upload/'+name+'.html', 'output=log.log'];
    var proc = spawn(bin, args);
    var time = "/.*/";
    var pct = "/^\d/";
    var name = rows[0]['name'];
    var ip = rows[0]['ip'];
    var iterations = rows[0]['runs'];

    proc.stdout.setEncoding('utf8');
    proc.stdout.on('data', function(data) {
        var str = data.toString();
        var s = str.split("|");
        var p = s[0].split("/");
        var t = (s[1] == null) ? "" : s[1];

        if(p != null && s[0] != "@"){ //Needed to check for @ because the program prints this as first line, which is good then we can do the query further done only once.
            //Check the return numbers from simc to see how many sims it has done
            if(parseInt(p[0]) < parseInt(p[1])){ 
                //Check if the 6th match is a number and the 7th only contains letters
                if(t != null){ 
                    var time = t.replace(/(\r\n|\n|\r)/gm,""); //Remove any line disturbers for db

                    //Update the database with the amount of time left on the simulation
                    db.query("UPDATE `queue` SET `status`=" + db.escape(time) + " WHERE (`name`=" + name + ")");
                    //console.log(p[0]+"/"+p[1] + " - " + t + " left");
                }
                //console.log(p[0]+"/"+p[1] + " iterations done");
            }
        }else{
            //If the stdout is null run this query since we don't want to run this more than once.
            db.query("UPDATE `queue` SET `simulated`='2' WHERE (`name`=" + name + " AND simulated!='2')");
            //console.log("Updated db to 2");
        }
    });

    proc.stderr.on('data', function (data) {
        var str = data.toString();
        //If the program returns stderr we want to make sure it stops and we update the database to let the user know.
        if(str.indexOf("ERROR! Setup failure...")){ 

            //Update the database with the amount of time left on the simulation
            db.query("UPDATE `queue` SET `simulated`='3' WHERE (`name`=" + name + ")");

            //Kill the DB connection
            db.destroy(); 
            die("There was an error: " + data);
        }
    });

    proc.on('exit', function (code) {
        //Setup the ftp connection
        var ftp = new JSFtp({
          host: "ftp",
          port: 21,
          user: "ftp",
          pass: "ftp"
        });

        //Simulation ended with success update the database and kill.
        db.query("UPDATE `queue` SET `simulated`='1' WHERE (`name`=" + name + " AND simulated='2')");

        ftp.put('/var/www/upload/'+rows[0]['name']+'.html', 'public_html/mysite/'+ip2long(rows[0]['ip'])+'/'+rows[0]['name']+'.html', function(hadError) {
          if (!hadError)
            console.log("FTP error");

            ftp.raw.quit();
        });
        db.destroy();

        //die("Simulation is done");
//NEW CODE!!! 
        callback();
//END OF NEW CODE
    });

});//end sql
}

//NEW CODE!!!
function wait10sec(){
    setTimeout(function(){
        mydbquery(wait10sec);
    }, 10000);
}

mydbquery(wait10sec);
//END OF NEW CODE

所以它会进行你的查询,然后在发射另一个之前等待10秒.

1 个回答
  • 将您的数据库查询放入带回调的函数中,并使回调在10秒后再次触发该函数:

    function mydbquery(callback) {
        db.query("SELECT * FROM queue WHERE cooldown > UNIX_TIMESTAMP(NOW()) AND simulated=0 ORDER BY cooldown DESC LIMIT 1", function(err, rows){
        if(err != null){
            die("Query error: " + err);
        }
    
        if(rows < 1){
            die("No rows");
        }
    
        //Set the vars from the query
        var name = rows[0]['name'];
        var ip = rows[0]['ip'];
        var iterations = rows[0]['runs'];
    
        var bin = "/home/hoar/sum/run"
        var args = ['arg='+name, 'arg2='+iterations, 'path=/var/www/upload/'+name+'.html', 'output=log.log'];
        var proc = spawn(bin, args);
        var time = "/.*/";
        var pct = "/^\d/";
        var name = rows[0]['name'];
        var ip = rows[0]['ip'];
        var iterations = rows[0]['runs'];
    
        proc.stdout.setEncoding('utf8');
        proc.stdout.on('data', function(data) {
            var str = data.toString();
            var s = str.split("|");
            var p = s[0].split("/");
            var t = (s[1] == null) ? "" : s[1];
    
            if(p != null && s[0] != "@"){ //Needed to check for @ because the program prints this as first line, which is good then we can do the query further done only once.
                //Check the return numbers from simc to see how many sims it has done
                if(parseInt(p[0]) < parseInt(p[1])){ 
                    //Check if the 6th match is a number and the 7th only contains letters
                    if(t != null){ 
                        var time = t.replace(/(\r\n|\n|\r)/gm,""); //Remove any line disturbers for db
    
                        //Update the database with the amount of time left on the simulation
                        db.query("UPDATE `queue` SET `status`=" + db.escape(time) + " WHERE (`name`=" + name + ")");
                        //console.log(p[0]+"/"+p[1] + " - " + t + " left");
                    }
                    //console.log(p[0]+"/"+p[1] + " iterations done");
                }
            }else{
                //If the stdout is null run this query since we don't want to run this more than once.
                db.query("UPDATE `queue` SET `simulated`='2' WHERE (`name`=" + name + " AND simulated!='2')");
                //console.log("Updated db to 2");
            }
        });
    
        proc.stderr.on('data', function (data) {
            var str = data.toString();
            //If the program returns stderr we want to make sure it stops and we update the database to let the user know.
            if(str.indexOf("ERROR! Setup failure...")){ 
    
                //Update the database with the amount of time left on the simulation
                db.query("UPDATE `queue` SET `simulated`='3' WHERE (`name`=" + name + ")");
    
                //Kill the DB connection
                db.destroy(); 
                die("There was an error: " + data);
            }
        });
    
        proc.on('exit', function (code) {
            //Setup the ftp connection
            var ftp = new JSFtp({
              host: "ftp",
              port: 21,
              user: "ftp",
              pass: "ftp"
            });
    
            //Simulation ended with success update the database and kill.
            db.query("UPDATE `queue` SET `simulated`='1' WHERE (`name`=" + name + " AND simulated='2')");
    
            ftp.put('/var/www/upload/'+rows[0]['name']+'.html', 'public_html/mysite/'+ip2long(rows[0]['ip'])+'/'+rows[0]['name']+'.html', function(hadError) {
              if (!hadError)
                console.log("FTP error");
    
                ftp.raw.quit();
            });
            db.destroy();
    
            //die("Simulation is done");
    //NEW CODE!!! 
            callback();
    //END OF NEW CODE
        });
    
    });//end sql
    }
    
    //NEW CODE!!!
    function wait10sec(){
        setTimeout(function(){
            mydbquery(wait10sec);
        }, 10000);
    }
    
    mydbquery(wait10sec);
    //END OF NEW CODE
    

    所以它会进行你的查询,然后在发射另一个之前等待10秒.

    2023-01-30 13:02 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有