Search code examples
javascriptnode.jsswitch-statementschedulingnode-cron

NodeJS schedule (start/stop) multiple tasks


I have a HTML slider with 4 positions. 15, 30, 45, 60 "minutes". Depending on the position I want to execute some code every selected minutes. So I decides to implement a switch case and use node-cron (or node-schedule) to achieve this.

But I am struggling with the cron jobs. So if I start with every 30 minutes and than switch to e.g. 15 minutes there are 2 cron jobs running and so on. I experimented with .stop() end .start() (or .cancel()) but without success.

Can anyone please give me a hint to achieve my goal? Maybe there is a totally different solution...

Thanks so far

const { ipcRenderer } = require("electron");
const cron = require('node-cron');

var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value;

slider.oninput = function () {
  output.innerHTML = this.value;
  time();
};

function time() {
    const slidertime = document.getElementById("myRange").value

  switch (parseInt(slidertime)) {
    case 15:
        console.log("every 15 min");
        pattern = '0 */15 * * * *' 
      break;
    case 30:
        console.log("every 30 min");
        pattern = '0 */30 * * * *'
      break;
    case 45:
        console.log("every 45 min");
        pattern = '0 */45 * * * *'
      break;
    case 60:
        console.log("every 60 min");
        pattern = '0 */60 * * * *'
      break;
  }

  const task = cron.schedule(pattern, function () {
    ipcRenderer.send("asynchronous-message", `foo`);
  });
}
time();

Solution

  • I found a working solution. I guess this is kind of messy code but it works :) (refactoring is coming)

    What I've done is to declare the CronJobs globally and in the switch statement I start and stop the Jobs. I tried to declare the CronJobs inside the function hydrate_time() but it wont work.

    const { ipcRenderer } = require("electron");
    const cron = require("node-cron");
    
    const hy_15 = cron.schedule(
      "0 */15 * * * *",
      function () {
        ipcRenderer.send("asynchronous-message", `get hydrate`);
      },
      {
        scheduled: false,
      }
    );
    const hy_30 = cron.schedule(
      "0 */30 * * * *",
      function () {
        ipcRenderer.send("asynchronous-message", `get hydrate`);
      },
      {
        scheduled: false,
      }
    );
    const hy_45 = cron.schedule(
      "0 */45 * * * *",
      function () {
        ipcRenderer.send("asynchronous-message", `get hydrate`);
      },
      {
        scheduled: false,
      }
    );
    const hy_60 = cron.schedule(
      "0 */60 * * * *",
      function () {
        ipcRenderer.send("asynchronous-message", `get hydrate`);
      },
      {
        scheduled: false,
      }
    );
    
    var slider = document.getElementById("myRange");
    var output = document.getElementById("demo");
    output.innerHTML = slider.value;
    
    slider.oninput = function () {
      output.innerHTML = this.value;
      hydrate_time();
    };
    
    function hydrate_time() {
      let slidertime = document.getElementById("myRange").value;
    
      switch (parseInt(slidertime)) {
        case 15:
          console.log("get hydrate 15 min");
          hy_30.stop();
          hy_15.start();
          break;
        case 30:
          console.log("get hydrate 30 min");
          hy_15.stop();
          hy_45.stop();
          hy_30.start();
          break;
        case 45:
          console.log("get hydrate 45 min");
          hy_30.stop();
          hy_60.stop();
          hy_45.start();
          break;
        case 60:
          console.log("get hydrate 60 min");
          hy_45.stop();
          hy_60.start();
    
          break;
        default:
        // code block
      }
    }
    hydrate_time();