fix(ai): ensure proper task cleanup in AiDeepReadingService

- Moved the deletion of the Redis task ID to the finally block to guarantee it is always executed, preventing potential memory leaks.
- Removed redundant Redis client initialization to streamline the code.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-05-07 00:27:47 +08:00
parent 991cb0aeca
commit f7cd8c7035
2 changed files with 4 additions and 6 deletions

View File

@@ -225,11 +225,11 @@ export class AiDeepReadingService {
}
const taskId = `ai:deepreading:${articleId}`
const redis = this.redisService.getClient()
try {
if (this.cachedTaskId2AiPromise.has(taskId)) {
return this.cachedTaskId2AiPromise.get(taskId)
}
const redis = this.redisService.getClient()
const isProcessing = await redis.get(taskId)
@@ -256,8 +256,6 @@ export class AiDeepReadingService {
const result = await this.deepReadingAgentChain(id)
await redis.del(taskId)
const contentMd5 = md5(text)
const doc = await this.aiDeepReadingModel.create({
@@ -283,6 +281,7 @@ export class AiDeepReadingService {
)
} finally {
this.cachedTaskId2AiPromise.delete(taskId)
await redis.del(taskId)
}
}

View File

@@ -113,11 +113,11 @@ export class AiSummaryService {
}
const taskId = `ai:summary:${articleId}:${lang}`
const redis = this.redisService.getClient()
try {
if (this.cachedTaskId2AiPromise.has(taskId)) {
return this.cachedTaskId2AiPromise.get(taskId)
}
const redis = this.redisService.getClient()
const isProcessing = await redis.get(taskId)
@@ -140,8 +140,6 @@ export class AiSummaryService {
const summary = await this.summaryChain(id, lang)
await redis.del(taskId)
const contentMd5 = md5(text)
const doc = await this.aiSummaryModel.create({
@@ -161,6 +159,7 @@ export class AiSummaryService {
throw new BizException(ErrorCodeEnum.AIException, error.message)
} finally {
this.cachedTaskId2AiPromise.delete(taskId)
await redis.del(taskId)
}
}