Skip to main content

Implementing Job Schedulers in Node.js

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.

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...