DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. Scheduling Jobs on Node.js With node-schedule

Scheduling Jobs on Node.js With node-schedule

Check out this great hands-on tutorial for scheduling jobs in Node. It's perfect if you've ever needed cron-like functionality in this single threaded language.

Emmanouil Gkatziouras user avatar by
Emmanouil Gkatziouras
CORE ·
May. 07, 16 · Tutorial
Like (5)
Save
Tweet
Share
34.85K Views

Join the DZone community and get the full member experience.

Join For Free

Batching is a great part of today's 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

If your are used to cron and the cron expression format, it will be pretty easy.

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

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

But, there's also 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);

And, 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 the case that your job is not needed you can cancel it pretty easy:

newYearJob.cancel();
job scheduling Node.js

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • RabbitMQ vs. Memphis.dev
  • Agile Scrum and the New Way of Work in 2023
  • Kubernetes vs Docker: Differences Explained
  • 3 Ways That You Can Operate Record Beyond DTO [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: