Implemented network service

This commit is contained in:
Alex Holliday committed 2024-06-17 10:47:36 -07:00
1 parent 86d9389abc
commit 14f68d58f7
2 files changed
+90 -20

No files matched your search

+11 -20
View File
@@ -7,9 +7,8 @@ const connection = {
const JOBS_PER_WORKER = 5;
const logger = require("../utils/logger");
const { errorMessages, successMessages } = require("../utils/messages");
const NetworkService = require("./networkService");
const SERVICE_NAME = "JobQueue";
const axios = require("axios");
const ping = require("ping");
class JobQueue {
/**
@@ -23,6 +22,7 @@ class JobQueue {
});
this.workers = [];
this.db = null;
this.networkService = null;
}
/**
@@ -36,6 +36,7 @@ class JobQueue {
const queue = new JobQueue();
try {
queue.db = db;
queue.networkService = new NetworkService(db);
const monitors = await db.getAllMonitors();
for (const monitor of monitors) {
await queue.addJob(monitor.id, monitor);
@@ -57,24 +58,14 @@ class JobQueue {
const worker = new Worker(
QUEUE_NAME,
async (job) => {
// 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`);
}
try {
await this.networkService.getStatus(job);
} catch (error) {
logger.error(`Error processing job ${job.id}: ${error.message}`, {
service: SERVICE_NAME,
jobId: job.id,
error: error,
});
}
},
{
+79
View File
@@ -0,0 +1,79 @@
const axios = require("axios");
const ping = require("ping");
const logger = require("../utils/logger");
class NetworkService {
constructor(db) {
this.db = db;
this.TYPE_PING = "ping";
this.TYPE_HTTP = "http";
this.SERVICE_NAME = "NetworkService";
}
async handlePing(job) {
try {
const response = await ping.promise.probe(job.data.url);
const isAlive = response.alive;
await this.logAndStoreCheck(job, isAlive);
return isAlive;
} catch (error) {
await this.logAndStoreCheck(job, false, error);
return false;
}
}
async handleHttp(job) {
try {
const response = await axios.get(job.data.url);
const isAlive = response.status >= 200 && response.status < 300;
await this.logAndStoreCheck(job, isAlive);
return isAlive;
} catch (error) {
await this.logAndStoreCheck(job, false, error);
return false;
}
}
async getStatus(job) {
try {
switch (job.data.type) {
case this.TYPE_PING:
return await this.handlePing(job);
case this.TYPE_HTTP:
return await this.handleHttp(job);
default:
console.error(`Unsupported type: ${job.data.type}`, {
service: this.SERVICE_NAME,
timestamp: new Date().toISOString(),
});
return false;
}
} catch (error) {
throw error;
}
}
async logAndStoreCheck(job, isAlive, error = null) {
const status = isAlive ? "alive" : "dead";
if (error) {
logger.error(`Error processing job ${job.id}: ${error.message}`, {
service: this.SERVICE_NAME,
jobId: job.id,
error: error,
});
return;
}
if (isAlive) {
console.log(`Job ${job.data.url} is alive`);
return;
}
if (!isAlive) {
console.log(`Job ${job.data.url} is dead`);
return;
}
}
}
module.exports = NetworkService;