debug: add detailed logging to DocumentImportService.createImport
All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 49s
All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 49s
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
4f614f9d1f
commit
433e0095ee
@ -1,10 +1,12 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { DocumentImportRepository } from './document-import.repository';
|
import { DocumentImportRepository } from './document-import.repository';
|
||||||
import { RedisService } from '../../infrastructure/redis/redis.service';
|
import { RedisService } from '../../infrastructure/redis/redis.service';
|
||||||
import { QueueService } from '../../infrastructure/queue/queue.service';
|
import { QueueService } from '../../infrastructure/queue/queue.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DocumentImportService {
|
export class DocumentImportService {
|
||||||
|
private readonly logger = new Logger(DocumentImportService.name);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly repository: DocumentImportRepository,
|
private readonly repository: DocumentImportRepository,
|
||||||
private readonly redis: RedisService,
|
private readonly redis: RedisService,
|
||||||
@ -18,18 +20,24 @@ export class DocumentImportService {
|
|||||||
sourceType?: string;
|
sourceType?: string;
|
||||||
rawText?: string;
|
rawText?: string;
|
||||||
}) {
|
}) {
|
||||||
|
this.logger.log(`createImport called: userId=${dto.userId}, kbId=${dto.knowledgeBaseId}, fileName=${dto.fileName}, sourceType=${dto.sourceType}`);
|
||||||
|
try {
|
||||||
const lockKey = `lock:document-import:${dto.fileName || Date.now()}`;
|
const lockKey = `lock:document-import:${dto.fileName || Date.now()}`;
|
||||||
|
this.logger.log(`Attempting Redis lock: ${lockKey}`);
|
||||||
const lockToken = await this.redis.lock(lockKey, 1800);
|
const lockToken = await this.redis.lock(lockKey, 1800);
|
||||||
if (!lockToken) {
|
if (!lockToken) {
|
||||||
throw new Error('相同文件正在导入中,请稍候');
|
throw new Error('相同文件正在导入中,请稍候');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.log(`Creating DB record...`);
|
||||||
const job = await this.repository.create(dto);
|
const job = await this.repository.create(dto);
|
||||||
|
this.logger.log(`DB record created: ${job.id}`);
|
||||||
|
|
||||||
await this.redis.set(`job:document-import:${job.id}:status`, 'pending', 86400);
|
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}:progress`, '0', 86400);
|
||||||
await this.redis.set(`job:document-import:${job.id}:message`, '任务已加入队列', 86400);
|
await this.redis.set(`job:document-import:${job.id}:message`, '任务已加入队列', 86400);
|
||||||
|
|
||||||
|
this.logger.log(`Enqueuing job...`);
|
||||||
await this.queue.add('document-import', {
|
await this.queue.add('document-import', {
|
||||||
importId: job.id,
|
importId: job.id,
|
||||||
userId: dto.userId || 'anonymous',
|
userId: dto.userId || 'anonymous',
|
||||||
@ -37,11 +45,15 @@ export class DocumentImportService {
|
|||||||
rawText: dto.rawText,
|
rawText: dto.rawText,
|
||||||
fileName: dto.fileName,
|
fileName: dto.fileName,
|
||||||
});
|
});
|
||||||
|
this.logger.log(`Job enqueued: ${job.id}`);
|
||||||
|
|
||||||
// Release the lock — the worker will re-lock if needed
|
|
||||||
await this.redis.unlock(lockKey, lockToken);
|
await this.redis.unlock(lockKey, lockToken);
|
||||||
|
|
||||||
return { jobId: job.id, status: 'queued' };
|
return { jobId: job.id, status: 'queued' };
|
||||||
|
} catch (err: any) {
|
||||||
|
this.logger.error(`createImport failed: ${err.message}`, err.stack);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getStatus(id: string) {
|
async getStatus(id: string) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user