Implemented ping type for monitors, added cleanup for job queue
This commit is contained in:
1 parent
f3de5ce381
commit
f9f73dbc47
6 files changed
+58
-4
No files matched your search
@@ -93,6 +93,16 @@ const startApp = async () => {
|
|||||||
|
|
||||||
await connectDbAndRunServer(app, db);
|
await connectDbAndRunServer(app, db);
|
||||||
const jobQueue = await JobQueue.createJobQueue(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) => {
|
startApp().catch((error) => {
|
||||||
|
|||||||
@@ -14,6 +14,11 @@ const MonitorSchema = mongoose.Schema(
|
|||||||
description: {
|
description: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
|
type: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
enum: ["http", "ping"],
|
||||||
|
},
|
||||||
url: {
|
url: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
|
|||||||
Generated
+10
@@ -21,6 +21,7 @@
|
|||||||
"jsonwebtoken": "9.0.2",
|
"jsonwebtoken": "9.0.2",
|
||||||
"mailersend": "^2.2.0",
|
"mailersend": "^2.2.0",
|
||||||
"mongoose": "^8.3.3",
|
"mongoose": "^8.3.3",
|
||||||
|
"ping": "0.4.4",
|
||||||
"winston": "^3.13.0"
|
"winston": "^3.13.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -2205,6 +2206,15 @@
|
|||||||
"url": "https://github.com/sponsors/jonschlinkert"
|
"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": {
|
"node_modules/proxy-addr": {
|
||||||
"version": "2.0.7",
|
"version": "2.0.7",
|
||||||
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
|
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
"jsonwebtoken": "9.0.2",
|
"jsonwebtoken": "9.0.2",
|
||||||
"mailersend": "^2.2.0",
|
"mailersend": "^2.2.0",
|
||||||
"mongoose": "^8.3.3",
|
"mongoose": "^8.3.3",
|
||||||
|
"ping": "0.4.4",
|
||||||
"winston": "^3.13.0"
|
"winston": "^3.13.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ const JOBS_PER_WORKER = 5;
|
|||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
const SERVICE_NAME = "JobQueue";
|
const SERVICE_NAME = "JobQueue";
|
||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
|
const ping = require("ping");
|
||||||
|
|
||||||
class JobQueue {
|
class JobQueue {
|
||||||
/**
|
/**
|
||||||
@@ -35,6 +36,9 @@ class JobQueue {
|
|||||||
try {
|
try {
|
||||||
queue.db = db;
|
queue.db = db;
|
||||||
const monitors = await db.getAllMonitors();
|
const monitors = await db.getAllMonitors();
|
||||||
|
for (const monitor of monitors) {
|
||||||
|
await queue.addJob(monitor.id, monitor);
|
||||||
|
}
|
||||||
const workerStats = await queue.getWorkerStats();
|
const workerStats = await queue.getWorkerStats();
|
||||||
await queue.scaleWorkers(workerStats);
|
await queue.scaleWorkers(workerStats);
|
||||||
return queue;
|
return queue;
|
||||||
@@ -52,9 +56,25 @@ class JobQueue {
|
|||||||
const worker = new Worker(
|
const worker = new Worker(
|
||||||
QUEUE_NAME,
|
QUEUE_NAME,
|
||||||
async (job) => {
|
async (job) => {
|
||||||
const response = await axios.get(job.data.url);
|
// TODO Extract to service
|
||||||
// TODO create a check object and save it to the db
|
if (job.data.type === "ping") {
|
||||||
console.log(response.status);
|
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,
|
connection,
|
||||||
@@ -215,12 +235,19 @@ class JobQueue {
|
|||||||
*/
|
*/
|
||||||
async obliterate() {
|
async obliterate() {
|
||||||
try {
|
try {
|
||||||
|
const jobs = await this.getJobs();
|
||||||
|
for (const job of jobs) {
|
||||||
|
await this.queue.remove(job.id);
|
||||||
|
}
|
||||||
|
console.log(jobs);
|
||||||
await this.queue.obliterate();
|
await this.queue.obliterate();
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO Cleanup Queue on shutdown
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = JobQueue;
|
module.exports = JobQueue;
|
||||||
@@ -60,7 +60,8 @@ const monitorValidation = joi.object({
|
|||||||
userId: joi.string().required(),
|
userId: joi.string().required(),
|
||||||
name: joi.string().required(),
|
name: joi.string().required(),
|
||||||
description: joi.string().required(),
|
description: joi.string().required(),
|
||||||
url: joi.string().uri().required(),
|
type: joi.string().required(),
|
||||||
|
url: joi.string().required(),
|
||||||
isActive: joi.boolean(),
|
isActive: joi.boolean(),
|
||||||
interval: joi.number(),
|
interval: joi.number(),
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user