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

+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;