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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user