Skip to content

Commit f7cd8c7

Browse files
committed
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>
1 parent 991cb0a commit f7cd8c7

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

apps/core/src/modules/ai/ai-deep-reading/ai-deep-reading.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ export class AiDeepReadingService {
225225
}
226226

227227
const taskId = `ai:deepreading:${articleId}`
228+
const redis = this.redisService.getClient()
228229
try {
229230
if (this.cachedTaskId2AiPromise.has(taskId)) {
230231
return this.cachedTaskId2AiPromise.get(taskId)
231232
}
232-
const redis = this.redisService.getClient()
233233

234234
const isProcessing = await redis.get(taskId)
235235

@@ -256,8 +256,6 @@ export class AiDeepReadingService {
256256

257257
const result = await this.deepReadingAgentChain(id)
258258

259-
await redis.del(taskId)
260-
261259
const contentMd5 = md5(text)
262260

263261
const doc = await this.aiDeepReadingModel.create({
@@ -283,6 +281,7 @@ export class AiDeepReadingService {
283281
)
284282
} finally {
285283
this.cachedTaskId2AiPromise.delete(taskId)
284+
await redis.del(taskId)
286285
}
287286
}
288287

apps/core/src/modules/ai/ai-summary/ai-summary.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ export class AiSummaryService {
113113
}
114114

115115
const taskId = `ai:summary:${articleId}:${lang}`
116+
const redis = this.redisService.getClient()
116117
try {
117118
if (this.cachedTaskId2AiPromise.has(taskId)) {
118119
return this.cachedTaskId2AiPromise.get(taskId)
119120
}
120-
const redis = this.redisService.getClient()
121121

122122
const isProcessing = await redis.get(taskId)
123123

@@ -140,8 +140,6 @@ export class AiSummaryService {
140140

141141
const summary = await this.summaryChain(id, lang)
142142

143-
await redis.del(taskId)
144-
145143
const contentMd5 = md5(text)
146144

147145
const doc = await this.aiSummaryModel.create({
@@ -161,6 +159,7 @@ export class AiSummaryService {
161159
throw new BizException(ErrorCodeEnum.AIException, error.message)
162160
} finally {
163161
this.cachedTaskId2AiPromise.delete(taskId)
162+
await redis.del(taskId)
164163
}
165164
}
166165

0 commit comments

Comments
 (0)