⏳ Task scheduling allows you to schedule your code to be executed at a scheduled date/ time, at recurring intervals, or once after a specified interval. In Linux, this is often handled by packages likecron
. In this article, I will show you top 4 task scheduling packages that emulate cron-like functionality for Node.js apps.
Node Cron
Thenode-cron
module is tiny task scheduler in pure JavaScript for node.js based on GNU crontab. This module allows you to schedule task in node.js using full crontab syntax.
Popularity
- 1.449.775 Weekly Downloads (Up to the time of this article)
Installation
You can installnode-cron
by usingnpm
.
$ npm install --save node-cron
Examples
var cron = require('node-cron');cron.schedule('* * * * *', () => { console.log('running a task every minute');});
Node Schedule
Node Schedule is a flexible cron-like and not-cron-like job scheduler for Node.js. It allows you to schedule jobs (arbitrary functions) for execution at specific dates, with optional recurrence rules. It only uses a single timer at any given time (rather than reevaluating upcoming jobs every second/ minute).
Popularity
- 1.941.398 Weekly Downloads (Up to the time of this article)
Installation
You can installnode-schedule
by usingnpm
.
$ npm install node-schedule
Examples
const schedule = require('node-schedule');const job = schedule.scheduleJob('42 * * * *', function(){ console.log('The answer to life, the universe, and everything!');});
Agenda
Agenda is a light-weight job scheduling library for Node.js that offers:
- Minimal overhead. Agenda aims to keep its code base small.
- Mongo backed persistence layer.
- Promises based API.
- Scheduling with configurable priority, concurrency, and repeating.
- Scheduling via cron or human readable syntax.
- Event backed job queue that you can hook into.
Popularity
- 55.774 Weekly Downloads (Up to the time of this article)
Installation
npm
You can installagenda
by usingnpm
.
$ npm install agenda
You will also need a workingMongo database (v3) to point it to.
CJS / Module Imports
For regular javascript code, just use the default entrypoint
constAgenda=require('agenda');
For Typescript, Webpack or other module imports, useagenda/es
entrypoint:
import{Agenda}from'agenda/es';
NOTE:
- If you're migrating from
@types/agenda
you also should change imports toagenda/es
. - Instead of
import Agenda from 'agenda'
useimport Agenda from 'agenda/es'
.
Examples
constmongoConnectionString="mongodb://127.0.0.1/agenda";constagenda=newAgenda({db:{address:mongoConnectionString}});// Or override the default collection name:// const agenda = new Agenda({db: {address: mongoConnectionString, collection: 'jobCollectionName'}});// or pass additional connection options:// const agenda = new Agenda({db: {address: mongoConnectionString, collection: 'jobCollectionName', options: {ssl: true}}});// or pass in an existing mongodb-native MongoClient instance// const agenda = new Agenda({mongo: myMongoClient});agenda.define("delete old users",async(job)=>{awaitUser.remove({lastLogIn:{$lt:twoDaysAgo}});});(asyncfunction(){// IIFE to give access to async/awaitawaitagenda.start();awaitagenda.every("3 minutes","delete old users");// Alternatively, you could also do:awaitagenda.every("*/3 * * * *","delete old users");})();
agenda.define("send email report",{priority:"high",concurrency:10},async(job)=>{const{to}=job.attrs.data;awaitemailClient.send({to,from:"example@example.com",subject:"Email Report",body:"...",});});(asyncfunction(){awaitagenda.start();awaitagenda.schedule("in 20 minutes","send email report",{to:"admin@example.com",});})();
(asyncfunction(){constweeklyReport=agenda.create("send email report",{to:"example@example.com",});awaitagenda.start();awaitweeklyReport.repeatEvery("1 week").save();})();
👋 And you? Have you ever used any of these packages or another packages to do cron stuff in Node.js? Feel free to put a comment below 😃
📱 Keep in Touch
If you like this article, don't forget to follow and stay in touch with my latest ones in the future by following me via:
Twitter:https://twitter.com/RichardWynn01
Medium:https://richard-wynn.medium.com
Github:https://github.com/richard-wynn
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse