How To Schedule Jobs On NodeJS

How To Schedule Jobs On NodeJS

node-cron.png

In this article, we are going to learn how to schedule jobs that run periodically on NodeJS.
When building applications, we might come across certain tasks we want to perform monthly, yearly, or even per minute, and to achieve this we will be using an npm module called node-cron.

Step 1 (Set Up Your Project)

  • Create a folder called schedule-job-on-node and open it up on VSCode or any editor you use.
  • Run this command in your terminal npm init -y. Make sure you are in the project directory on the terminal.

The npm init command is going to create a package.json file in your project folder. This file contains meta-data about your project and should look like below. The -y flag accepts all default values for the file.

{
  "name": "schedule-job-on-node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Step 2 (Install Dependencies)

  • Run this command on your terminal npm i node-cron

    The npm i command will help us install the dependencies we declared after it. The node-cron module is what will be responsible for scheduling these tasks for us. These tasks can either be one-off or periodic.

Step 3 (Schedule The Job)

  • Create a file in your root directory called index.js.
  • Copy and paste the code below.
const cron = require('node-cron');

let count = 1;

cron.schedule('* * * * *', () =>
    console.log('node-cron scheduled this. This has run ' + count++ + ' times'),
);

In the code above we import the node-cron module to use in our project using require('node-cron') and we store it in a variable for easy access. We then create a variable count that increments every time our scheduled job runs. We then use the method .schedule() provided to us by node-cron to schedule a job that runs every minute and prints out to the console node-cron scheduled this. This has run 1 times'. The first parameter of the method takes in the schedule and the second takes in the task to be performed.

Running The Code

Go to your terminal and run this command node .\index.js to run your code.
On your terminal you should see

node-cron scheduled this. This has run 1 times
node-cron scheduled this. This has run 2 times
node-cron scheduled this. This has run 3 times
node-cron scheduled this. This has run 4 times
node-cron scheduled this. This has run 5 times

Setting Schedule Patterns On node-cron

The first parameter passed to the .schedule() method in the example above is '* * * * *'. This means schedule the job to run every minute. But how do we schedule jobs to run per year or per hour? To do this we need to understand the cron syntax which is basically how to communicate this information to node-cron.

 # ┌────────────── second (optional)
 # │ ┌──────────── minute
 # │ │ ┌────────── hour
 # │ │ │ ┌──────── day of month
 # │ │ │ │ ┌────── month
 # │ │ │ │ │ ┌──── day of week
 # │ │ │ │ │ │
 # │ │ │ │ │ │
 # * * * * * *

Accepted values in each field

  • second - 0-59
  • minute - 0-59
  • hour - 0-23
  • day of month - 1-31
  • month - 1 - 12 or month in words
  • day of week - 0-7 (or names, 0 or 7 are sunday)

    Thank you for reading, let's connect!

    Here is my Twitter
    And Instagram
    And Linkedin