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.jsonVerify 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
Import the node-cron library
Add the required package to your application using CommonJS syntax:javascriptconst cron = require('node-cron');Define a task function
Create a simple function to log the current date and time:javascriptconst task = () => { console.log(`Running scheduled task at ${new Date().toISOString()}`); };Schedule the task
Use a cron expression to run the task every minute:javascriptcron.schedule('* * * * *', task);Test your task
Run your script using Node.js and verify its output in the console:bashnode 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
Import necessary modules and read data
Use CommonJSrequireand read JSON file synchronously:javascriptconst fs = require('fs'); let invoices = JSON.parse(fs.readFileSync('./data/invoices.json', 'utf8'));Filter paid invoices
Create a function to filter invoices withstatus: paid:javascriptconst archiveInvoices = () => invoices.filter(invoice => invoice.status === 'paid');Write paid invoices to the archive
Save filtered data to a new file:javascriptfs.writeFileSync('./data/archive.json', JSON.stringify(archiveInvoices(), null, 2), 'utf8');Update remaining invoices
Remove archived invoices from the original data and update the JSON file:javascriptinvoices = invoices.filter(invoice => invoice.status !== 'paid'); fs.writeFileSync('./data/invoices.json', JSON.stringify(invoices, null, 2), 'utf8');Schedule the task
Importnode-cronand run the archive job nightly:javascriptconst 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
Define date criteria
Calculate the date threshold:javascriptconst dateThreshold = new Date(); dateThreshold.setDate(dateThreshold.getDate() - 180);Import records and filter outdated ones
Use CommonJSrequireand filter:javascriptconst 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');Schedule cleanup
Importnode-cronand automate the cleanup process:javascriptconst 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.