From c329bb89519043bc7ab5a7f13f720d18a14d6238 Mon Sep 17 00:00:00 2001 From: Muhammad Ashhub Ali Date: Thu, 14 Dec 2023 20:07:05 +0500 Subject: [PATCH] Added Cron Job Scheduling --- .../workflows/AutomaticChanges.yml | 0 EventBus/index.js | 28 ++++++++-------- .../controllers/DailyUsageController.js | 24 +++++++++++++- UsageMntrServ/index.js | 32 ++++++++++++++++--- UsageMntrServ/package-lock.json | 32 +++++++++++++++++++ UsageMntrServ/package.json | 2 ++ 6 files changed, 98 insertions(+), 20 deletions(-) rename {github => .github}/workflows/AutomaticChanges.yml (100%) diff --git a/github/workflows/AutomaticChanges.yml b/.github/workflows/AutomaticChanges.yml similarity index 100% rename from github/workflows/AutomaticChanges.yml rename to .github/workflows/AutomaticChanges.yml diff --git a/EventBus/index.js b/EventBus/index.js index 8c71535..6b0d67e 100644 --- a/EventBus/index.js +++ b/EventBus/index.js @@ -28,23 +28,21 @@ app.post("/events", async (req, res) => { }); } - axios.post("http://localhost:4001/events", formData, { - headers: formData.getHeaders(), - }).then(() => { - res.send({ status: "OK" }); - }); + const headers = formData.getHeaders(); - axios.post("http://localhost:4002/events", formData, { - headers: formData.getHeaders(), - }).then(() => { - res.send({ status: "OK" }); - }); + await axios.post("http://localhost:4001/events", formData, { + headers, + }) - axios.post("http://localhost:4003/events", formData, { - headers: formData.getHeaders(), - }).then(() => { - res.send({ status: "OK" }); - }); + await axios.post("http://localhost:4002/events", formData, { + headers, + }) + + await axios.post("http://localhost:4003/events", formData, { + headers, + }) + + res.send({ status: "OK" }); } catch (err) { console.log(err.message); diff --git a/UsageMntrServ/controllers/DailyUsageController.js b/UsageMntrServ/controllers/DailyUsageController.js index 4eed958..f1f0b92 100644 --- a/UsageMntrServ/controllers/DailyUsageController.js +++ b/UsageMntrServ/controllers/DailyUsageController.js @@ -41,8 +41,30 @@ const addNewDailyUsageInstance = async (req, res) => { res.status(500).json({ message: "Internal server error." }); } +} + +const resetDailyLimit = async (req, res) => { + try { + const dailyUsages = await DailyUsage.find(); + + if (!dailyUsages) { + return res.status(404).json({ message: "Daily usage not found." }); + } + + dailyUsages.forEach(async (dailyUsage) => { + dailyUsage.usedBandwidth = 0; + await dailyUsage.save(); + }); + + res.status(200).json({ message: "Daily usage reset." }); + } catch (error) { + console.error(error); + res.status(500).json({ message: "Internal server error." }); + } + } module.exports = { getDailyUsageById, - addNewDailyUsageInstance + addNewDailyUsageInstance, + resetDailyLimit } \ No newline at end of file diff --git a/UsageMntrServ/index.js b/UsageMntrServ/index.js index df8b78f..1a4e55e 100644 --- a/UsageMntrServ/index.js +++ b/UsageMntrServ/index.js @@ -1,15 +1,31 @@ +require("dotenv").config(); + const express = require("express"); const bodyParser = require("body-parser"); const cors = require("cors"); const axios = require("axios"); -const { getDailyUsageById, addNewDailyUsageInstance } = require("./controllers/DailyUsageController"); +const cron = require('node-cron'); +const db = require("mongoose"); +const { getDailyUsageById, addNewDailyUsageInstance, resetDailyLimit } = require("./controllers/DailyUsageController"); + const app = express(); app.use(bodyParser.json()); app.use(cors()); + app.get("/usage", getDailyUsageById); app.post("/createUser", addNewDailyUsageInstance); +app.post("/resetDailyLimit", resetDailyLimit); + + +cron.schedule('0 0 * * *', async () => { + console.log("UsageMntrServ: Resetting daily limit..."); + await axios.post("http://localhost:4001/resetDailyLimit").catch((err) => { + console.log(err.message); + }); +}); + app.post("/events", async (req, res) => { const { type } = req.body; @@ -42,6 +58,14 @@ app.post("/events", async (req, res) => { }); -app.listen(4002, () => { - console.log("UsageMntrServ: Listening on 4002"); -}); \ No newline at end of file +db.connect(process.env.MONGO_URI) + .then((result) => { + console.log("Connected to the Database..."); + // Listen for Requests + app.listen(4002, () => { + console.log("UsageMntrServ: Listening on 4002"); + }); + }) + .catch((err) => { + console.error(err); + }); \ No newline at end of file diff --git a/UsageMntrServ/package-lock.json b/UsageMntrServ/package-lock.json index b32b5b0..bc4f33a 100644 --- a/UsageMntrServ/package-lock.json +++ b/UsageMntrServ/package-lock.json @@ -12,8 +12,10 @@ "axios": "^1.6.2", "body-parser": "^1.20.2", "cors": "^2.8.5", + "dotenv": "^16.3.1", "express": "^4.18.2", "mongoose": "^8.0.3", + "node-cron": "^3.0.3", "nodemon": "^3.0.2" } }, @@ -299,6 +301,17 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/dotenv": { + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -852,6 +865,17 @@ "node": ">= 0.6" } }, + "node_modules/node-cron": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz", + "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==", + "dependencies": { + "uuid": "8.3.2" + }, + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/nodemon": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.2.tgz", @@ -1270,6 +1294,14 @@ "node": ">= 0.4.0" } }, + "node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/UsageMntrServ/package.json b/UsageMntrServ/package.json index ba36fe4..305930f 100644 --- a/UsageMntrServ/package.json +++ b/UsageMntrServ/package.json @@ -12,8 +12,10 @@ "axios": "^1.6.2", "body-parser": "^1.20.2", "cors": "^2.8.5", + "dotenv": "^16.3.1", "express": "^4.18.2", "mongoose": "^8.0.3", + "node-cron": "^3.0.3", "nodemon": "^3.0.2" } }