Implemented network service
This commit is contained in:
1 parent
86d9389abc
commit
14f68d58f7
2 files changed
+90
-20
No files matched your search
+11
-20
@@ -7,9 +7,8 @@ const connection = {
|
|||||||
const JOBS_PER_WORKER = 5;
|
const JOBS_PER_WORKER = 5;
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
const { errorMessages, successMessages } = require("../utils/messages");
|
const { errorMessages, successMessages } = require("../utils/messages");
|
||||||
|
const NetworkService = require("./networkService");
|
||||||
const SERVICE_NAME = "JobQueue";
|
const SERVICE_NAME = "JobQueue";
|
||||||
const axios = require("axios");
|
|
||||||
const ping = require("ping");
|
|
||||||
|
|
||||||
class JobQueue {
|
class JobQueue {
|
||||||
/**
|
/**
|
||||||
@@ -23,6 +22,7 @@ class JobQueue {
|
|||||||
});
|
});
|
||||||
this.workers = [];
|
this.workers = [];
|
||||||
this.db = null;
|
this.db = null;
|
||||||
|
this.networkService = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,6 +36,7 @@ class JobQueue {
|
|||||||
const queue = new JobQueue();
|
const queue = new JobQueue();
|
||||||
try {
|
try {
|
||||||
queue.db = db;
|
queue.db = db;
|
||||||
|
queue.networkService = new NetworkService(db);
|
||||||
const monitors = await db.getAllMonitors();
|
const monitors = await db.getAllMonitors();
|
||||||
for (const monitor of monitors) {
|
for (const monitor of monitors) {
|
||||||
await queue.addJob(monitor.id, monitor);
|
await queue.addJob(monitor.id, monitor);
|
||||||
@@ -57,24 +58,14 @@ class JobQueue {
|
|||||||
const worker = new Worker(
|
const worker = new Worker(
|
||||||
QUEUE_NAME,
|
QUEUE_NAME,
|
||||||
async (job) => {
|
async (job) => {
|
||||||
// TODO Extract to service
|
try {
|
||||||
if (job.data.type === "ping") {
|
await this.networkService.getStatus(job);
|
||||||
const response = await ping.promise.probe(job.data.url);
|
} catch (error) {
|
||||||
// TODO insert checks into DB
|
logger.error(`Error processing job ${job.id}: ${error.message}`, {
|
||||||
if (response.alive === true) {
|
service: SERVICE_NAME,
|
||||||
console.log(`${job.data.url} is alive`);
|
jobId: job.id,
|
||||||
} else {
|
error: error,
|
||||||
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`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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;
|
||||||
Reference in new issue
Block a user