Cron

                Never    
if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);
  require("./config/mongoose.config");

  const emailArray = [
    "siddharththakur1800@gmail.com",
    "siddharththakur416@gmail.com",
    "siddharth.webosmotic@gmail.com",
    "sidthakur142@gmail.com",
  ];

  const batchSize = 2;
  const emailBatches = [];
  const mailProcesses = [];

  for (let i = 0; i < emailArray.length; i += batchSize) {
    emailBatches.push(emailArray.slice(i, i + batchSize));
  }

  for (let i = 0; i < numCPUs; i++) {
    const worker = cluster.fork();
    mailProcesses.push(worker);
    worker.send({ emailBatch: emailBatches[i] });
  }
  cluster.on("exit", (worker) => {
    console.log(`Worker ${worker.process.pid} died`);
    cluster.fork();
  });

  app.get("/stop", (req, res, next) => {
    mailProcesses.forEach((process) => {
      process.send("shutdown");
    });
    res.send("Stopped");
  });
} else {
  let emailData;
  const task = cron.schedule(
    "*/10 * * * * *",
    function () {
      if (!emailData) return;
      sendMail(emailData);
    },
    {
      scheduled: false,
    }
  );

  process.on("message", (message) => {
    if (message.emailBatch) {
      emailData = message.emailBatch;
      task.start();
    }
  });
  process.on("message", (message) => {
    if (message === "shutdown") {
      process.exit(0);
    }
  });

  app.get("/", (req, res, next) => {
    res.send("Welcome to the page!");
  });

  app.use((req, res, next) => {
    next(createError.NotFound("Please enter valid URL!"));
  });

  app.use(errorHandler);
  app.listen(PORT, () => {
    console.log(`Worker ${process.pid} started`);
  });
}

Raw Text