Skip to content

Commit

Permalink
Added Cron Job Scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
NightWalker7558 committed Dec 14, 2023
1 parent e0a21f8 commit c329bb8
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 20 deletions.
File renamed without changes.
28 changes: 13 additions & 15 deletions EventBus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 23 additions & 1 deletion UsageMntrServ/controllers/DailyUsageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
32 changes: 28 additions & 4 deletions UsageMntrServ/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,6 +58,14 @@ app.post("/events", async (req, res) => {
});


app.listen(4002, () => {
console.log("UsageMntrServ: Listening on 4002");
});
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);
});
32 changes: 32 additions & 0 deletions UsageMntrServ/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions UsageMntrServ/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit c329bb8

Please sign in to comment.