0

I am using the NodeJS Agenda Scheduler and I wish to know a few things before going to production.

There are 3 scenarios

  1. A User called Peter wants to schedule a Message to be sent by 5PM on the Second of July 2022

  2. Another User called Mary wants to schedule a Message to be sent by 10PM on the 15th of August 2022.

For every new request to schedule a new message to be sent in the future I am creating a new Job for it using the function below

defineNewJob(messageId, jobDate) {
    let jobName = `send_${messageId}`;
    try {
        agenda.define(jobName, jobHandler);
        await agenda.schedule(jobDate, jobName, { message_id: messageId, removeOnCompleted: true });
    } catch (e) {
        console.log(`Error Scheduling Job`);
    }
}

This means that I might end up with over 1 million messages from different users all to be scheduled in the future at completely different times and stored in the MongoDB database

  1. I noticed that when I restart my server, Agenda has lost all I scheduled so I need to read back the data from my database and call the function above again with each of the returned messages from my database. Is this an efficient way to do things? I don't think so.

Please currently the scheduling works flawlessly at the specified dates and time. I'm only concerned about the efficiency of this in production with a lot of users scheduling all kinds of messages at different dates and times.

So, Is this fine in production or do I need to make changes?

ololo
  • 1,326
  • 2
  • 14
  • 47
  • how frequently does your server get restarted? – Rinkesh P May 26 '22 at 09:51
  • Whenever I make a change on my server code and push to heroku it will get restarted by heroku – ololo May 26 '22 at 09:55
  • I would say that any solution wont be straightforward. One thing that could be done is setting up a buffer mechanism such that your code directly doesn't create a job rather it pushes to the buffer, and another process which consumes from the buffer and creates the job, once the job is done, you remove that entry from the buffer This would atleast ensure that you aren't hitting the db everytime you restart. And of course, you will have to setup it seperately from your main code, else it would also lose the data when the server restarts. – Rinkesh P May 26 '22 at 10:02

0 Answers0