3

在初始运行时,它会运行 app.js,但是会在间隔时跳过它。我在这里可能做错了什么?

var cron = require('node-cron');

cron.schedule('*/10 * * * * *', function(){
    var shell = require('./app.js');
     commandList = [
        "node app.js"
        ]

        console.log('Bot successfully executed'); 

});

日志:

ec2-user:~/environment $ node forever.js
Bot successfully executed
You have already retweeted this Tweet.
You have already retweeted this Tweet.
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
4

1 回答 1

2

我猜你app.js会立即执行代码,所以它只通过要求它来执行(并且要求被缓存,所以它只执行一次)。

您应该从您的app.js而不是返回函数并在每次运行时调用它。

// app.js
module.exports = () => { 
    // your original code here
};

// forever.js
var cron = require('node-cron');
var shell = require('./app.js');

cron.schedule('*/10 * * * * *', function(){
    shell();

    console.log('Bot successfully executed');
});

或者,也许您可​​以require在运行之前清除缓存:

delete require.cache[require.resolve('./app.js')]

于 2018-04-26T19:21:09.320 回答