I just started using Node.js, and now I'm trying to make my script run in the background every 10 seconds, like a daemon waiting for something when there is something to start from the database. reads the output from the program and performs certain tasks depending on the output.
This is what I have been able to do so far. It works as I expected, but can only work once even in the background. How can I make it work like a daemon every 10 seconds?
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;
var db = mysql.createConnection({
host : 'db',
port : 3306,
database: 'db',
user : 'db',
password : 'db'
});
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");
}
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] != "@"){
if(parseInt(p[0]) < parseInt(p[1])){
if(t != null){
var time = t.replace(/(\r\n|\n|\r)/gm,"");
db.query("UPDATE `queue` SET `status`=" + db.escape(time) + " WHERE (`name`=" + name + ")");
}
}
}else{
db.query("UPDATE `queue` SET `simulated`='2' WHERE (`name`=" + name + " AND simulated!='2')");
}
});
proc.stderr.on('data', function (data) {
var str = data.toString();
if(str.indexOf("ERROR! Setup failure...")){
db.query("UPDATE `queue` SET `simulated`='3' WHERE (`name`=" + name + ")");
db.destroy();
die("There was an error: " + data);
}
});
proc.on('exit', function (code) {
var ftp = new JSFtp({
host: "ftp",
port: 21,
user: "ftp",
pass: "ftp"
});
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();
});
});
source
share