javascript 每 10 秒运行一次 node.js 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21461132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running a node.js script every 10 seconds
提问by user3246446
I just started using Node.js and I'm now trying to make my script run in the background every 10 seconds like a daemon waiting for something to do, when there is something to run from the database It reads the output from the program and does certain tasks depending on the output.
我刚刚开始使用 Node.js,现在我试图让我的脚本每 10 秒在后台运行一次,就像一个守护进程在等待某事做某事时,当有一些东西要从数据库运行时它读取程序的输出和根据输出执行某些任务。
This is what I've been able to do so far, It works just as I intended but can only run once even in the background. How can I make it run like a daemon every 10 seconds?
这是我到目前为止能够做的事情,它按我的预期工作,但即使在后台也只能运行一次。我怎样才能让它像守护进程一样每 10 秒运行一次?
Code:
代码:
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
回答by DrakaSAN
Put your db query in a function with callback, and make the callback fire the function again 10sec later:
将您的数据库查询放在带有回调的函数中,并在 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
So it will do your query, then wait 10sec before firing another.
所以它会做你的查询,然后在触发另一个之前等待 10 秒。
回答by Peter Lyons
Just have your program run continuously and use setTimeout
to re-execute the main logic on a timer. There is also setInterval
which is tempting but you risk starting a run before the prior run completes. Here's the basic pattern.
只需让您的程序连续运行并用于setTimeout
在计时器上重新执行主逻辑即可。还有setInterval
一些很诱人,但您可能会在前一次运行完成之前开始运行。这是基本模式。
function doMainStuff() {
//do all your stuff
lastAsyncThing(function (error) {
//When your final async thing is done, start the timer
if (error) {
//log error. Maybe exit if it's irrecoverable.
}
setTimeout(doMainStuff, 10 * 1000);
});
}
//when your program starts, do stuff right away.
doMainStuff();
回答by vodolaz095
run this script by forever package - https://npmjs.org/package/forever
通过永久包运行此脚本 - https://npmjs.org/package/forever
$ forever script.js
This fill run this script in background, detached from console.
此填充在后台运行此脚本,与控制台分离。
For 10 seconds timeuout you can use
您可以使用 10 秒超时
setInterval(function(){...}, 10*1000);
More info is there - http://www.w3schools.com/jsref/met_win_setinterval.asp
更多信息 - http://www.w3schools.com/jsref/met_win_setinterval.asp