2026-05-23 19:27:46 +08:00
|
|
|
import { Injectable, Logger, Optional } from '@nestjs/common';
|
2026-05-17 19:08:07 +08:00
|
|
|
import { InjectQueue } from '@nestjs/bullmq';
|
|
|
|
|
import { Queue } from 'bullmq';
|
|
|
|
|
|
|
|
|
|
export const QUEUE_AI_ANALYSIS = 'ai-analysis';
|
|
|
|
|
export const QUEUE_DOCUMENT_IMPORT = 'document-import';
|
|
|
|
|
export const QUEUE_NOTIFICATION = 'notification';
|
2026-05-22 23:03:32 +08:00
|
|
|
export const QUEUE_AUDIT_LOG = 'audit-logs';
|
2026-05-23 09:47:30 +08:00
|
|
|
export const QUEUE_FILE_CLEANUP = 'file-cleanup';
|
2026-05-09 18:25:04 +08:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class QueueService {
|
2026-05-17 19:08:07 +08:00
|
|
|
private readonly logger = new Logger(QueueService.name);
|
2026-05-09 18:25:04 +08:00
|
|
|
|
2026-05-17 19:08:07 +08:00
|
|
|
constructor(
|
|
|
|
|
@InjectQueue(QUEUE_AI_ANALYSIS) private readonly aiQueue: Queue,
|
|
|
|
|
@InjectQueue(QUEUE_DOCUMENT_IMPORT) private readonly importQueue: Queue,
|
|
|
|
|
@InjectQueue(QUEUE_NOTIFICATION) private readonly notifyQueue: Queue,
|
|
|
|
|
) {}
|
|
|
|
|
|
2026-05-23 19:27:46 +08:00
|
|
|
async add(queueName: string, data: any, opts?: { jobId?: string; attempts?: number; backoff?: number }) {
|
2026-05-17 19:08:07 +08:00
|
|
|
const queue = this.getQueue(queueName);
|
2026-05-23 19:27:46 +08:00
|
|
|
const job = await queue.add(queueName, data, opts || {});
|
2026-05-17 19:08:07 +08:00
|
|
|
this.logger.log(`Job ${job.id} added to ${queueName}`);
|
|
|
|
|
return job;
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 19:08:07 +08:00
|
|
|
async getJob(queueName: string, jobId: string) {
|
|
|
|
|
const queue = this.getQueue(queueName);
|
|
|
|
|
return queue.getJob(jobId);
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 19:08:07 +08:00
|
|
|
private getQueue(name: string): Queue {
|
|
|
|
|
switch (name) {
|
|
|
|
|
case QUEUE_AI_ANALYSIS: return this.aiQueue;
|
|
|
|
|
case QUEUE_DOCUMENT_IMPORT: return this.importQueue;
|
|
|
|
|
case QUEUE_NOTIFICATION: return this.notifyQueue;
|
|
|
|
|
default: throw new Error(`Unknown queue: ${name}`);
|
|
|
|
|
}
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
}
|