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 --yes2npm 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")23const 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})1819class Weather {20 static getRec(date) {21 return this.find({22 recordedDate: {23 $lte: new Date(date),24 },25 }).exec()26 }2728 static insertBulkData(data) {29 return this.insertMany(data)30 }3132 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 }4647 static getArchivedData() {48 return this.find({49 isArchived: true,50 }).exec()51 }52}5354weatherSchema.loadClass(Weather)5556module.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.
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")78mongoose9 .connect("mongodb://localhost:27017/nodescheduler")10 .then(res => {11 console.log("mongoose connected successfully")1213 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 }2324 await model.insertBulkData(data)2526 res.send("Data is inserted")27 })2829 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 ago45 await model.archiveData(d)67 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.
Comments
Post a Comment