|
| 1 | +import { Logger } from '@nestjs/common'; |
| 2 | +import { ConfigService } from '@nestjs/config'; |
| 3 | +import { ConnectionOptions, Job, Queue, QueueEvents } from 'bullmq'; |
| 4 | +import Redis from 'ioredis'; |
| 5 | +import { |
| 6 | + SesameQueueAdapter, |
| 7 | + SesameQueueEventsEmitter, |
| 8 | + SesameSubmittedJob, |
| 9 | +} from '~/_common/interfaces/sesame-job.interface'; |
| 10 | + |
| 11 | +class BullmqQueueEventsEmitter implements SesameQueueEventsEmitter { |
| 12 | + public constructor(private readonly queueEvents: QueueEvents) {} |
| 13 | + |
| 14 | + public on(event: string, handler: (...args: any[]) => void): void { |
| 15 | + // BullMQ event names are validated at runtime |
| 16 | + (this.queueEvents as any).on(event, handler); |
| 17 | + } |
| 18 | + |
| 19 | + public off(event: string, handler: (...args: any[]) => void): void { |
| 20 | + (this.queueEvents as any).off(event, handler); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +export class BullmqQueueAdapter implements SesameQueueAdapter { |
| 25 | + private readonly _logger = new Logger(BullmqQueueAdapter.name); |
| 26 | + private _queue: Queue; |
| 27 | + private _queueEvents: QueueEvents; |
| 28 | + private _eventsEmitter: BullmqQueueEventsEmitter; |
| 29 | + |
| 30 | + public constructor( |
| 31 | + private readonly redis: Redis, |
| 32 | + private readonly config: ConfigService, |
| 33 | + ) {} |
| 34 | + |
| 35 | + public get events(): SesameQueueEventsEmitter { |
| 36 | + return this._eventsEmitter; |
| 37 | + } |
| 38 | + |
| 39 | + public async connect(): Promise<void> { |
| 40 | + const queueName = this.config.get<string>('application.nameQueue'); |
| 41 | + const connection = this.redis as unknown as ConnectionOptions; |
| 42 | + |
| 43 | + this._queue = new Queue(queueName, { connection }); |
| 44 | + this._queueEvents = new QueueEvents(queueName, { connection }); |
| 45 | + this._eventsEmitter = new BullmqQueueEventsEmitter(this._queueEvents); |
| 46 | + this._logger.log(`BullMQ queue "${queueName}" ready`); |
| 47 | + } |
| 48 | + |
| 49 | + public async close(): Promise<void> { |
| 50 | + await this._queueEvents?.close(); |
| 51 | + await this._queue?.close(); |
| 52 | + } |
| 53 | + |
| 54 | + public async add( |
| 55 | + name: string, |
| 56 | + data: Record<string, unknown>, |
| 57 | + options?: { jobId?: string; attempts?: number }, |
| 58 | + ): Promise<SesameSubmittedJob> { |
| 59 | + const job = await this._queue.add(name, data, { |
| 60 | + jobId: options?.jobId, |
| 61 | + attempts: options?.attempts ?? 1, |
| 62 | + }); |
| 63 | + return this.toSubmittedJob(job); |
| 64 | + } |
| 65 | + |
| 66 | + public async getCompleted(): Promise<Array<{ id: string; name: string; returnvalue: unknown }>> { |
| 67 | + const jobs = await this._queue.getCompleted(); |
| 68 | + return jobs.map((job) => ({ |
| 69 | + id: String(job.id), |
| 70 | + name: job.name, |
| 71 | + returnvalue: job.returnvalue, |
| 72 | + })); |
| 73 | + } |
| 74 | + |
| 75 | + private toSubmittedJob(job: Job): SesameSubmittedJob { |
| 76 | + return { |
| 77 | + id: String(job.id), |
| 78 | + waitUntilFinished: (timeoutMs) => job.waitUntilFinished(this._queueEvents, timeoutMs), |
| 79 | + getState: () => job.getState(), |
| 80 | + discard: async () => { |
| 81 | + await job.discard(); |
| 82 | + }, |
| 83 | + }; |
| 84 | + } |
| 85 | +} |
0 commit comments