Reset timer for already running cron-node job

Viewed 812

I need to run a scheduler every 5 min which will do few http calls . After first time I want to start the next timer only when all the http calls are finished. How can I do this cannot find the solution on 'node-cron' documentation. This is how I am trying to do.

const CronJob = require('cron').CronJob;
const axios = require("axios");

const job = new CronJob('* */5 * * * *', function() {
    console.log(new Date());
  calls();
},null,true);


function calls(){
  job.stop();
  axios.get("url").then((res)=>{
    console.log(res.data);
    job.start();
  })
}
1 Answers

probably axios doesn't give any specific api as per your need but you can create a flag which turns on before making axios.get() and resets on getting response. Inside cron job Skip your task if flag is on, else call(), sample:

const CronJob = require('cron').CronJob;
const axios = require("axios");
var isRunning = false;
const job = new CronJob('* */5 * * * *', function() {
  console.log(new Date());
  if(!isRunning) calls();
},null,true);


function calls(){
  job.stop();
  isRunning = true;
  axios.get("url").then((res)=>{
    isRunning = false
    console.log(res.data);
    job.start();
  })
}
Related