Implementing Job Schedulers in Node.js

In this article, we will see what are job schedulers and how to implement the job schedulers in nodejs. Implementing Job Schedulers in Node.js

A Recent article on Nodejs,

PM2 for Node.js developers

Building P2P Video Chat Application using webRTC and Node.js

Understanding EventEmitter in Node.js With a UseCase

Ever wondered how the application server back up the files periodically without any physical interruption. This is where Cron jobs come in.

Cron Jobs schedules a job periodically to do the actions that are configured to do.

there are few use cases where cron jobs play a vital role. they are,

  • Deleting Log files - Application generates a lot of logs.clearing old logs will save lots of space in the server. it can be done using cron jobs.
  • DB Backup - Database backup saves the application from disasters. Cron job will be helpful to do that.
  • Application Logic - we can use cron jobs to do some application logic on a time basis.

How cron job works

we will write a cron job to archive the old records in the database in a production application.

Firstly, create a project and install the following dependencies,

1npm init --yes
2npm install express node-cron mongoose faker
  • express - web server library in nodejs
  • node-cron - cron job scheduler library in nodejs
  • mongoose - ORM for MongoDB

After that, create a file called Model.js and add the following code

1const mongoose = require("mongoose")
2
3const weatherSchema = new mongoose.Schema({
4 minTemp: {
5 type: Number,
6 },
7 maxTemp: {
8 type: Number,
9 },
10 recordedDate: {
11 type: Date,
12 },
13 isArchived: {
14 type: Boolean,
15 default: false,
16 },
17})
18
19class Weather {
20 static getRec(date) {
21 return this.find({
22 recordedDate: {
23 $lte: new Date(date),
24 },
25 }).exec()
26 }
27
28 static insertBulkData(data) {
29 return this.insertMany(data)
30 }
31
32 static archiveData(date) {
33 return this.updateMany(
34 {
35 recordedDate: {
36 $lte: new Date(date),
37 },
38 },
39 {
40 $set: {
41 isArchived: true,
42 },
43 }
44 ).exec()
45 }
46
47 static getArchivedData() {
48 return this.find({
49 isArchived: true,
50 }).exec()
51 }
52}
53
54weatherSchema.loadClass(Weather)
55
56module.exports = mongoose.model("Weather", weatherSchema)

Mainly, Model.js creates a mongoose schema for a DB table which stores the weather data in the database.

After that, create a file called scheduler.js and add the code for job scheduler.

1cron.schedule("* * * * * *", () => {
2 console.log("Running every minute")
3})

cron schedule schedules the job for time format that is mentioned.

nodeschduler

To learn more about cron job format, there is a great site crontab-guru which explains in detail

Connect the mongoose with the Express to insert some dummy data to database.

1const cron = require("node-cron")
2const express = require("express")
3const mongoose = require("mongoose")
4const app = express()
5const faker = require("faker")
6const model = require("./Model")
7
8mongoose
9 .connect("mongodb://localhost:27017/nodescheduler")
10 .then(res => {
11 console.log("mongoose connected successfully")
12
13 app.get("/insertdata", async (req, res) => {
14 let data = []
15 for (let i = 0; i < 100; i++) {
16 let record = {
17 minTemp: faker.random.number(),
18 maxTemp: faker.random.number(),
19 recordedDate: faker.date.past(),
20 }
21 data.push(record)
22 }
23
24 await model.insertBulkData(data)
25
26 res.send("Data is inserted")
27 })
28
29 app.listen(4000, () => {
30 console.log("Server is running port 4000")
31 })
32 })
33 .catch(err => {
34 console.error(err)
35 })

To insert some dummy data using fakerjs. run the script with the command, and visit the URL http://localhost:4000/insertdata

1node scheduler.js

it will create some bulk dummy data to test the job scheduler. Now it is time to add the job scheduler.

1cron.schedule("* * * * */3 *", async () => {
2 var d = new Date()
3 d.setMonth(d.getMonth() - 2) //1 month ago
4
5 await model.archiveData(d)
6
7 console.log("scheduler => archived")
8})

Above cron job will run every 3 months, to mark the data as archived in the database.

Likewise, we can use cron jobs to schedule a job for our application logics.

Summary

Above all, cron jobs play a vital role in some application development use cases. it is always good to know how cron jobs work in application development.

Reference : https://crontab.guru

To Read More

Kubernetes for Nodejs developers

Do you keep hearing the word kubernetes in the tech community and you couldn't u...

TypeScript Interfaces vs Types

In this article, we will see what are interfaces and types and the difference be...

How to find project ideas to practi...

Ever wondered what how to get a real world experience on web development while w...

Building a Production-grade Nodejs,...

This article is the first part of building a production grade nodejs,graphql and...

Modern React Redux Tutorials with R...

This tutorial explain how you can build an application using modern react redux ...

Building a Piano with React Hooks

In this article, we will see how to build a piano with react hooks. Building a P...

TypeScript Basics - The Definitive ...

In this article, we will learn some basics of typescript which helps you to deve...

Here's why podman is more secured t...

In this article we will see about podman and why it is more secured way to run c...

What is gRPC ? How to implement gRP...

Everyone talks about gRPC. Have you ever wonder how it works or how to implement...