Automating backend operations: An introduction to Cron jobs
A beginner's guide to the use of Cron Jobs
Backend development has since seen a lot of improvements and innovations of recent, such as automation, cloud support and others. In this article, we will be discussing Cron, what it entails, and its various use cases. We would also use Cron for a simple demo project.
By so doing, we intend to increase the knowledge base of the readers of this article and give them the basic tips needed to grasp all that Cron scheduling has to offer. This web article has been simplified and it’s useful for all node JS backend developers whether beginner, intermediate or experienced. Having discussed all that let's deep dive into Cron!!!.
Before we begin, here are some important prerequisites for this tutorial.
Basic Knowledge of Node JS
Basic Knowledge of NPM and NPM installation
What is Cron?
First of all, Cron originated from Chronos, a Greek word which literal’s meaning is Time. It simply works on the principle of periodically running a specific operation or groups of operation at a predefined date, time or interval. It is an ingrained execution in many home devices and appliances.
Cron principle is ingrained in several operations such as daily reminders, daily WhatsApp backups and many more. As stressed earlier, Cron execution can be done at specific time intervals, some of which include yearly, monthly, weekly, and daily intervals. “On reboot / restart “ is a non conventional interval that can be used. Having discussed the basics of Cron let’s move on to its use cases in backend development.
Cron Uses cases in Backend development
Backend is quite unique with its complexities and its operations. However, the apt understanding and the utilization of Cron operations in backend development is an added advantage to the developer. It helps in automating complex operations and enhances efficiency in backend development. Some of the significant operations in which Cron operations can be applied to in backend development include;
Scheduling mails
Automating determined interval backups
Implementing social notification features
Data Syncing
Subscription implementation
Email Marketing
Site monitoring and maintenance and many more.
Node JS Cron Packages
Node JS as a backend development tool supports the use of Cron operations. There are quite a lot of Cron-oriented packages that are available for use via the node package manager. Some of the popular ones include;
Cron
Node-cron
Cron-schedule
Node-schedule
Bree
Bottleneck
Bull
Agenda.
All of these tools are well suited for a wide variety of operations based on the user’s preference. However, they all have strengths in one aspect of Cron development compared to the others.
Node-schedule is more suited for time-based scheduling as opposed to interval-based scheduling. Cron scheduler can run even after the script isn’t running. Agenda and Bree schedulers can persist and still work even with system restarts.
In this article, we intend to use Node-schedule to demonstrate a Cron task.
A Demo Project
So far so good, we are at the meat of the subject matter. Now let’s go on to implement a demo Cron task with Node-scheduler. We intend to build an auto responder which would send a prompt to the front-end every 2 hours interval. So now, let’s begin. Firstly, lets install Node-schedule package. You can do so by typing npm i node-schedule
in your command prompt. Then you would receive a prompt that it has been successfully installed.
Thereafter, initialize a new Node JS backend project folder. After all these are completed, let’s get our hands dirty with code. In our server page, we would begin by importing and initializing node-schedule package.
After doing this, we would then move on to writing the code to be scheduled. This will be achieved by using the scheduleJob
function. We intend to display “Merry Christmas”
every 5 seconds.
schedule.scheduleJob('*/5 *', function() { console.log("Merry Christmas") })
The asterisk symbols are used to signify the time in terms of seconds, minutes, hours, dats, weeks , months etc. Your final code is expected to look like this.
Here is a demo of its execution.
Conclusion
So we have been able to harness the usefulness of testing cron scheduling in our web application. Additional features and other operations can be executed too via this package and the other ones listed above.
I sincerely hope you learnt something new and enjoyed this innovative tutorial. Feel free to check out my other articles here. Till next time, keep on coding.