Merge pull request #148 from bluewave-labs/feat/network-service
Implemented network service, resolves #145
This commit is contained in:
2 files changed
+106
-20
No files matched your search
+11
-20
@@ -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,
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
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 measureResponseTime(operation) {
|
||||
const startTime = Date.now();
|
||||
try {
|
||||
const response = await operation();
|
||||
const endTime = Date.now();
|
||||
return { responseTime: endTime - startTime, response };
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async handlePing(job) {
|
||||
const operation = async () => {
|
||||
const response = await ping.promise.probe(job.data.url);
|
||||
return response;
|
||||
};
|
||||
|
||||
try {
|
||||
const { responseTime, response } = await this.measureResponseTime(
|
||||
operation
|
||||
);
|
||||
const isAlive = response.alive;
|
||||
await this.logAndStoreCheck(job, isAlive, responseTime);
|
||||
return isAlive;
|
||||
} catch (error) {
|
||||
await this.logAndStoreCheck(job, false, 0, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async handleHttp(job) {
|
||||
const operation = async () => {
|
||||
const response = await axios.get(job.data.url);
|
||||
return response;
|
||||
};
|
||||
try {
|
||||
const { responseTime, response } = await this.measureResponseTime(
|
||||
operation
|
||||
);
|
||||
const isAlive = response.status >= 200 && response.status < 300;
|
||||
await this.logAndStoreCheck(job, isAlive, responseTime);
|
||||
return isAlive;
|
||||
} catch (error) {
|
||||
await this.logAndStoreCheck(job, false, 0, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async getStatus(job) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
async logAndStoreCheck(job, isAlive, responseTime, error = null) {
|
||||
const status = isAlive ? "alive" : "dead";
|
||||
if (error) {
|
||||
console.log(`Job ${job.data.url} is dead`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isAlive) {
|
||||
console.log(
|
||||
`Job ${job.data.url} is alive, response time: ${responseTime}ms`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Job ${job.data.url} is dead`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = NetworkService;
|
||||
Reference in new issue
Block a user