2026-05-18 10:17:06 +08:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2026-05-09 18:25:04 +08:00
|
|
|
import { DocumentImportRepository } from './document-import.repository';
|
|
|
|
|
import { RedisService } from '../../infrastructure/redis/redis.service';
|
|
|
|
|
import { QueueService } from '../../infrastructure/queue/queue.service';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class DocumentImportService {
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly repository: DocumentImportRepository,
|
|
|
|
|
private readonly redis: RedisService,
|
|
|
|
|
private readonly queue: QueueService,
|
|
|
|
|
) {}
|
|
|
|
|
|
2026-05-18 10:07:57 +08:00
|
|
|
async createImport(dto: {
|
|
|
|
|
userId?: string;
|
|
|
|
|
knowledgeBaseId?: string;
|
|
|
|
|
fileName?: string;
|
|
|
|
|
sourceType?: string;
|
|
|
|
|
rawText?: string;
|
|
|
|
|
}) {
|
2026-05-09 18:25:04 +08:00
|
|
|
const lockKey = `lock:document-import:${dto.fileName || Date.now()}`;
|
|
|
|
|
const lockToken = await this.redis.lock(lockKey, 1800);
|
|
|
|
|
if (!lockToken) {
|
|
|
|
|
throw new Error('相同文件正在导入中,请稍候');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const job = await this.repository.create(dto);
|
|
|
|
|
|
|
|
|
|
await this.redis.set(`job:document-import:${job.id}:status`, 'pending', 86400);
|
|
|
|
|
await this.redis.set(`job:document-import:${job.id}:progress`, '0', 86400);
|
|
|
|
|
await this.redis.set(`job:document-import:${job.id}:message`, '任务已加入队列', 86400);
|
|
|
|
|
|
2026-05-18 10:17:06 +08:00
|
|
|
await this.queue.add('document-import', {
|
2026-05-18 10:07:57 +08:00
|
|
|
importId: job.id,
|
|
|
|
|
userId: dto.userId || 'anonymous',
|
|
|
|
|
knowledgeBaseId: dto.knowledgeBaseId,
|
|
|
|
|
rawText: dto.rawText,
|
|
|
|
|
fileName: dto.fileName,
|
|
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
|
2026-05-18 10:17:06 +08:00
|
|
|
// Release the lock — the worker will re-lock if needed
|
|
|
|
|
await this.redis.unlock(lockKey, lockToken);
|
2026-05-18 10:07:57 +08:00
|
|
|
|
2026-05-18 10:17:06 +08:00
|
|
|
return { jobId: job.id, status: 'queued' };
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getStatus(id: string) {
|
|
|
|
|
const redisStatus = await this.redis.get(`job:document-import:${id}:status`);
|
|
|
|
|
const redisProgress = await this.redis.get(`job:document-import:${id}:progress`);
|
|
|
|
|
const redisMessage = await this.redis.get(`job:document-import:${id}:message`);
|
|
|
|
|
const dbJob = await this.repository.findById(id);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id,
|
2026-05-17 00:39:46 +08:00
|
|
|
fileName: dbJob?.sourceName,
|
2026-05-09 18:25:04 +08:00
|
|
|
status: redisStatus || dbJob?.status || 'unknown',
|
|
|
|
|
progress: redisProgress ? parseInt(redisProgress, 10) : 0,
|
|
|
|
|
message: redisMessage || null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|