3

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?

Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33
zabusa
  • 2,520
  • 21
  • 25
  • Is there particular additional information you are looking for form the other answers? – Lucas Hendren Feb 18 '19 at 16:11
  • Yes... I need to know when using bull or agenda if something happen to the queues the recurrent task will work or not? It's an additional info that I need to know – zabusa Feb 19 '19 at 11:01

2 Answers2

1

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.

Elliot Blackburn
  • 3,759
  • 1
  • 23
  • 42
  • to use I don't want to use cron jobs anymore. because managing crons will be again an overhead.can you suggest any of the bull or agenda libraries. – zabusa Feb 12 '19 at 13:11
  • There's a node cron module if you'd rather use nodejs code but somewhere you have to have a configuration telling your system when you check for tasks to run. Here's one I've used before which works well https://www.npmjs.com/package/cron – Elliot Blackburn Feb 12 '19 at 13:20
  • @i don't want to lose my job when process is offline. I think when using node-cron it will not execute the process is over – zabusa Feb 12 '19 at 13:44
  • You have to have a process running for this to work, be it nodejs, cron, or mysql (using mysql events). If you don't wnat to use cron, and don't want to use nodejs, you're really left with mysql events. – Elliot Blackburn Feb 12 '19 at 18:11
  • we can use a queue system to make work when it offline also right?...when process live then queue will send pending messages to the process – zabusa Feb 13 '19 at 04:30
  • Yes that's right, that was the intention of adding in a queue system. But you will still need something that kicks off and queue up the messages and workers will need to come alive to service the messages somehow. – Elliot Blackburn Feb 13 '19 at 09:48
0

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

Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33