Run Cron Jobs after the Node server restarts (vola

2019-08-25 01:42发布

I use the cron library in NPM.

I want to make events for users, that create jobs that are expected to be executed in a month after the event.

Let's say that this is my code:

function userSignedUpEvent(user) {
  var today = new Date();
  today.setDate(today.getDate() + 30);

  let cronSignature = `${today.getMinutes()} ${today.getHours()} ${today.getDate()} ${today.getMonth()} *`
  let reminderJob = new CronJob(cronSignature,
    () => {
      user.sendEmail("It has been a month since you joined us!");
    }, null, true, "Asia/Jerusalem");
}

The problem is that if the server is shut-down at the exact time of the cron-signature, the code will never be executed.

Is there a way in my case to save these jobs and sign them again whenever the server is up again?

Theoretically, I can save their signatures in my Database and implement that by myself, but I want to be sure there isn't a driven method to do it.

2条回答
女痞
2楼-- · 2019-08-25 02:14

As you mentioned, you'd have to record it as "done" in some way.

I don't believe the cron package provides any way to do that directly.

There are a number of ways. If you are already using a database which has user-specific data, that would definitely be the most logical place to store it.

Another option would be to store it in a flat file of some sort.

Another approach could be to have a queue that would read jobs off as it completes them.

As the users sign up, you would add a record for them to the bottom of the queue, probably along with a timestamp since there is substantial delay until it is run. This would mean the queue would be in chronological order, with the soonest item at the top, latest at the bottom.

The tech behind the queue would be somewhat irrelevant. It could be as simple as a plaintext file, or something sophisticated like a Kafka queue

Then your program could look at the first line of the queue every so often. If that item is ready to go, do whatever you need to do, then remove it from the queue. If not, just leave it alone and check it again in a little while.

This way, the item is still in the queue until it is done, and even if your program is shutdown when it's time to fire, it'll still fire when it starts back up.

查看更多
对你真心纯属浪费
3楼-- · 2019-08-25 02:15

So I've implemented this in this project: crone-durability

查看更多
登录 后发表回答