Skip to content

Commit 7047b76

Browse files
committed
refactor(image): optimize image processing with AsyncQueue
- Replaced the promise-based image processing with an AsyncQueue to manage concurrent tasks more efficiently. - Improved error handling and logging during image size and metadata retrieval. - Updated the addMultiple method in AsyncQueue to return a wait function for better task management. Signed-off-by: Innei <tukon479@gmail.com>
1 parent 7229ccf commit 7047b76

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

apps/core/src/processors/helper/helper.image.service.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Injectable, Logger, OnModuleInit } from '@nestjs/common'
66

77
import { ConfigsService } from '~/modules/configs/configs.service'
88
import { pickImagesFromMarkdown } from '~/utils/pic.util'
9+
import { AsyncQueue } from '~/utils/queue.util'
910
import { requireDepsWithInstall } from '~/utils/tool.util'
1011

1112
import { HttpService } from './helper.http.service'
@@ -41,8 +42,8 @@ export class ImageService implements OnModuleInit {
4142
(originImages ?? []).map((image) => [image.src, { ...image }]),
4243
)
4344

44-
const task = [] as Promise<ImageModel>[]
45-
for (const src of newImages) {
45+
const queue = new AsyncQueue(2)
46+
const imageProcessingTasks = newImages.map((src) => async () => {
4647
const originImage = oldImagesMap.get(src)
4748
const keys = new Set(Object.keys(originImage || {}))
4849

@@ -55,44 +56,43 @@ export class ImageService implements OnModuleInit {
5556
)
5657
) {
5758
result.push(originImage)
58-
continue
59+
return
5960
}
60-
const promise = new Promise<ImageModel>((resolve) => {
61+
62+
try {
6163
this.logger.log(`Get --> ${src}`)
62-
this.getOnlineImageSizeAndMeta(src)
63-
.then(({ size, accent, blurHash }) => {
64-
const filename = src.split('/').pop()
65-
this.logger.debug(
66-
`[${filename}]: height: ${size.height}, width: ${size.width}, accent: ${accent}`,
67-
)
68-
69-
resolve({ ...size, accent, src, blurHash })
70-
})
71-
.catch((error) => {
72-
this.logger.error(`GET --> ${src} ${error.message}`)
73-
74-
const oldRecord = oldImagesMap.get(src)
75-
if (oldRecord) {
76-
resolve(oldRecord)
77-
} else
78-
resolve({
79-
width: undefined,
80-
height: undefined,
81-
type: undefined,
82-
accent: undefined,
83-
src: undefined,
84-
blurHash: undefined,
85-
})
64+
const { size, accent, blurHash } =
65+
await this.getOnlineImageSizeAndMeta(src)
66+
const filename = src.split('/').pop()
67+
this.logger.debug(
68+
`[${filename}]: height: ${size.height}, width: ${size.width}, accent: ${accent}`,
69+
)
70+
71+
result.push({ ...size, accent, src, blurHash })
72+
} catch (error) {
73+
this.logger.error(`GET --> ${src} ${error.message}`)
74+
75+
const oldRecord = oldImagesMap.get(src)
76+
if (oldRecord) {
77+
result.push(oldRecord)
78+
} else {
79+
result.push({
80+
width: undefined,
81+
height: undefined,
82+
type: undefined,
83+
accent: undefined,
84+
src: undefined,
85+
blurHash: undefined,
8686
})
87-
})
87+
}
88+
}
89+
})
8890

89-
task.push(promise)
90-
}
91-
const images = await Promise.all(task)
92-
result.push(...images)
91+
// Add all tasks to the queue and wait for completion
92+
const wait = queue.addMultiple(imageProcessingTasks)
93+
await wait()
9394

9495
// 老图片不要过滤,记录到列头
95-
9696
if (originImages) {
9797
for (const oldImageRecord of originImages) {
9898
const src = oldImageRecord.src

apps/core/src/utils/queue.util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class AsyncQueue {
3434

3535
addMultiple(requests: (() => Promise<any>)[]) {
3636
this.queue.push(...requests)
37-
this.runNext()
37+
const wait = this.runNext()
38+
return async () => await wait
3839
}
3940
}

0 commit comments

Comments
 (0)