Node-Cron: how to ensure last job is complete

Josh J

I am using node-cron to send operational emails (currently searching for new emails that need to be sent every minute).

My function (operationalEmails) looks for mongodb records that have a sent flag = false, sends the email, then changes the sent flag = true.

If an iteration of the cron has a large payload of records to send, it could take more than a minute.

How do I ensure the last iteration of the cron is complete before starting a new one?

//Run every Min
cron.schedule("* * * * *", () => {
  operationalEmails();
});
Chris

you would need to create a simple lock

const running = false;

function operationalEmails() {

   if (running) {
     return
   }

   running = true;
   // do stuff
   running = false;
}

//Run every Min
cron.schedule("* * * * *", () => {
  operationalEmails();
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事