Merge pull request #1502 from bluewave-labs/fix/be/improved-jobqueue

fix: be/improved jobqueue
This commit is contained in:
Alexander Holliday authored and GitHub committed 2025-01-02 09:59:04 -08:00
commit 7b3a83a37f
5 files changed
+343 -207

No files matched your search

@@ -47,7 +47,6 @@ const createHardwareCheck = async (hardwareCheckData) => {
const deleteHardwareChecksByMonitorId = async (monitorId) => { const deleteHardwareChecksByMonitorId = async (monitorId) => {
try { try {
const result = await HardwareCheck.deleteMany({ monitorId }); const result = await HardwareCheck.deleteMany({ monitorId });
console.log("deleted hardware checks", result);
return result.deletedCount; return result.deletedCount;
} catch (error) { } catch (error) {
error.service = SERVICE_NAME; error.service = SERVICE_NAME;
+20 -3
View File
@@ -68,6 +68,8 @@ import ServiceRegistry from "./service/serviceRegistry.js";
import MongoDB from "./db/mongo/MongoDB.js"; import MongoDB from "./db/mongo/MongoDB.js";
import IORedis from "ioredis";
const SERVICE_NAME = "Server"; const SERVICE_NAME = "Server";
const SHUTDOWN_TIMEOUT = 1000; const SHUTDOWN_TIMEOUT = 1000;
let isShuttingDown = false; let isShuttingDown = false;
@@ -87,12 +89,24 @@ const shutdown = async () => {
} }
isShuttingDown = true; isShuttingDown = true;
logger.info({ message: "Attempting graceful shutdown" }); logger.info({ message: "Attempting graceful shutdown" });
setTimeout(() => { setTimeout(async () => {
logger.error({ logger.error({
message: "Could not shut down in time, forcing shutdown", message: "Could not shut down in time, forcing shutdown",
service: SERVICE_NAME, service: SERVICE_NAME,
method: "shutdown", method: "shutdown",
}); });
// flush Redis
const settings =
ServiceRegistry.get(SettingsService.SERVICE_NAME).getSettings() || {};
const { redisHost = "127.0.0.1", redisPort = 6379 } = settings;
const redis = new IORedis({
host: redisHost,
port: redisPort,
});
logger.info({ message: "Flushing Redis" });
await redis.flushall();
logger.info({ message: "Redis flushed" });
process.exit(1); process.exit(1);
}, SHUTDOWN_TIMEOUT); }, SHUTDOWN_TIMEOUT);
try { try {
@@ -133,10 +147,11 @@ const startApp = async () => {
const networkService = new NetworkService(axios, ping, logger, http, Docker, net); const networkService = new NetworkService(axios, ping, logger, http, Docker, net);
const statusService = new StatusService(db, logger); const statusService = new StatusService(db, logger);
const notificationService = new NotificationService(emailService, db, logger); const notificationService = new NotificationService(emailService, db, logger);
const jobQueue = await JobQueue.createJobQueue(
const jobQueue = new JobQueue(
db, db,
networkService,
statusService, statusService,
networkService,
notificationService, notificationService,
settingsService, settingsService,
logger, logger,
@@ -212,6 +227,8 @@ const startApp = async () => {
); );
const queueRoutes = new QueueRoutes(queueController); const queueRoutes = new QueueRoutes(queueController);
const statusPageRoutes = new StatusPageRoutes(statusPageController); const statusPageRoutes = new StatusPageRoutes(statusPageController);
// Init job queue
await jobQueue.initJobQueue();
// Middleware // Middleware
app.use( app.use(
cors() cors()
+4 -4
View File
@@ -18,6 +18,7 @@
"express": "^4.19.2", "express": "^4.19.2",
"handlebars": "^4.7.8", "handlebars": "^4.7.8",
"helmet": "^8.0.0", "helmet": "^8.0.0",
"ioredis": "^5.4.2",
"joi": "^17.13.1", "joi": "^17.13.1",
"jsonwebtoken": "9.0.2", "jsonwebtoken": "9.0.2",
"mailersend": "^2.2.0", "mailersend": "^2.2.0",
@@ -3902,10 +3903,9 @@
"license": "ISC" "license": "ISC"
}, },
"node_modules/ioredis": { "node_modules/ioredis": {
"version": "5.4.1", "version": "5.4.2",
"resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.4.1.tgz", "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.4.2.tgz",
"integrity": "sha512-2YZsvl7jopIa1gaePkeMtd9rAcSjOOjPtpcLlOeusyO+XH2SK5ZcT+UCrElPP+WVIInh2TzeI4XW9ENaSLVVHA==", "integrity": "sha512-0SZXGNGZ+WzISQ67QDyZ2x0+wVxjjUndtD8oSeik/4ajifeiRufed8fCb8QW8VMyi4MXcS+UO1k/0NGhvq1PAg==",
"license": "MIT",
"dependencies": { "dependencies": {
"@ioredis/commands": "^1.1.1", "@ioredis/commands": "^1.1.1",
"cluster-key-slot": "^1.1.0", "cluster-key-slot": "^1.1.0",
+1
View File
@@ -25,6 +25,7 @@
"express": "^4.19.2", "express": "^4.19.2",
"handlebars": "^4.7.8", "handlebars": "^4.7.8",
"helmet": "^8.0.0", "helmet": "^8.0.0",
"ioredis": "^5.4.2",
"joi": "^17.13.1", "joi": "^17.13.1",
"jsonwebtoken": "9.0.2", "jsonwebtoken": "9.0.2",
"mailersend": "^2.2.0", "mailersend": "^2.2.0",
+318 -199
View File
@@ -1,30 +1,22 @@
const QUEUE_NAME = "monitors"; const QUEUE_NAMES = ["uptime", "pagespeed", "hardware"];
const SERVICE_NAME = "NewJobQueue";
const JOBS_PER_WORKER = 5; const JOBS_PER_WORKER = 5;
const SERVICE_NAME = "JobQueue"; const QUEUE_LOOKUP = {
hardware: "hardware",
http: "uptime",
ping: "uptime",
docker: "uptime",
pagespeed: "pagespeed",
};
import { errorMessages, successMessages } from "../utils/messages.js"; import { successMessages, errorMessages } from "../utils/messages.js";
/** class NewJobQueue {
* JobQueue
*
* This service is responsible for managing the job queue.
* It handles enqueuing, dequeuing, and processing jobs.
* It scales the number of workers based on the number of jobs/worker
*/
class JobQueue {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
/**
* @class JobQueue
* @classdesc Manages job queue and workers.
*
* @param {Object} statusService - Service for handling status updates.
* @param {Object} notificationService - Service for handling notifications.
* @param {Object} settingsService - Service for retrieving settings.
* @param {Object} logger - Logger for logging information.
* @param {Function} Queue - Queue constructor.
* @param {Function} Worker - Worker constructor.
*/
constructor( constructor(
db,
statusService, statusService,
networkService,
notificationService, notificationService,
settingsService, settingsService,
logger, logger,
@@ -32,102 +24,69 @@ class JobQueue {
Worker Worker
) { ) {
const settings = settingsService.getSettings() || {}; const settings = settingsService.getSettings() || {};
const { redisHost = "127.0.0.1", redisPort = 6379 } = settings; const { redisHost = "127.0.0.1", redisPort = 6379 } = settings;
const connection = { const connection = {
host: redisHost, host: redisHost,
port: redisPort, port: redisPort,
}; };
this.queue = new Queue(QUEUE_NAME, {
connection, this.queues = {};
}); this.workers = {};
this.connection = connection; this.connection = connection;
this.workers = []; this.db = db;
this.db = null; this.networkService = networkService;
this.networkService = null;
this.statusService = statusService; this.statusService = statusService;
this.notificationService = notificationService; this.notificationService = notificationService;
this.settingsService = settingsService; this.settingsService = settingsService;
this.logger = logger; this.logger = logger;
this.Worker = Worker; this.Worker = Worker;
QUEUE_NAMES.forEach((name) => {
this.queues[name] = new Queue(name, { connection });
this.workers[name] = [];
});
} }
/** /**
* Creates and initializes a JobQueue instance. * Initializes job queues by adding jobs for all active monitors
* * @async
* @param {Object} db - Database service for accessing monitors. * @function initJobQueue
* @param {Object} networkService - Service for network operations. * @description Retrieves all monitors from the database and adds jobs for active ones to their respective queues
* @param {Object} statusService - Service for handling status updates. * @throws {Error} If there's an error retrieving monitors or adding jobs
* @param {Object} notificationService - Service for handling notifications. * @returns {Promise<void>}
* @param {Object} settingsService - Service for retrieving settings.
* @param {Object} logger - Logger for logging information.
* @param {Function} Queue - Queue constructor.
* @param {Function} Worker - Worker constructor.
* @returns {Promise<JobQueue>} - The initialized JobQueue instance.
* @throws {Error} - Throws an error if initialization fails.
*/ */
static async createJobQueue( async initJobQueue() {
db, const monitors = await this.db.getAllMonitors();
networkService, await Promise.all(
statusService, monitors
notificationService, .filter((monitor) => monitor.isActive)
settingsService, .map(async (monitor) => {
logger, try {
Queue, console.log(`Adding job for monitor: ${monitor._id} (${monitor.type})`);
Worker await this.addJob(monitor._id, monitor);
) { console.log(`Successfully added job for monitor: ${monitor._id}`);
const queue = new JobQueue( } catch (error) {
statusService, console.error(`Failed to add job for monitor ${monitor._id}:`, error);
notificationService, // Don't throw here to allow other jobs to continue
settingsService, }
logger,
Queue,
Worker
);
try {
queue.db = db;
queue.networkService = networkService;
const monitors = await db.getAllMonitors();
for (const monitor of monitors) {
if (monitor.isActive) {
queue.addJob(monitor.id, monitor).catch((error) => {
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "createJobQueue",
stack: error.stack,
});
});
}
}
queue
.getWorkerStats()
.then((workerStats) => {
queue.scaleWorkers(workerStats);
}) })
.catch((error) => { );
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "createJobQueue",
stack: error.stack,
});
});
return queue;
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "createJobQueue") : null;
throw error;
}
} }
/** /**
* Checks if the given monitor is in a maintenance window. * Checks if a monitor is currently in a maintenance window
* * @async
* @param {string} monitorId - The ID of the monitor to check. * @param {string} monitorId - The ID of the monitor to check
* @returns {Promise<boolean>} - Returns true if the monitor is in a maintenance window, otherwise false. * @returns {Promise<boolean>} Returns true if the monitor is in an active maintenance window, false otherwise
* @throws {Error} - Throws an error if the database query fails. * @throws {Error} If there's an error retrieving maintenance windows from the database
* @description
* Retrieves all maintenance windows for a monitor and checks if any are currently active.
* A maintenance window is considered active if:
* 1. The window is marked as active AND
* 2. Either:
* - Current time falls between start and end times
* - For repeating windows: Current time falls between any repeated interval
*/ */
async isInMaintenanceWindow(monitorId) { async isInMaintenanceWindow(monitorId) {
const maintenanceWindows = await this.db.getMaintenanceWindowsByMonitorId(monitorId); const maintenanceWindows = await this.db.getMaintenanceWindowsByMonitorId(monitorId);
@@ -160,39 +119,70 @@ class JobQueue {
} }
/** /**
* Creates a job handler function for processing jobs. * Creates a job processing handler for monitor checks
* @function createJobHandler
* @returns {Function} An async function that processes monitor check jobs
* @description
* Creates and returns a job handler that:
* 1. Checks if monitor is in maintenance window
* 2. If not in maintenance, performs network status check
* 3. Updates monitor status in database
* 4. Triggers notifications if status changed
* *
* @returns {Function} An async function that processes a job. * @param {Object} job - The job to process
* @param {Object} job.data - The monitor data
* @param {string} job.data._id - Monitor ID
* @param {string} job.id - Job ID
*
* @throws {Error} Logs errors but doesn't throw them to prevent job failure
* @returns {Promise<void>} Resolves when job processing is complete
*/ */
createJobHandler() { createJobHandler() {
return async (job) => { return async (job) => {
try { try {
// Get all maintenance windows for this monitor // Get all maintenance windows for this monitor
await job.updateProgress(0);
const monitorId = job.data._id; const monitorId = job.data._id;
const maintenanceWindowActive = await this.isInMaintenanceWindow(monitorId); const maintenanceWindowActive = await this.isInMaintenanceWindow(monitorId);
// If a maintenance window is active, we're done // If a maintenance window is active, we're done
if (maintenanceWindowActive) { if (maintenanceWindowActive) {
await job.updateProgress(100);
this.logger.info({ this.logger.info({
message: `Monitor ${monitorId} is in maintenance window`, message: `Monitor ${monitorId} is in maintenance window`,
service: SERVICE_NAME, service: SERVICE_NAME,
method: "createWorker", method: "createWorker",
}); });
return; return false;
} }
// Get the current status // Get the current status
await job.updateProgress(30);
const networkResponse = await this.networkService.getStatus(job); const networkResponse = await this.networkService.getStatus(job);
// Handle status change // Handle status change
await job.updateProgress(60);
const { monitor, statusChanged, prevStatus } = const { monitor, statusChanged, prevStatus } =
await this.statusService.updateStatus(networkResponse); await this.statusService.updateStatus(networkResponse);
// Handle notifications // Handle notifications
this.notificationService.handleNotifications({ await job.updateProgress(80);
...networkResponse, this.notificationService
monitor, .handleNotifications({
prevStatus, ...networkResponse,
statusChanged, monitor,
}); prevStatus,
statusChanged,
})
.catch((error) => {
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "createJobHandler",
details: `Error sending notifications for job ${job.id}: ${error.message}`,
stack: error.stack,
});
});
await job.updateProgress(100);
return true;
} catch (error) { } catch (error) {
this.logger.error({ this.logger.error({
message: error.message, message: error.message,
@@ -201,29 +191,41 @@ class JobQueue {
details: `Error processing job ${job.id}: ${error.message}`, details: `Error processing job ${job.id}: ${error.message}`,
stack: error.stack, stack: error.stack,
}); });
throw error;
} }
}; };
} }
/** /**
* Creates a worker for the queue * Creates a new worker for processing jobs in a queue
* Operations are carried out in the async callback * @param {Queue} queue - The BullMQ queue to create a worker for
* @returns {Worker} The newly created worker * @returns {Worker} A new BullMQ worker instance
* @description
* Creates and configures a new worker with:
* - Queue-specific job handler
* - Redis connection settings
* - Default worker options
* The worker processes jobs from the specified queue using the job handler
* created by createJobHandler()
*
* @throws {Error} If worker creation fails or connection is invalid
*/ */
createWorker() { createWorker(queue) {
const worker = new this.Worker(QUEUE_NAME, this.createJobHandler(), { const worker = new this.Worker(queue.name, this.createJobHandler(), {
connection: this.connection, connection: this.connection,
concurrency: 5,
});
worker.on("failed", (job, err) => {
this.logger.error({
message: `Worker failed job: ${job.id}`,
service: SERVICE_NAME,
method: "createWorker",
stack: err.stack,
});
}); });
return worker; return worker;
} }
/**
* @typedef {Object} WorkerStats
* @property {Array<Job>} jobs - Array of jobs in the Queue
* @property {number} - workerLoad - The number of jobs per worker
*
*/
/** /**
* Gets stats related to the workers * Gets stats related to the workers
* This is used for scaling workers right now * This is used for scaling workers right now
@@ -233,10 +235,10 @@ class JobQueue {
* @async * @async
* @returns {Promise<WorkerStats>} - Returns the worker stats * @returns {Promise<WorkerStats>} - Returns the worker stats
*/ */
async getWorkerStats() { async getWorkerStats(queue) {
try { try {
const jobs = await this.queue.getRepeatableJobs(); const jobs = await queue.getRepeatableJobs();
const load = jobs.length / this.workers.length; const load = jobs.length / this.workers[queue.name].length;
return { jobs, load }; return { jobs, load };
} catch (error) { } catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null; error.service === undefined ? (error.service = SERVICE_NAME) : null;
@@ -246,46 +248,56 @@ class JobQueue {
} }
/** /**
* Scale Workers * Scales workers up or down based on queue load
* This function scales workers based on the load per worker * @async
* If the load is higher than the JOBS_PER_WORKER threshold, we add more workers * @param {Object} workerStats - Statistics about current worker load
* If the load is lower than the JOBS_PER_WORKER threshold, we release workers * @param {number} workerStats.load - Current load per worker
* This approach ignores server performance, which we should add in the future * @param {Array} workerStats.jobs - Array of current jobs
* * @param {Queue} queue - The BullMQ queue to scale workers for
* @returns {Promise<boolean>} True if scaling occurred, false if no scaling was needed
* @throws {Error} If no workers array exists for the queue
* @description
* Scales workers based on these rules:
* - Maintains minimum of 5 workers
* - Adds workers if load exceeds JOBS_PER_WORKER
* - Removes workers if load is below JOBS_PER_WORKER
* - Creates initial workers if none exist
* Worker scaling is calculated based on excess jobs or excess capacity
*/
async scaleWorkers(workerStats, queue) {
const workers = this.workers[queue.name];
if (workers === undefined) {
throw new Error(`No workers found for ${queue.name}`);
}
* @async if (workers.length === 0) {
* @param {WorkerStats} workerStats - The payload for the job.
* @returns {Promise<boolean>}
*/
async scaleWorkers(workerStats) {
if (this.workers.length === 0) {
// There are no workers, need to add one // There are no workers, need to add one
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
const worker = this.createWorker(); const worker = this.createWorker(queue);
this.workers.push(worker); workers.push(worker);
} }
return true; return true;
} }
if (workerStats.load > JOBS_PER_WORKER) { if (workerStats.load > JOBS_PER_WORKER) {
// Find out how many more jobs we have than current workers can handle // Find out how many more jobs we have than current workers can handle
const excessJobs = workerStats.jobs.length - this.workers.length * JOBS_PER_WORKER; const excessJobs = workerStats.jobs.length - workers.length * JOBS_PER_WORKER;
// Divide by jobs/worker to find out how many workers to add // Divide by jobs/worker to find out how many workers to add
const workersToAdd = Math.ceil(excessJobs / JOBS_PER_WORKER); const workersToAdd = Math.ceil(excessJobs / JOBS_PER_WORKER);
for (let i = 0; i < workersToAdd; i++) { for (let i = 0; i < workersToAdd; i++) {
const worker = this.createWorker(); const worker = this.createWorker(queue);
this.workers.push(worker); workers.push(worker);
} }
return true; return true;
} }
if (workerStats.load < JOBS_PER_WORKER) { if (workerStats.load < JOBS_PER_WORKER) {
// Find out how much excess capacity we have // Find out how much excess capacity we have
const workerCapacity = this.workers.length * JOBS_PER_WORKER; const workerCapacity = workers.length * JOBS_PER_WORKER;
const excessCapacity = workerCapacity - workerStats.jobs.length; const excessCapacity = workerCapacity - workerStats.jobs.length;
// Calculate how many workers to remove // Calculate how many workers to remove
let workersToRemove = Math.floor(excessCapacity / JOBS_PER_WORKER); // Make sure there are always at least 5 let workersToRemove = Math.floor(excessCapacity / JOBS_PER_WORKER); // Make sure there are always at least 5
while (workersToRemove > 0 && this.workers.length > 5) { while (workersToRemove > 0 && workers.length > 5) {
const worker = this.workers.pop(); const worker = workers.pop();
workersToRemove--; workersToRemove--;
await worker.close().catch((error) => { await worker.close().catch((error) => {
// Catch the error instead of throwing it // Catch the error instead of throwing it
@@ -309,9 +321,9 @@ class JobQueue {
* @returns {Promise<Array<Job>>} * @returns {Promise<Array<Job>>}
* @throws {Error} - Throws error if getting jobs fails * @throws {Error} - Throws error if getting jobs fails
*/ */
async getJobs() { async getJobs(queue) {
try { try {
const jobs = await this.queue.getRepeatableJobs(); const jobs = await queue.getRepeatableJobs();
return jobs; return jobs;
} catch (error) { } catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null; error.service === undefined ? (error.service = SERVICE_NAME) : null;
@@ -321,21 +333,32 @@ class JobQueue {
} }
/** /**
* Retrieves the statistics of jobs and workers. * Retrieves detailed statistics about jobs and workers for all queues
* * @async
* @returns {Promise<Object>} - An object containing job statistics and the number of workers. * @returns {Promise<Object>} Queue statistics object
* @throws {Error} - Throws an error if the job statistics retrieval fails. * @throws {Error} If there's an error retrieving job information
* @description
* Returns an object with statistics for each queue including:
* - List of jobs with their URLs and current states
* - Number of workers assigned to the queue
*/ */
async getJobStats() { async getJobStats() {
try { try {
const jobs = await this.queue.getJobs(); let stats = {};
const ret = await Promise.all( await Promise.all(
jobs.map(async (job) => { QUEUE_NAMES.map(async (name) => {
const state = await job.getState(); const queue = this.queues[name];
return { url: job.data.url, state }; const jobs = await queue.getJobs();
const ret = await Promise.all(
jobs.map(async (job) => {
const state = await job.getState();
return { url: job.data.url, state };
})
);
stats[name] = { jobs: ret, workers: this.workers[name].length };
}) })
); );
return { jobs: ret, workers: this.workers.length }; return stats;
} catch (error) { } catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null; error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "getJobStats") : null; error.method === undefined ? (error.method = "getJobStats") : null;
@@ -344,25 +367,59 @@ class JobQueue {
} }
/** /**
* Adds a job to the queue and scales workers based on worker stats. * Adds both immediate and repeatable jobs to the appropriate queue
*
* @async * @async
* @param {string} jobName - The name of the job to be added. * @param {string} jobName - Name identifier for the job
* @param {Monitor} payload - The payload for the job. * @param {Object} monitor - Job data and configuration
* @throws {Error} - Will throw an error if the job cannot be added or workers don't scale * @param {string} monitor.type - Type of monitor/queue ('uptime', 'pagespeed', 'hardware')
* @param {string} [monitor.url] - URL to monitor (optional)
* @param {number} [monitor.interval=60000] - Repeat interval in milliseconds
* @param {string} monitor._id - Monitor ID
* @throws {Error} If queue not found for payload type
* @throws {Error} If job addition fails
* @description
* 1. Identifies correct queue based on payload type
* 2. Adds immediate job execution
* 3. Adds repeatable job with specified interval
* 4. Scales workers based on updated queue load
* Jobs are configured with exponential backoff, single attempt,
* and automatic removal on completion
*/ */
async addJob(jobName, payload) { async addJob(jobName, monitor) {
try { try {
this.logger.info({ message: `Adding job ${payload?.url ?? "No URL"}` }); this.logger.info({ message: `Adding job ${monitor?.url ?? "No URL"}` });
// Find the correct queue
const queue = this.queues[QUEUE_LOOKUP[monitor.type]];
if (queue === undefined) {
throw new Error(`Queue for ${monitor.type} not found`);
}
// build job options
const jobOptions = {
attempts: 1,
backoff: {
type: "exponential",
delay: 1000,
},
removeOnComplete: true,
removeOnFail: false,
timeout: 1 * 60 * 1000,
};
// Execute job immediately // Execute job immediately
await this.queue.add(jobName, payload); await queue.add(jobName, monitor, jobOptions);
await this.queue.add(jobName, payload, { await queue.add(jobName, monitor, {
...jobOptions,
repeat: { repeat: {
every: payload?.interval ?? 60000, every: monitor?.interval ?? 60000,
immediately: false,
}, },
}); });
const workerStats = await this.getWorkerStats();
await this.scaleWorkers(workerStats); const workerStats = await this.getWorkerStats(queue);
await this.scaleWorkers(workerStats, queue);
} catch (error) { } catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null; error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "addJob") : null; error.method === undefined ? (error.method = "addJob") : null;
@@ -371,15 +428,25 @@ class JobQueue {
} }
/** /**
* Deletes a job from the queue. * Deletes a repeatable job from its queue and adjusts worker scaling
*
* @async * @async
* @param {Monitor} monitor - The monitor to remove. * @param {Object} monitor - Monitor object containing job details
* @throws {Error} * @param {string} monitor._id - ID of the monitor/job to delete
* @param {string} monitor.type - Type of monitor determining queue selection
* @param {number} monitor.interval - Job repeat interval in milliseconds
* @throws {Error} If queue not found for monitor type
* @throws {Error} If job deletion fails
* @description
* 1. Identifies correct queue based on monitor type
* 2. Removes repeatable job using monitor ID and interval
* 3. Logs success or failure of deletion
* 4. Updates worker scaling based on new queue load
* Returns void but logs operation result
*/ */
async deleteJob(monitor) { async deleteJob(monitor) {
try { try {
const wasDeleted = await this.queue.removeRepeatable(monitor._id, { const queue = this.queues[QUEUE_LOOKUP[monitor.type]];
const wasDeleted = await queue.removeRepeatable(monitor._id, {
every: monitor.interval, every: monitor.interval,
}); });
if (wasDeleted === true) { if (wasDeleted === true) {
@@ -389,8 +456,8 @@ class JobQueue {
method: "deleteJob", method: "deleteJob",
details: `Deleted job ${monitor._id}`, details: `Deleted job ${monitor._id}`,
}); });
const workerStats = await this.getWorkerStats(); const workerStats = await this.getWorkerStats(queue);
await this.scaleWorkers(workerStats); await this.scaleWorkers(workerStats, queue);
} else { } else {
this.logger.error({ this.logger.error({
message: errorMessages.JOB_QUEUE_DELETE_JOB, message: errorMessages.JOB_QUEUE_DELETE_JOB,
@@ -407,21 +474,59 @@ class JobQueue {
} }
/** /**
* Retrieves the metrics of the job queue. * Retrieves comprehensive metrics for all queues
* @async
* @returns {Promise<Object.<string, QueueMetrics>>} Object with metrics for each queue
* @throws {Error} If metrics retrieval fails
* @description
* Collects the following metrics for each queue:
* - Number of waiting jobs
* - Number of active jobs
* - Number of completed jobs
* - Number of failed jobs
* - Number of delayed jobs
* - Number of repeatable jobs
* - Number of active workers
* *
* @returns {Promise<Object>} - An object containing various job queue metrics. * @typedef {Object} QueueMetrics
* @throws {Error} - Throws an error if the metrics retrieval fails. * @property {number} waiting - Count of jobs waiting to be processed
* @property {number} active - Count of jobs currently being processed
* @property {number} completed - Count of successfully completed jobs
* @property {number} failed - Count of failed jobs
* @property {number} delayed - Count of delayed jobs
* @property {number} repeatableJobs - Count of repeatable job patterns
* @property {number} workers - Count of active workers for this queue
*/ */
async getMetrics() { async getMetrics() {
try { try {
const metrics = { let metrics = {};
waiting: await this.queue.getWaitingCount(),
active: await this.queue.getActiveCount(), await Promise.all(
completed: await this.queue.getCompletedCount(), QUEUE_NAMES.map(async (name) => {
failed: await this.queue.getFailedCount(), const queue = this.queues[name];
delayed: await this.queue.getDelayedCount(), const workers = this.workers[name];
repeatableJobs: (await this.queue.getRepeatableJobs()).length, const [waiting, active, completed, failed, delayed, repeatableJobs] =
}; await Promise.all([
queue.getWaitingCount(),
queue.getActiveCount(),
queue.getCompletedCount(),
queue.getFailedCount(),
queue.getDelayedCount(),
queue.getRepeatableJobs(),
]);
metrics[name] = {
waiting,
active,
completed,
failed,
delayed,
repeatableJobs: repeatableJobs.length,
workers: workers.length,
};
})
);
return metrics; return metrics;
} catch (error) { } catch (error) {
this.logger.error({ this.logger.error({
@@ -441,23 +546,37 @@ class JobQueue {
async obliterate() { async obliterate() {
try { try {
this.logger.info({ message: "Attempting to obliterate job queue..." }); this.logger.info({ message: "Attempting to obliterate job queue..." });
await this.queue.pause();
const jobs = await this.getJobs();
// Remove all repeatable jobs
for (const job of jobs) {
await this.queue.removeRepeatableByKey(job.key);
await this.queue.remove(job.id);
}
// Close workers
await Promise.all( await Promise.all(
this.workers.map(async (worker) => { QUEUE_NAMES.map(async (name) => {
await worker.close(); const queue = this.queues[name];
await queue.pause();
const jobs = await this.getJobs(queue);
// Remove all repeatable jobs
for (const job of jobs) {
await queue.removeRepeatableByKey(job.key);
await queue.remove(job.id);
}
}) })
); );
await this.queue.obliterate(); // Close workers
await Promise.all(
QUEUE_NAMES.map(async (name) => {
const workers = this.workers[name];
await Promise.all(
workers.map(async (worker) => {
await worker.close();
})
);
})
);
QUEUE_NAMES.forEach(async (name) => {
const queue = this.queues[name];
await queue.obliterate();
});
const metrics = await this.getMetrics(); const metrics = await this.getMetrics();
this.logger.info({ this.logger.info({
message: successMessages.JOB_QUEUE_OBLITERATE, message: successMessages.JOB_QUEUE_OBLITERATE,
@@ -474,4 +593,4 @@ class JobQueue {
} }
} }
export default JobQueue; export default NewJobQueue;