What's an effective way of running CRON jobs in your NodeJS application?

What's an effective way of running CRON jobs in your NodeJS application?

In Software Development, we often have to automate certain processes, such as sending an email to a customer if he or she has not paid for a certain service by the end of the month.

Previously, without a robust information system, you would have people manually verifying, making a list and communicating with the respective customers.


Nowadays, you can do this process in batches, and an effective way of doing this - is by using cron jobs - requiring no human intervention at all. Another means is having an User Interface (UI) with several options, which can be used, particularly if the precise timing is unknown.

A batch is a group of things or people dealt with at the same time or considered similar in type [1]
The software utility cron also known as cron job1 is a time-based job scheduler in Unix-like computer operating systems. Users that set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. [2]

The first time I used cron jobs was several years ago using the application Cron Tab for archiving data after a certain point in time.

For NodeJS, there are plenty of resources freely available on the net for doing batches. I am using node-cron, which is easy to install and use, if you already have expressed installed on your server.


Once done, you can simply run your tasks in batches through the crons.

What is my strategy?

There are two strategies:

  1. the crons are used separately
  2. they are included in the apps

The first option is excellent for jobs that rely on calling different applications. For example, deleting a set of log files every week. You don't need to include these codes in your application; unless really needed or the logic can be shared.

I prefer including cron jobs in the server side applications so that I can reuse the existing classes, methods and logic. For instance, imagine having to rewrite the logic for a multi-tenant database, when it's already present and well tested. I just need to call the classes and use the methods.


[1] https://dictionary.cambridge.org/dictionary/english/batch

[2] https://en.wikipedia.org/wiki/Cron