Merge pull request #140 from bluewave-labs/feat/ping

Implemented ping type for monitors, added cleanup for job queue, resolves #136
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-06-15 20:05:25 -07:00
commit 3a5b1d21dd
6 files changed
+58 -5

No files matched your search

+10
View File
@@ -93,6 +93,16 @@ const startApp = async () => {
await connectDbAndRunServer(app, db);
const jobQueue = await JobQueue.createJobQueue(db);
const cleanup = async () => {
console.log("Shutting down gracefully");
await jobQueue.obliterate();
console.log("Finished cleanup");
process.exit(0);
};
process.on("SIGINT", cleanup);
process.on("SIGTERM", cleanup);
};
startApp().catch((error) => {
+5
View File
@@ -14,6 +14,11 @@ const MonitorSchema = mongoose.Schema(
description: {
type: String,
},
type: {
type: String,
required: true,
enum: ["http", "ping"],
},
url: {
type: String,
required: true,
+10
View File
@@ -21,6 +21,7 @@
"jsonwebtoken": "9.0.2",
"mailersend": "^2.2.0",
"mongoose": "^8.3.3",
"ping": "0.4.4",
"winston": "^3.13.0"
},
"devDependencies": {
@@ -2205,6 +2206,15 @@
"url": "https://github.com/sponsors/jonschlinkert"
}
},
"node_modules/ping": {
"version": "0.4.4",
"resolved": "https://registry.npmjs.org/ping/-/ping-0.4.4.tgz",
"integrity": "sha512-56ZMC0j7SCsMMLdOoUg12VZCfj/+ZO+yfOSjaNCRrmZZr6GLbN2X/Ui56T15dI8NhiHckaw5X2pvyfAomanwqQ==",
"license": "MIT",
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/proxy-addr": {
"version": "2.0.7",
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
+1
View File
@@ -23,6 +23,7 @@
"jsonwebtoken": "9.0.2",
"mailersend": "^2.2.0",
"mongoose": "^8.3.3",
"ping": "0.4.4",
"winston": "^3.13.0"
},
"devDependencies": {
+30 -4
View File
@@ -9,6 +9,7 @@ const logger = require("../utils/logger");
const { errorMessages, successMessages } = require("../utils/messages");
const SERVICE_NAME = "JobQueue";
const axios = require("axios");
const ping = require("ping");
class JobQueue {
/**
@@ -36,6 +37,9 @@ class JobQueue {
try {
queue.db = db;
const monitors = await db.getAllMonitors();
for (const monitor of monitors) {
await queue.addJob(monitor.id, monitor);
}
const workerStats = await queue.getWorkerStats();
await queue.scaleWorkers(workerStats);
return queue;
@@ -53,10 +57,25 @@ class JobQueue {
const worker = new Worker(
QUEUE_NAME,
async (job) => {
//TODO add a check to see what type of service
const response = await axios.get(job.data.url);
// TODO create a check object and save it to the db
console.log(response.status);
// TODO Extract to service
if (job.data.type === "ping") {
const response = await ping.promise.probe(job.data.url);
// TODO insert checks into DB
if (response.alive === true) {
console.log(`${job.data.url} is alive`);
} else {
console.log(`${job.data.url} is dead`);
}
} else if (job.data.type === "http") {
const response = await axios.get(job.data.url);
// TODO create a check object and save it to the db
if (response.status >= 200 && response.status < 300) {
// TODO insert checks into DB
console.log(`${job.data.url} is alive`);
} else {
console.log(`${job.data.url} is dead`);
}
}
},
{
connection,
@@ -219,6 +238,11 @@ class JobQueue {
*/
async obliterate() {
try {
const jobs = await this.getJobs();
for (const job of jobs) {
await this.queue.remove(job.id);
}
console.log(jobs);
await this.queue.obliterate();
logger.info(successMessages.JOB_QUEUE_OBLITERATE, {
service: SERVICE_NAME,
@@ -229,6 +253,8 @@ class JobQueue {
throw error;
}
}
//TODO Cleanup Queue on shutdown
}
module.exports = JobQueue;
+2 -1
View File
@@ -60,7 +60,8 @@ const monitorValidation = joi.object({
userId: joi.string().required(),
name: joi.string().required(),
description: joi.string().required(),
url: joi.string().uri().required(),
type: joi.string().required(),
url: joi.string().required(),
isActive: joi.boolean(),
interval: joi.number(),
});