Batch Jobs in Node.JS


Recently, I was working on my Node.js backend program and I was looking for a batch job kind of implementation. Most of my backend program was in Node.js, so I decided that my batch jobs should also be in Node.js. Knowing this, I started searching for batch jobs in Node.js.

Surprisingly, I came across a node package ‘cron’, at I was mainly looking for very simple implementation for my batch jobs. I quickly looked into the documentation and found it simple. I implemented the same and great things just happened, i.e., my batch jobs are running without any interruptions in my Azure environment for the last 2 months. So, I decided to share the basic use of ‘cron’ with all the developers.

Basic Cron Usage

Typically, for a batch job, we need some process to run based on some schedule. Using the ‘Cron’ package, we will define one of our NodeJS function to be executed based on schedule. ‘Cron’ helps us in defining which function to be executed and on what schedule.

Following steps will tell you how to use Cron for your batch jobs in the simplest way.

  1. Creating a basic Node.js program - mybatchjobs

    Let's start by creating a folder in our local drive to store our batch job program using command prompt,

    mkdir mybatchjobs

    cd mybatchjobs

    Assuming we have Node.js and NPM installed, let's create and initialize our package.json, which will store our Node.js program - mybatchjobs dependencies and definitions.

    npm init

    Once you go through the npm init options (which are very easy to follow), you'll see something like this,

  1. Installing a Cron package

    Now, we will install a cron package using below command,

    npm install cron --save

  1. Implementing a Cron

    Now, create a JavaScript file – index.js and start implementing batch jobs using cron.

    Our index.js can be seen as below,

    1. var CronJob = require('cron').CronJob;  
    2. new CronJob('* * * * * *'function() {  
    3.     console.log('This is a test batch job started at ' + new Date());  
    4. }, nulltrue'');  

    The above code can be explained as,

    Code line var CronJob = require('cron').CronJob;  includes ‘cron’ module for our program and exports the functions needed to run a cron job.

    1. new CronJob('* * * * * *'function() {  
    2.     console.log('This is a test batch job started at ' + new Date());  
    3. }, nulltrue'');  

    The above lines instantiate function CronJob and runs a function for every second and displays a message on console.

    Constructor CronJob() has following parameters,

    • cronTime – This is the time when to execute the job. This is in JS Date object
    • onTick – This is the parameter which accepts which function needs to be executed. In above code snippet we are just printing date and time on console
    • onComplete – This parameter is optional. Once job is complete this will get executed. We have not provided this in the above code snippet
    • start – We are specifying as true, to run the job immediately
    • timeZone – This we are passing as ‘’. This specifies timezone for the job execution
  1. Running our program

    With above code, now let’s run our program using the below command,

    node index.js

    You can notice, our batch job is running every second now. We can see console entries show for every second. This is mainly because of function CronJob() accepts parameter to set the scheduling for the job. In current case, first parameter to function CronJob() is CronTime and value is set as ‘* * * * * *’, this tells cron to run a job every second.

  1. Cron Ranges and their values

    Like in the earlier example, we have seen jobs running for every second. When parameter is , this job will run for every second. This way we can set the values for CronJobs for their scheduling.

    Possible parameter values can be seen as below,

    • Seconds: 0-59
    • Minutes: 0-59
    • Hours: 0-23
    • Day of Month: 1-31
    • Months: 0-11
    • Day of Week: 0-6

    Let’s consider some below examples and their scheduling parameters,

Cron Job Examples and Their Parameter Values

Example 1. Run a job for every 10 second

CronTime values we need to set as, '*/10 * * * * *'

Output of this setting can be seen as,

Example 2. Run a job on 4:00 PM every day

CronTime values we need to set as, '00 00 16 * * *'

Output of this setting can be seen as,

Example 3. Run a job on 11th day of every month at specific time

CronTime values we need to set as, '00 03 16 11 * *'. This job will be executed on the 11th day of every month and at 16:03:00. Since this is for a specific time, this will run only once and then will get executed next month at same time.

Code can be seen as,

  1. var CronJob = require('cron').CronJob;  
  2. new CronJob('00 03 16 11 * *'function() {  
  3.     console.log('This is a test batch job started at ' + new Date());  
  4. }, nulltrue'');  

Output of this setting can be seen as,

This way you can use ‘Cron’ package for batch scheduling using Node.JS. I recommend you to go ahead,  try this, and play with possible combinations of CronTime parameter. I am already using it to run not just one job but multiple jobs with multiple instances without any issues so far.

Up Next
    Ebook Download
    View all
    View all