How to reschedule jobs in setInterval node-scheduler?

441 views Asked by At

I am trying to run a scheduled jobs process in a setInterval method but unable to implement it correctly.

What I am trying to do is, I have a list of students whom I need to send email at a date. The content of method might change (I've mentioned below), so I have to run this job in an interval of 1hour so that if something updates in the DB, it reschedules the job with updated data.

I have to assign jobs for each student with same date. But here, the jobs are not getting rescheduled. They are stacking on top of each other.

Example: If I run the server for 30s now, it will schedule jobs for each student 6 times giving me a total of 6*3 = 18 jobs. What I want is just 3 jobs for 3 students.

const schedule = require("node-schedule");

setInterval(() => {
  ["student1", "student2", "student3"].forEach((student) => {
    const job1 = schedule.scheduleJob(date1, function () {
      console.log("notification for: " + student);
      // Data in here might change
    });
  });
}, 5000);

Package used: node-schedule (https://www.npmjs.com/package/node-schedule)

0

There are 0 answers