How to use the Node.js Schedule module to create timing tasks

By lmartinez, 24 January, 2022
nodejs
Unsplash

 

Node.js, as they define it on their website, is an "event-driven asynchronous JavaScript runtime, Node.js framework is designed to build scalable network applications." Among the different functions and applications, it presents the Node.js Schedulers, precisely our focus in this blog.

 

Task scheduling, or Jobs in this case, refers to the process of triggering a job, task, or function at a predetermined time or scheduling it for certain events. Most task schedulers are based on cron, the time-based job scheduler on Unix-like systems.

 

There are several Node.js schedulers suitable for this runtime. This time, we are going to learn how to use the node-schedule for timing tasks.

 

Node.js Scheduler: ideal for scheduling tasks

 

First, start with the installation:

 

Command: npm install node-schedule

 

To use it in a date-based scheduling method, the time is first determined, for example November 21, 2017, at 5:30

 

var schedule = require('node-schedule');
var date = new Date(2017, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(){
    console.log('The world is going to end today.');
});
 j.cancel(); // Cancel the preset plan

 

Then the fixed minutes per hour

 

var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
rule.minute = 42;
var j = schedule.scheduleJob(rule, function(){
    console.log('The answer to life, the universe, and everything!');
});

 

Then, you have to schedule the task at a certain time on some days of the week, for example, every Thursday, Friday, and Saturday at 5:00 p.m.

 

var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(4, 6)];
rule.hour = 17;
rule.minute = 0;
var j = schedule.scheduleJob(rule, function(){
    console.log('Today is recognized by Rebecca Black!');
});

 

And we run every second

 

var rule = new schedule.RecurrenceRule();
var times = [];
for(var i=1; i<60; i++){
times.push(i);
}
rule.second = times;
var c=0;
var j = schedule.scheduleJob(rule, function(){
     c++;
      console.log(c);
});

 

With this, you finish installing the node-schedule in your application with the Node.js environment.

 

What makes Node.js framework so special?

 

Node.js framework is easy to use, especially for people who are new to this field. It is especially used to develop agile prototypes. It is also used to build fast and scalable services, used for companies like PayPal, Uber, Walmart. Node.js developers will feel comfortable with this tool, no matter how experienced they are.

 

A special case occurred at PayPal, they rebuilt one of their Java-based applications using Node, discovering that the latter developed twice as fast with fewer developers involved, 33% fewer lines of code, and 40% fewer files. They even doubled the number of requests served per second.

 

At Rootstack, our expert developers have made use of this technology to solve several of our clients' technological problems. If you consider yourself an expert in Node.js, you can be part of this team, we are constantly looking for talent to help us create the applications and websites of the present and the future.

 

We recommend you on video


 

Thumbnail
Image
e-commerce
Weight
6
Hero
Title
How to use the Node.js Schedule module to create timing tasks
Image
Image
Software Consulting Services
Text Color
White
Text Alignment
Left
Size
Medium
Overlay effect
Hide overlay effect
Date
Sidebar
CTA