Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Richard Wynn
Richard Wynn

Posted on

     

3 Task Scheduling Packages for Node.js

⏳ 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
Enter fullscreen modeExit fullscreen mode

Examples

var cron = require('node-cron');cron.schedule('* * * * *', () => {  console.log('running a task every minute');});
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

Examples

const schedule = require('node-schedule');const job = schedule.scheduleJob('42 * * * *', function(){  console.log('The answer to life, the universe, and everything!');});
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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');
Enter fullscreen modeExit fullscreen mode

For Typescript, Webpack or other module imports, useagenda/es entrypoint:

import{Agenda}from'agenda/es';
Enter fullscreen modeExit fullscreen mode

NOTE:

  • If you're migrating from@types/agenda you also should change imports toagenda/es.
  • Instead ofimport 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");})();
Enter fullscreen modeExit fullscreen mode
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",});})();
Enter fullscreen modeExit fullscreen mode
(asyncfunction(){constweeklyReport=agenda.create("send email report",{to:"example@example.com",});awaitagenda.start();awaitweeklyReport.repeatEvery("1 week").save();})();
Enter fullscreen modeExit fullscreen mode

👋 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

👋 Hi, I'm Richard Wynn, a Senior Full-stack Software Engineer with a joy of Coding, Writing and Sharing knowledge
  • Joined

More fromRichard Wynn

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp