UNPKG

@clipwhisperer/common

Version:

ClipWhisperer Common - Shared library providing core utilities, database schemas, authentication, bucket management, and common functionality across all ClipWhisperer microservices

116 lines (115 loc) 3.88 kB
"use strict"; /** * Queue Infrastructure * Centralized BullMQ queue and worker management */ Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseQueueManager = exports.QueueFactory = void 0; const bullmq_1 = require("bullmq"); const redis_1 = require("../constants/redis"); const performance_1 = require("./performance"); /** * Queue Factory - Creates standardized BullMQ queues */ class QueueFactory { /** * Create a queue with standard configuration */ static createQueue(queueName, config) { return new bullmq_1.Queue(queueName, { connection: (config === null || config === void 0 ? void 0 : config.connection) || redis_1.REDIS_CONNECTION_CONFIG, defaultJobOptions: { ...redis_1.DEFAULT_QUEUE_OPTIONS, ...config === null || config === void 0 ? void 0 : config.defaultJobOptions, }, }); } /** * Create a worker with standard configuration */ static createWorker(queueName, processor, config) { return new bullmq_1.Worker(queueName, processor, { connection: (config === null || config === void 0 ? void 0 : config.connection) || redis_1.REDIS_CONNECTION_CONFIG, ...config, }); } } exports.QueueFactory = QueueFactory; /** * Base Queue Manager - Provides common queue operations */ class BaseQueueManager { constructor(queueName, config) { this.queueName = queueName; this.config = config; this.queue = QueueFactory.createQueue(queueName, config); this.performanceTracker = new performance_1.PerformanceTracker(); } /** * Add a job to the queue */ async addJob(jobName, data, options) { const startTime = Date.now(); try { const job = await this.queue.add(jobName, data, { ...redis_1.DEFAULT_QUEUE_OPTIONS, ...options, }); this.performanceTracker.trackSuccess(Date.now() - startTime); return { jobId: job.id || `job_${Date.now()}` }; } catch (error) { this.performanceTracker.trackError(Date.now() - startTime); throw error; } } /** * Start the worker */ startWorker(processor) { this.worker = QueueFactory.createWorker(this.queueName, async (job) => { return await this.performanceTracker.trackRequest(async () => { return await processor(job); }); }, this.config); this.worker.on("completed", (job) => { console.log(`✅ Job ${job.id} completed successfully`); }); this.worker.on("failed", (job, err) => { console.error(`❌ Job ${job === null || job === void 0 ? void 0 : job.id} failed:`, err); }); } /** * Get queue statistics */ async getQueueStats() { return { waiting: await this.queue.getWaiting().then(jobs => jobs.length), active: await this.queue.getActive().then(jobs => jobs.length), completed: await this.queue.getCompleted().then(jobs => jobs.length), failed: await this.queue.getFailed().then(jobs => jobs.length), }; } /** * Get enhanced performance metrics including queue size */ async getPerformanceMetrics() { const baseMetrics = this.performanceTracker.getMetrics(); const queueStats = await this.getQueueStats(); return { ...baseMetrics, queueSize: queueStats.waiting + queueStats.active, queueStats, }; } /** * Close queue and worker connections */ async close() { await this.queue.close(); if (this.worker) { await this.worker.close(); } } } exports.BaseQueueManager = BaseQueueManager;