0

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)

Surya Mahla
  • 483
  • 5
  • 20
  • Just setting a schedule as 1 run per an hour isn't suitable? – Anatoly Dec 12 '20 at 10:04
  • If I only set 1 job and put it in setInterval with 1hr time interval, it will stack the jobs after every hour which passes. So, if my job is set for 5pm and it is currently 12pm, then at 5pm, 5 jobs will be executed. I figured that if I name each job in my student's array dynamically, like jobStudent1,jobStudent2 in place of job1 for every student and then cancel it and reschedule it, it will cancel all previous jobs with that name if it exists and schedule a new one. But now I am stuck with how do I name my job1 variable dynamically. Any help with that will solve the issue. – Surya Mahla Dec 12 '20 at 10:18
  • Please change your question according to your current issue – Anatoly Dec 12 '20 at 10:19

0 Answers0