如何添加嵌套节点调度程序

How to add nested node-scheduler

假设我有 100,000 条用户记录。我想使用 nodejs node-mailer.

在 10:00 PM 向他们发送每日工作提醒

如果我一次查询所有这些并发送活动作业,它会给出 Javascript heap out of memory。所以我决定按照以下步骤操作。

  1. 下午 10:00 开始。
  2. 每 15 分钟为 1000 位用户发送电子邮件,直到所有用户都完成。
  3. 每天都一样。

所以我决定开始嵌套node-scheduler第一个调度程序 应该在 10:00 下午开始,嵌套调度程序 应该 运行 每 15 分钟一次。

  schedule.scheduleJob("*/2 * * * *", async () => {
    console.log("Parent --------every two minutes---------------------------");
    let count= 1;
    schedule.scheduleJob("*/5 * * * * *", async () => {
      console.log("Nested ------------every 5 seconds-----------------------", ++count);

    });
  });

以上代码的结果:

Tue, 09 Apr 2019 06:40:10 GMT  Nested ------------------- every five second---------------- 3
Tue, 09 Apr 2019 06:40:15 GMT  Nested ------------------- every five second---------------- 4
Tue, 09 Apr 2019 06:40:20 GMT  Nested ------------------- every five second---------------- 5
Tue, 09 Apr 2019 06:40:25 GMT  Nested ------------------- every five second---------------- 6
Tue, 09 Apr 2019 06:40:30 GMT  Nested ------------------- every five second---------------- 7
Tue, 09 Apr 2019 06:40:35 GMT  Nested ------------------- every five second---------------- 8
Tue, 09 Apr 2019 06:40:40 GMT  Nested ------------------- every five second---------------- 9
Tue, 09 Apr 2019 06:40:45 GMT  Nested ------------------- every five second---------------- 10
Tue, 09 Apr 2019 06:40:50 GMT  Nested ------------------- every five second---------------- 11
Tue, 09 Apr 2019 06:40:55 GMT  Nested ------------------- every five second---------------- 12
Tue, 09 Apr 2019 06:41:00 GMT  Nested ------------------- every five second---------------- 13
Tue, 09 Apr 2019 06:41:05 GMT  Nested ------------------- every five second---------------- 14
Tue, 09 Apr 2019 06:41:10 GMT  Nested ------------------- every five second---------------- 15
Tue, 09 Apr 2019 06:41:15 GMT  Nested ------------------- every five second---------------- 16
Tue, 09 Apr 2019 06:41:20 GMT  Nested ------------------- every five second---------------- 17
Tue, 09 Apr 2019 06:41:25 GMT  Nested ------------------- every five second---------------- 18
Tue, 09 Apr 2019 06:41:30 GMT  Nested ------------------- every five second---------------- 19
Tue, 09 Apr 2019 06:41:35 GMT  Nested ------------------- every five second---------------- 20
Tue, 09 Apr 2019 06:41:40 GMT  Nested ------------------- every five second---------------- 21
Tue, 09 Apr 2019 06:41:45 GMT  Nested ------------------- every five second---------------- 22
Tue, 09 Apr 2019 06:41:50 GMT  Nested ------------------- every five second---------------- 23
Tue, 09 Apr 2019 06:41:55 GMT  Nested ------------------- every five second---------------- 24
Tue, 09 Apr 2019 06:42:00 GMT  Nested ------------------- every five second---------------- 25
Tue, 09 Apr 2019 06:42:00 GMT  Parent -----------------every two minute------------------
Tue, 09 Apr 2019 06:42:05 GMT  Nested ------------------- every five second---------------- 2
Tue, 09 Apr 2019 06:42:05 GMT  Nested ------------------- every five second---------------- 26
Tue, 09 Apr 2019 06:42:10 GMT  Nested ------------------- every five second---------------- 27
Tue, 09 Apr 2019 06:42:10 GMT  Nested ------------------- every five second---------------- 3
Tue, 09 Apr 2019 06:42:15 GMT  Nested ------------------- every five second---------------- 4
Tue, 09 Apr 2019 06:42:15 GMT  Nested ------------------- every five second---------------- 28
Tue, 09 Apr 2019 06:42:20 GMT  Nested ------------------- every five second---------------- 29
Tue, 09 Apr 2019 06:42:20 GMT  Nested ------------------- every five second---------------- 5
Tue, 09 Apr 2019 06:42:25 GMT  Nested ------------------- every five second---------------- 6
Tue, 09 Apr 2019 06:42:25 GMT  Nested ------------------- every five second---------------- 30
Tue, 09 Apr 2019 06:42:30 GMT  Nested ------------------- every five second---------------- 31
Tue, 09 Apr 2019 06:42:30 GMT  Nested ------------------- every five second---------------- 7

问题,嵌套调度程序 两分钟后没有重新启动。

解决方案,嵌套调度器应该每两分钟重启一次。

我不知道该怎么做。

如有任何解决方案,我们将不胜感激。

发生这种情况是因为您每 15 分钟创建一个新作业而没有停止旧作业。您可以在开始新工作之前简单地停止旧工作。

let oldJob = null;

schedule.scheduleJob("*/2 * * * *", async () => {

    console.log("Parent --------every two minutes---------------------------");
    let count = 1;

    if (oldJob) {
        oldJob.cancel();
    }

    oldJob = schedule.scheduleJob("*/5 * * * * *", async () => {
        console.log("Nested ------------every 5 seconds-----------------------", ++count);
    });
});