DEV Community

Cover image for Getting Started Task Scheduling and Cron Jobs in Node.js with MongoDB
SH-Hong
SH-Hong

Posted on • Updated on • Originally published at pulsecron.com

Getting Started Task Scheduling and Cron Jobs in Node.js with MongoDB

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

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;
Enter fullscreen mode Exit fullscreen mode

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
});

Enter fullscreen mode Exit fullscreen mode

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 }});
Enter fullscreen mode Exit fullscreen mode

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

5. Event Listeners

Add listeners for various job events:

pulse.on('success', (job) => {
  console.log(time(), `Job <${job.attrs.name}> succeeded`);
});
Enter fullscreen mode Exit fullscreen mode

6. Start the Server

Launch your Express server:

app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

Implementing and managing such a system yourself is necessary but can be tedious and time-consuming. To focus more on product engineering, consider using a cloud service like Pulsecron for your scheduling needs. Pulsecron offers a managed solution that simplifies the entire process, allowing you to concentrate on building great features for your users.

πŸ‘‰ For the complete code and further details, please refer to the GitHub repository

πŸ‘‰ Learn more about Pulsecron's event-based scheduling solutions

πŸ‘‰ Join our open source project related to this topic

Top comments (0)