Implemented ping type for monitors, added cleanup for job queue

This commit is contained in:
Alex Holliday committed 2024-06-14 14:31:07 -07:00
1 parent f3de5ce381
commit f9f73dbc47
6 files changed
+58 -4

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 -3
View File
@@ -8,6 +8,7 @@ const JOBS_PER_WORKER = 5;
const logger = require("../utils/logger");
const SERVICE_NAME = "JobQueue";
const axios = require("axios");
const ping = require("ping");
class JobQueue {
/**
@@ -35,6 +36,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;
@@ -52,9 +56,25 @@ class JobQueue {
const worker = new Worker(
QUEUE_NAME,
async (job) => {
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,
@@ -215,12 +235,19 @@ 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();
return true;
} catch (error) {
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(),
});