UNPKG

@srttk/queue

Version:

BullMQ queues simplified.

216 lines (212 loc) 5.82 kB
'use strict'; var bullmq = require('bullmq'); const DEFAULT_NAMESPACE = "default_queues"; const DEFAULT_QUEUE_OPTIONS = {}; const DEFAULT_WORKER_OPTIONS = {}; const DEFAULT_JOB_OPTIONS = {}; class QueueManager { _queueMap; queues = []; workers = []; config; connection; shutdownTriggered = false; // Default options _defaultQueueOptions = DEFAULT_QUEUE_OPTIONS; _defaultWorkerOptions = DEFAULT_WORKER_OPTIONS; _defaultJobOptions = DEFAULT_JOB_OPTIONS; constructor(queueMaps, config) { this._queueMap = queueMaps; this.config = config; if (config?.connection) { this.connection = config.connection; } } get namespace() { return this?.config?.namespace || DEFAULT_NAMESPACE; } startQueues() { if (!this._queueMap) { return null; } let queues = Object.values(this._queueMap); if (!queues?.length) { return null; } queues.map((q) => { let _options = { ...this._defaultQueueOptions, ...q?.options || {} }; let _queue = new bullmq.Queue(q.name, { prefix: this.namespace, connection: this.connection, ..._options }); this.queues.push(_queue); }); return this.queues; } startWorkers(groupName = null) { let _queueMapsValues = Object.values(this._queueMap).filter((qp) => { if (groupName) { return qp.groupName === groupName; } return qp; }); _queueMapsValues.forEach((qp) => { let exists = this.workers.find((w) => w.name === qp.name); if (!exists) { let _options = { ...this._defaultWorkerOptions, ...qp.workerOptions || {} }; let _w = new bullmq.Worker(qp.name, qp.process, { connection: this.connection, prefix: this.namespace, ..._options }); if (qp?.onActive) { _w.on("active", qp.onActive); } if (qp?.onClosed) { _w.on("closed", qp.onClosed); } if (qp?.onCompleted) { _w.on("completed", qp.onCompleted); } if (qp?.onError) { _w.on("error", qp.onError); } if (qp?.onDrained) { _w.on("drained", qp.onDrained); } if (qp?.onFailed) { _w.on("failed", qp.onFailed); } if (qp?.onPaused) { _w.on("paused", qp.onPaused); } if (qp?.onProgress) { _w.on("progress", qp.onProgress); } if (qp?.onReady) { _w.on("ready", qp.onReady); } if (qp?.onStalled) { _w.on("stalled", qp.onStalled); } this.workers.push(_w); } }); } // Add job async addJob(name, jobname, payload, jobOptions) { let qMapItem = this.getFromMapByName(name); if (!qMapItem) { console.warn(`Queue ${String(name)} not found.`); return null; } let queue = this.getQueueByName(qMapItem.name); if (!queue) { console.warn(`Queue ${String(name)} not initialized.`); return null; } let _jobOptions = { ...this._defaultJobOptions, ...qMapItem.defaultJobOptions || {}, ...jobOptions || {} }; let job = await queue.add(jobname, payload, _jobOptions); return job; } // Add to scheduler async scheduleJob(name, schedulerId, repeat, payload) { let qMapItem = this.getFromMapByName(name); if (!qMapItem) { console.warn(`Queue ${String(name)} not found.`); return null; } let queue = this.getQueueByName(qMapItem.name); if (!queue) { console.warn(`Queue ${String(name)} not initialized.`); return null; } return await queue.upsertJobScheduler(schedulerId, repeat, payload); } async removeScheduleJob(name, schedulerId) { let qMapItem = this.getFromMapByName(name); if (!qMapItem) { console.warn(`Queue ${String(name)} not found.`); return null; } let queue = this.getQueueByName(qMapItem.name); if (!queue) { console.warn(`Queue ${String(name)} not initialized.`); return null; } return await queue.removeJobScheduler(schedulerId); } // Get all registered queues getQueues() { return this.queues; } // Get queue by name getQueue(name) { let mapItem = this.getFromMapByName(name); if (!mapItem) return; return this.getQueueByName(mapItem.name); } // Get all registered workers getWorkers() { return this.workers; } // Get worker by name getWorker(name) { let mapItem = this.getFromMapByName(name); if (!mapItem) return; return this.getWorkerByName(mapItem.name); } // Get IQueueProcess map item from `this.pmap` getFromMapByName(name) { let mapItem = this._queueMap[name]; if (!mapItem) return; return mapItem; } // Get initialized Bullmq queue getQueueByName(name) { let _queue = this.queues.find((queue) => queue.name === name); return _queue; } // Get initialized Bullmq worker getWorkerByName(name) { let _worker = this.workers.find((worker) => worker.name === name); return _worker; } // Shutdown all async shutdown() { if (this.shutdownTriggered) { return; } this.shutdownTriggered = true; if (this.workers?.length) { let $pauseWorkers = this.workers.map((w) => { return w.pause(); }); await Promise.all($pauseWorkers); } if (this.workers?.length) { let $closeWorkers = this.workers.map((w) => { return w.close(); }); await Promise.all($closeWorkers); } if (this.queues?.length) { let $closeQueues = this.queues.map((q) => { return q.close(); }); await Promise.all($closeQueues); } } setConnection(conn) { this.connection = conn; } } exports.QueueManager = QueueManager;