Getting Started Task Scheduling and Cron Jobs in Node.js
Scheduling tasks in a Node.js application can be crucial for automating emails, generating reports, and more. In this post, we'll explore how to set up a robust scheduling system using the Pulse library, integrated with an Express server. This approach uses MongoDB to manage tasks, ensuring durability and resilience.
Setting Up Your Environment
First, ensure you have Node.js and MongoDB installed on your system. Then, create a new Node.js project and install the necessary packages:
npm init -y
npm install express mongodb pulse --save
Integrate Pulse with Express
Here’s a step-by-step guide to integrating Pulse with an Express application for task scheduling:
1. Import Dependencies and Setup Express
Begin by importing required modules and setting up the Express server:
import express from 'express';
const app = express();
const port = 3000;
2. Configure Pulse
Set up Pulse to connect to MongoDB, where jobs will be managed:
const mongoConnectionString = 'mongodb://localhost:27017/pulse';
const pulse = new Pulse({
db: { address: mongoConnectionString, collection: 'cronjob' },
defaultConcurrency: 4,
maxConcurrency: 4,
processEvery: '10 seconds',
resumeOnRestart: true
});
3. Define Jobs
Create job definitions. For instance, a job to send emails and another to generate reports:
pulse.define('send nudge email', async (job, done) => {
const { to } = job.attrs.data;
// Add email sending logic here
done();
}, { shouldSaveResult: true, attempts: 5, backoff: { type: 'exponential', delay: 1000 }});
pulse.define('send weekly report', async (job, done) => {
const { to } = job.attrs.data;
// Add report generation logic here
done();
}, { shouldSaveResult: true, attempts: 5, backoff: { type: 'exponential', delay: 1000 }});
4. Express Routes to Trigger Jobs
Implement routes to schedule emails and reports:
app.post('/send-nudge-email', async (req, res) => {
await pulse.start();
const job = pulse.create('send nudge email', { to: req.body.to });
await job.schedule(new Date(Date.now() + 259200000)).save(); // 3 days later
res.status(200).send('Nudge email scheduled successfully');
});
app.post('/send-weekly-report', async (req, res) => {
await pulse.start();
const job = pulse.create('send weekly report', { to: req.body.to });
await job.repeatEvery('1 week').save();
res.status(200).send('Weekly report scheduled successfully');
});
5. Event Listeners
Add listeners for various job events:
pulse.on('success', (job) => {
console.log(time(), `Job <${job.attrs.name}> succeeded`);
});
6. Start the Server
Launch your Express server:
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
Conclusion
By integrating Pulse with Express and MongoDB, you can effectively manage scheduled tasks in your Node.js application. This setup is not only scalable but also resilient, thanks to MongoDB's robustness and Pulse’s efficient job handling capabilities. With this system, you can automate routine tasks efficiently and ensure your application remains performant and reliable.
👉 For the complete code and further details, please refer to the GitHub repository
👉 Learn more about Pulsecron's event-based scheduling solutions