I'm using the x-ray library to scrape a specific website for some content and save it in a JSON file.
I'd like to get the status every hour, but Node does not execute my setTimeout
.
This is my code:
var db, record, x;
x = require('x-ray')();
db = require('lowdb')('rec.json', {
storage: require('lowdb/file-async')
});
record = function() {
console.log('will contact server');
setTimeout(record, 60 * 60 * 1000);
x('http://example.com', 'h1')(function(err, content) {
console.log('server contacted');
if (err) {
console.log('error occurred');
db('status').push({
time: (new Date).toString(),
status: err
});
} else {
console.log('status recorded successfully');
db('status').push({
time: (new Date).toString(),
status: content
});
}
});
};
console.log('ready');
record();
It logs the first time without any issues, but after that, it does nothing. It seems that record
function is executed only once.
I'm using pm2
to manage the process, pm2 list
shows it's running well beyond 60 minutes. Even if Node is unable to execute the callback exactly in the given time, the process was alive (longest, before I restarted it) for around 80 minutes.
There are no errors in the logs and there were no unexpected restarts during my testing.
Maybe this has something to do with the libraries I'm using? I'm out of ideas what could be causing this.
Although not an exact solution, I'm posting this as an answer as it solves the issue, and also to close this question.
Ultimately, I solved this using cron
.