Implemented HTTP(s) Request, added add and delete to and from job queue
This commit is contained in:
1 parent
f827293a12
commit
a9ddb27218
6 files changed
+48
-10
No files matched your search
@@ -6,6 +6,8 @@ const connection = {
|
||||
};
|
||||
const JOBS_PER_WORKER = 5;
|
||||
const logger = require("../utils/logger");
|
||||
const SERVICE_NAME = "JobQueue";
|
||||
const axios = require("axios");
|
||||
|
||||
class JobQueue {
|
||||
/**
|
||||
@@ -18,6 +20,7 @@ class JobQueue {
|
||||
connection,
|
||||
});
|
||||
this.workers = [];
|
||||
this.db = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,9 +30,11 @@ class JobQueue {
|
||||
* @returns {Promise<JobQueue>} - Returns a new JobQueue
|
||||
*
|
||||
*/
|
||||
static async createJobQueue() {
|
||||
static async createJobQueue(db) {
|
||||
const queue = new JobQueue();
|
||||
try {
|
||||
queue.db = db;
|
||||
const monitors = await db.getAllMonitors();
|
||||
const workerStats = await queue.getWorkerStats();
|
||||
await queue.scaleWorkers(workerStats);
|
||||
return queue;
|
||||
@@ -47,8 +52,9 @@ class JobQueue {
|
||||
const worker = new Worker(
|
||||
QUEUE_NAME,
|
||||
async (job) => {
|
||||
// TODO Ping a monitor
|
||||
console.log(`${job.name} completed, workers: ${this.workers.length}`);
|
||||
const response = await axios.get(job.data.url);
|
||||
// TODO create a check object and save it to the db
|
||||
console.log(response.status);
|
||||
},
|
||||
{
|
||||
connection,
|
||||
@@ -147,7 +153,6 @@ class JobQueue {
|
||||
async getJobs() {
|
||||
try {
|
||||
const jobs = await this.queue.getRepeatableJobs();
|
||||
console.log("jobs", jobs);
|
||||
return jobs;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
@@ -166,8 +171,7 @@ class JobQueue {
|
||||
try {
|
||||
await this.queue.add(jobName, payload, {
|
||||
repeat: {
|
||||
every: 1000,
|
||||
limit: 100,
|
||||
every: payload.interval,
|
||||
},
|
||||
});
|
||||
const workerStats = await this.getWorkerStats();
|
||||
@@ -177,6 +181,34 @@ class JobQueue {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a job from the queue.
|
||||
*
|
||||
* @async
|
||||
* @param {Monitor} monitor - The monitor to remove.
|
||||
* @throws {Error}
|
||||
*/
|
||||
async deleteJob(monitor) {
|
||||
try {
|
||||
const deleted = await this.queue.removeRepeatable(monitor.id, {
|
||||
every: monitor.interval,
|
||||
});
|
||||
if (deleted) {
|
||||
logger.info("Job removed from queue", {
|
||||
service: SERVICE_NAME,
|
||||
jobId: monitor.id,
|
||||
});
|
||||
} else {
|
||||
logger.error("Job not found in queue", {
|
||||
service: SERVICE_NAME,
|
||||
jobId: monitor.id,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @async
|
||||
* @returns {Promise<boolean>} - Returns true if obliteration is successful
|
||||
|
||||
Reference in new issue
Block a user