Chris
Chris

Reputation: 993

How to make a code execute continuously every 15 minutes?

Hey guys I am running a web server on node + express and for some reason my code isn't executing every 10 minutes even though I clearly have the setInterval function set there. This is just one of the 4 codes I have set in my script file (called serverScripts.js) right next to app.js and I ran it with both node serverScripts.js and used forever module on it but it still doesn't work

var APOD = (function() {
    setInterval(function() {
        async.waterfall([
            function Request(callback) {
                let apodUrl = 'https://api.nasa.gov/planetary/apod?api_key=';
                let api_key = '*censored*';
                request(apodUrl+api_key, function(err, apodData) {
                    if (err) throw err;
                    apodData = JSON.parse(apodData.body);
                    callback(null, apodData);
                });
            },
            function GetTableItems(apodData, callback) {
                let apodParams = { TableName: "APOD" }
                db.scan(apodParams, (err,apodTable) => {
                    if (err) throw err;
                    callback(null, apodData, apodTable);
                });
            },
            function CheckUniques(apodData, apodTable, callback) {
                let tempArr = [];
                for (let i = 0; i < apodTable.Items.length; i++) {
                    tempArr.push(apodTable.Items[i].title);
                }
                let IsItThere = _.includes(tempArr, apodData.title);
                if (!IsItThere) {
                    setTimeout(function() {
                        let putParams = {
                            TableName: 'APOD',
                            Item: {
                                "title": apodData.title,
                                "date": apodData.date,
                                "description": apodData.explanation,
                                "hdurl": apodData.hdurl,
                                "media_type": apodData.media_type,
                                "url": apodData.url,
                                "copyright": apodData.copyright
                            }
                        }
                        db.put(putParams, (err) => {
                            if (err) throw err;
                            console.log(`\nAdded APOD item: ---"${apodData.title}"--- to the database.\n`);
                        });
                    },1000);
                }
                else { console.log("No new APOD items."); }
                callback(null);
            }
            ]);
        APOD;
    },600000);
}());

Upvotes: 3

Views: 1959

Answers (1)

Shekhar Tyagi
Shekhar Tyagi

Reputation: 1674

i think according to your question cron is the best match , you can set cron after every 15 minutes . https://www.npmjs.com/package/node-cron .

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

cron.schedule('0 0/15 * 1/1 * ? *', function(){
  console.log('running a task every 15 minutes');
});

Upvotes: 1

Related Questions