nightmarejsでcronを実行することはできません。Cronとnightmarejs
get_data()関数の最初の反復はうまく動作しますが、その後はcronが再起動され、関数は再びトリガされません。
また、「クロール終了」は記録されません。
私のコードで何が間違っているかご存知ですか?
ログ
1
cron
data fetched
2
cron
3
cron
-
var Nightmare = require('nightmare')
var nightmare = Nightmare({
typeInterval: 300,
show: true,
executionTimeout: 120000,
gotoTimeout: 120000
});
let data = ""
-
var get_data = function(){
return new Promise(function(resolve, reject) {
nightmare
.goto('https://url.com')
.type('[name=email]', '')
.wait(1000)
.type('[name=email]', 'myemail')
.wait(1000)
.type('[name=password]', '')
.wait(1000)
.type('[name=password]', 'mypassword')
.click('[type=submit]')
.wait(5000)
.goto('https://url.com')
.wait(25000)
.evaluate(function (page, done) {
return document.body.innerText
done()
})
.end()
.then(function (result) {
data = result
})
.then(function(data){
return fs.writeFile("./data.txt", data, function(err) {
if(err) {
console.log(err)
reject(err)
}
resolve(data)
});
})
.catch(function(error){
reject(error)
})
})
}
-
var i = 0
var job = new CronJob('0 */20 * * * *', function() {
++i
console.log(i)
console.log("cron")
get_data()
}, function() {
console.log("crawl ended")
},
true
);
job.start();