Automation

Creating Scheduled Tasks in Node.js Using node-cron

Learn to create and manage scheduled tasks in Node.js with the node-cron library, from cron expressions to running jobs inside a real project.

5 min read

Learn how to create and manage scheduled tasks in Node.js using the node-cron library. This comprehensive guide will walk you through the fundamentals of setting up cron jobs, configuring tasks with cron expressions, and solving common issues like multi-instance duplicate runs.

Prerequisites for Setting Up Node.js Cron Jobs

Before proceeding, ensure you meet the following requirements:

prerequisites

  • A Node.js development environment is already set up.
  • Node.js and npm are installed on your system.
  • Basic knowledge of JavaScript.
  • Familiarity with handling JSON files in Node.js.

Installing the node-cron Library

To manage scheduled tasks, you need to install the node-cron library. Use the following npm command:

Install node-cron

npm install node-cron
# Adds node-cron to package.json

Verify installation by confirming node-cron appears in your package.json dependencies.

Creating a Simple Scheduled Task

Follow these steps to set up a basic task scheduler:

steps

  1. Import the node-cron library
    Add the required package to your application using CommonJS syntax:

    javascript
    const cron = require('node-cron');
  2. Define a task function
    Create a simple function to log the current date and time:

    javascript
    const task = () => {
        console.log(`Running scheduled task at ${new Date().toISOString()}`);
    };
  3. Schedule the task
    Use a cron expression to run the task every minute:

    javascript
    cron.schedule('* * * * *', task);
  4. Test your task
    Run your script using Node.js and verify its output in the console:

    bash
    node index.js
    # Logs every minute

Understanding Cron Expressions

Cron expressions are patterns that define specific intervals for running scheduled tasks. The basic syntax comprises five fields, each representing a time unit:

* * * * *
- - - - -
| | | | |
| | | | └── Day of the week (0 - 7, Sunday=0/7)
| | | └──── Month (1 - 12)
| | └────── Day of month (1 - 31)
| └──────── Hour (0 - 23)
└────────── Minute (0 - 59)

Examples:

  • * * * * * — Run every minute.
  • */5 * * * * — Run every 5 minutes.
  • 0 9 * * * — Run daily at 9:00 AM.

Use Cases for Node.js Cron Jobs

Some practical applications for scheduled tasks include:

  • Archiving records: Automate archiving of paid invoices daily.
  • Housekeeping: Delete temporary files or outdated records.
  • Data manipulation: Perform regular data aggregation or backups.

Handling Multi-Instance Issues in Scheduled Tasks

Example: Archiving Paid Invoices

This example demonstrates archiving invoices with status: paid to a separate archive file.

steps

  1. Import necessary modules and read data
    Use CommonJS require and read JSON file synchronously:

    javascript
    const fs = require('fs');
    let invoices = JSON.parse(fs.readFileSync('./data/invoices.json', 'utf8'));
  2. Filter paid invoices
    Create a function to filter invoices with status: paid:

    javascript
    const archiveInvoices = () => invoices.filter(invoice => invoice.status === 'paid');
  3. Write paid invoices to the archive
    Save filtered data to a new file:

    javascript
    fs.writeFileSync('./data/archive.json', JSON.stringify(archiveInvoices(), null, 2), 'utf8');
  4. Update remaining invoices
    Remove archived invoices from the original data and update the JSON file:

    javascript
    invoices = invoices.filter(invoice => invoice.status !== 'paid');
    fs.writeFileSync('./data/invoices.json', JSON.stringify(invoices, null, 2), 'utf8');
  5. Schedule the task
    Import node-cron and run the archive job nightly:

    javascript
    const cron = require('node-cron');
    
    cron.schedule('0 0 * * *', () => {
        console.log('Archiving invoices...');
        fs.writeFileSync('./data/archive.json', JSON.stringify(archiveInvoices(), null, 2), 'utf8');
        invoices = invoices.filter(invoice => invoice.status !== 'paid');
        fs.writeFileSync('./data/invoices.json', JSON.stringify(invoices, null, 2), 'utf8');
    });

Example: Housekeeping Old Records

If you store large datasets, you may need to periodically delete records older than a defined period (e.g., 180 days).

steps

  1. Define date criteria
    Calculate the date threshold:

    javascript
    const dateThreshold = new Date();
    dateThreshold.setDate(dateThreshold.getDate() - 180);
  2. Import records and filter outdated ones
    Use CommonJS require and filter:

    javascript
    const fs = require('fs');
    let records = JSON.parse(fs.readFileSync('./data/records.json', 'utf8'));
    
    records = records.filter(record => new Date(record.date) >= dateThreshold);
    fs.writeFileSync('./data/records.json', JSON.stringify(records, null, 2), 'utf8');
  3. Schedule cleanup
    Import node-cron and automate the cleanup process:

    javascript
    const cron = require('node-cron');
    
    cron.schedule('0 2 * * *', () => {
        console.log('Running housekeeping...');
        let records = JSON.parse(fs.readFileSync('./data/records.json', 'utf8'));
        const dateThreshold = new Date();
        dateThreshold.setDate(dateThreshold.getDate() - 180);
        const filteredRecords = records.filter(record => new Date(record.date) >= dateThreshold);
        fs.writeFileSync('./data/records.json', JSON.stringify(filteredRecords, null, 2), 'utf8');
    });

Verification of Results

After running your scheduled tasks: IT_GUIDES_COMPONENT_6

Implementing External Scheduler for Stability

Use External Schedulers for Distributed Systems

In distributed environments, relying solely on node-cron for scheduling can lead to issues with task duplication. Opt for:

  • Task queues (e.g., Bull, Bee-Queue) for centralized job processing.
  • External schedulers (e.g., Redis, Airflow) to ensure precise execution timing.
  • Leader-election patterns to execute jobs on a single instance.

FAQ

How do I prevent duplicate cron jobs in multi-instance Node.js apps?

Use distributed locking mechanisms or external schedulers to avoid duplicate task execution across multiple instances.

What are practical uses for node-cron?

Common applications include data cleanup, backups, record archiving, and batch processing.

What happens if the Node.js process stops?

Scheduled tasks won't run if the Node.js process is terminated. To ensure reliability, use external schedulers or process monitoring tools.


Official reference: node-cron.