zabusa
zabusa

Reputation: 2719

creating recurring events in nodejs to update or insert MySQL table

I have a MySQL table tasks. In tasks, we can create a normal task or a recurring task that will automatically create a new task in the MySQL tasks table and send an email notification to the user that a task has been created. After a lot of research, I found out that you can do it in four methods

  1. MySQL events
  2. Kue, bull, agenda(node.js scheduling libraries)
  3. Using a cron job to monitor every day for tasks

the recurring tasks would be repeated over weekly, daily, monthly, and yearly. We must put an option to remove the recurring event at any time. What would be a nice and clean solution?

Upvotes: 3

Views: 5315

Answers (2)

Lucas Hendren
Lucas Hendren

Reputation: 2826

I had to do something very similar, you can use the npm module node-schedule

Node scheduler has many features. You can first create your rule setup, which determines when it runs and then schedules the job, which is where determine what the job performs and activates it, I have an example below from my code which sets a job to run at midnight every day.

 var rule = new schedule.RecurrenceRule();
 rule.dayOfWeek = [0, new schedule.Range(1, 6)];
   var j = schedule.scheduleJob(rule, function(){
     sqlUpdate(server);
 });

This may not exactly fit all of your requirements alone but there are other features and setups you can do.

For example you can cancel any job with the cancel function

j.cancel()

You can also set start times and end times like so as shown in the npm page

let startTime = new Date(Date.now() + 5000);
let endTime = new Date(startTime.getTime() + 5000);
var j = schedule.scheduleJob({ start: startTime, end: endTime, rule: '*/1 * * * * *' }, function(){
  console.log('Time for tea!');
});

There are also other options for scheduling the date and time as this also follows the cron format. Meaning you can set dynamic times

var j = schedule.scheduleJob('42 * * * *', function(){
  console.log();
});

As such this would allow node.js to handle everything you need. You would likely need to set up a system to keep track of the scheduled jobs (var j) But it would allow you to cancel it and schedule it to your desire.

It additionally can allow you to reschedule, retrieve the next scheduled event and you can have multiple date formats.

If you need to persist the jobs after the process is turned of and on or reset you will need to save the details of the job, a MySQL database would make sense here, and upon startup, the code could make a quick pull and restart all of the created tasks based on the data from the database. And when you cancel a job you just delete it from the database. It should be noted the process needs to be on for this to work, a job will not run if the process is turned off

Upvotes: 0

Elliot Blackburn
Elliot Blackburn

Reputation: 4164

As you've identified there are a number of ways of going about this, here's how I would do it but I'm making a number of assumptions such as how many tasks you're likely to have and how flexible the system is going forward.

If you're unlikely to change the task time options (daily, weekly, monthly, yearly). Each task would have the following fields last_run_date and next_run_date. Every time a task is run I would update these fields and create an entry in a log table such as task_run_log which will also store the date/time the task was run at.

I would then have a cron job which fires a HTTP message to a nodejs service. This web service would look through the table of tasks, find which ones need to be executed for that day and would dispatch a message for each task into some sort of a queue (AWS SQS, GCP Pub/Sub, Apache Kafka, etc). Each message in the queue would represent a single task that needs to be carried out, workers can subscribe to this queue and process the task themselves. Once a worker has processed a job it would then make the log entry and update the last_run_date and next_run_date fields. If a task fails it'll add it into move that message into an error queue and will log a failed task in the task log.

This system would be robust as any failed jobs would exist as failed jobs in your database and would appear in an error queue (which you can either drain to remove the failed jobs, or you can replay them into the normal queue when the worker is fixed). It would also scale to many tasks that have to happen each day as you can scale up your workers. You also won't be flooding cron, your cron job will just send a single HTTP request each day to your HTTP service which kicks off the processing.

You can also setup alerts based on whether the cron job runs or not to make sure the process gets kicked off properly.

Upvotes: 1

Related Questions