Node.js

Scheduling jobs on Node.js with node-schedule

Batching is a great part of todays software development. The business world runs on batch from bank statements to promotion emails.

Node.js has some good libraries for such cases.

Node Schedule is a light cron like scheduler for node.

npm install node-schedule

In case your are used to cron and the cron expression format, it will be pretty easy for you.

var scheduler = require('node-schedule');
 
var montlyJob  = scheduler.scheduleJob('0 0 1 * *', function(){
  console.log('I run the first day of the month');
});

But you also have a javascript object approach

var scheduler = require('node-schedule');

var rule = new scheduler.RecurrenceRule();
rule.hour = 7
rule.dayOfWeek = new schedule.Range(0,6)
 
var dailyJob = schedule.scheduleJob(date, function(){
  console.log('I run on days at 7:00');
});

scheduler.scheduleJob(rule,task);

Also you can have tasks submitted by giving a date

var scheduler = require('node-schedule');

var date = new Date(2017, 1, 1, 0, 0, 0);
var newYearJob = scheduler.scheduleJob(date, function() {
    console.log("Happy new year");
});

However in case your job is not needed you can cancel it pretty easy

newYearJob.cancel();
Reference: Scheduling jobs on Node.js with node-schedule from our WCG partner Emmanouil Gkatziouras at the gkatzioura blog.

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button