Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat: implement unified retry mechanism across services

Timothy Yin086c51111cc7
diff --git a/app/components/masonry/item/Photo.vue b/app/components/masonry/item/Photo.vue
index 0eaac9d8..84aa81cf 100644
--- a/app/components/masonry/item/Photo.vue
+++ b/app/components/masonry/item/Photo.vue
@@ -26,7 +26,7 @@ const isVideoPlaying = ref(false)
const isVideoLoaded = ref(false)
const videoBlob = ref<Blob | null>(null)
const videoBlobUrl = ref<string | null>(null)
-const { convertMovToMp4, getProcessingState, preloadLivePhotosInViewport } = useLivePhotoProcessor()
+const { convertMovToMp4, getProcessingState } = useLivePhotoProcessor()
const isTouching = ref(false)
const touchCount = ref(0)
@@ -139,14 +139,14 @@ const playLivePhotoVideo = () => {
// 延迟播放以确保动画状态已设置
nextTick(() => {
if (!videoRef.value || !isVideoPlaying.value) return
-
+
// 设置视频播放属性
videoRef.value.muted = true // 确保静音播放
videoRef.value.playsInline = true
-
+
// 立即尝试播放,使用更好的错误处理
const playPromise = videoRef.value.play()
-
+
if (playPromise !== undefined) {
playPromise
.then(() => {
@@ -158,7 +158,7 @@ const playLivePhotoVideo = () => {
.catch((error: any) => {
console.warn('Failed to play LivePhoto video:', error)
isVideoPlaying.value = false
-
+
// 如果是因为用户交互策略导致的失败,尝试重新加载
if (error.name === 'NotAllowedError') {
console.log('Video play blocked by browser policy, retrying...')
@@ -309,7 +309,7 @@ const processLivePhotoWhenVisible = async () => {
props.photo.livePhotoVideoUrl,
props.photo.id,
)
-
+
if (blob) {
videoBlob.value = blob
// Clean up previous blob URL
@@ -318,7 +318,7 @@ const processLivePhotoWhenVisible = async () => {
}
videoBlobUrl.value = URL.createObjectURL(blob)
isVideoLoaded.value = true
-
+
// 预热视频元素以提高播放性能
if (videoRef.value) {
videoRef.value.load()
@@ -330,26 +330,6 @@ const processLivePhotoWhenVisible = async () => {
}
}
-// 预加载附近的LivePhoto(性能优化)
-const preloadNearbyLivePhotos = async () => {
- if (!isVisible.value) return
-
- // 这里可以从父组件接收附近照片的信息
- // 目前只处理当前照片
- if (props.photo.isLivePhoto && props.photo.livePhotoVideoUrl) {
- await preloadLivePhotosInViewport([
- {
- id: props.photo.id,
- livePhotoVideoUrl: props.photo.livePhotoVideoUrl,
- isVisible: isVisible.value
- }
- ], {
- maxConcurrent: 1,
- prioritizeVisible: true
- })
- }
-}
-
const formatExposureTime = (
exposureTime: string | number | undefined,
): string => {
@@ -529,9 +509,9 @@ onUnmounted(() => {
muted
playsinline
preload="metadata"
- :initial="{
+ :initial="{
opacity: 0,
- scale: 1.02
+ scale: 1.02,
}"
:animate="{
opacity: isVideoPlaying ? 1 : 0,
@@ -539,17 +519,21 @@ onUnmounted(() => {
}"
:transition="{
duration: isVideoPlaying ? 0.3 : 0.2,
- ease: isVideoPlaying ? [0.23, 1, 0.32, 1] : [0.25, 0.46, 0.45, 0.94],
+ ease: isVideoPlaying
+ ? [0.23, 1, 0.32, 1]
+ : [0.25, 0.46, 0.45, 0.94],
delay: isVideoPlaying ? 0.05 : 0,
}"
@ended="handleVideoEnded"
- @loadeddata="() => {
- // 视频加载完成后预热
- if (videoRef && !isVideoPlaying) {
- videoRef.currentTime = 0.1
- videoRef.pause()
+ @loadeddata="
+ () => {
+ // 视频加载完成后预热
+ if (videoRef && !isVideoPlaying) {
+ videoRef.currentTime = 0.1
+ videoRef.pause()
+ }
}
- }"
+ "
/>
</div>
diff --git a/app/components/upload/UploadQueueContainer.vue b/app/components/upload/UploadQueueContainer.vue
index c3de6390..5fa4c745 100644
--- a/app/components/upload/UploadQueueContainer.vue
+++ b/app/components/upload/UploadQueueContainer.vue
@@ -1,6 +1,5 @@
<script lang="ts" setup>
import { motion, AnimatePresence } from 'motion-v'
-import { computed, ref, watch } from 'vue'
interface UploadFile {
file: File
@@ -43,11 +42,13 @@ const stats = computed(() => {
const files = Array.from(props.uploadingFiles.values())
return {
total: files.length,
- uploading: files.filter(f => f.status === 'uploading').length,
- processing: files.filter(f => f.status === 'processing').length,
- completed: files.filter(f => f.status === 'completed').length,
- error: files.filter(f => f.status === 'error').length,
- active: files.filter(f => f.status === 'uploading' || f.status === 'processing').length,
+ uploading: files.filter((f) => f.status === 'uploading').length,
+ processing: files.filter((f) => f.status === 'processing').length,
+ completed: files.filter((f) => f.status === 'completed').length,
+ error: files.filter((f) => f.status === 'error').length,
+ active: files.filter(
+ (f) => f.status === 'uploading' || f.status === 'processing',
+ ).length,
}
})
@@ -55,9 +56,9 @@ const stats = computed(() => {
const overallProgress = computed(() => {
const files = Array.from(props.uploadingFiles.values())
if (files.length === 0) return 0
-
+
let totalProgress = 0
- files.forEach(file => {
+ files.forEach((file) => {
if (file.status === 'completed') {
totalProgress += 100
} else if (file.status === 'uploading' && file.progress !== undefined) {
@@ -66,7 +67,7 @@ const overallProgress = computed(() => {
totalProgress += 50 // 估算处理进度为50%
}
})
-
+
return Math.round(totalProgress / files.length)
})
@@ -113,48 +114,61 @@ const clearAllFiles = () => {
<!-- 头部 -->
<motion.div
class="p-4 border-b border-neutral-200 dark:border-neutral-700 cursor-pointer"
+ :while-hover="{ backgroundColor: 'rgba(0,0,0,0.02)' }"
+ :while-tap="{ scale: 0.98 }"
@click="toggleCollapsed"
- :whileHover="{ backgroundColor: 'rgba(0,0,0,0.02)' }"
- :whileTap="{ scale: 0.98 }"
>
<div class="flex items-center justify-between">
<div class="flex items-center gap-3">
<!-- 状态指示器 -->
<motion.div
class="w-3 h-3 rounded-full"
- :class="{
- 'bg-blue-500': statusColor === 'primary',
- 'bg-green-500': statusColor === 'success',
- 'bg-red-500': statusColor === 'error',
- 'bg-neutral-400': statusColor === 'neutral'
- }"
+ :class="{
+ 'bg-blue-500': statusColor === 'primary',
+ 'bg-green-500': statusColor === 'success',
+ 'bg-red-500': statusColor === 'error',
+ 'bg-neutral-400': statusColor === 'neutral',
+ }"
:animate="{
scale: stats.active > 0 ? [1, 1.2, 1] : 1,
- opacity: stats.active > 0 ? [0.7, 1, 0.7] : 1
+ opacity: stats.active > 0 ? [0.7, 1, 0.7] : 1,
}"
:transition="{
duration: 2,
- repeat: stats.active > 0 ? Infinity : 0
+ repeat: stats.active > 0 ? Infinity : 0,
}"
/>
-
+
<!-- 标题和统计 -->
<div>
- <h3 class="font-semibold text-sm text-neutral-900 dark:text-neutral-100">
+ <h3
+ class="font-semibold text-sm text-neutral-900 dark:text-neutral-100"
+ >
文件上传队列
<span class="text-neutral-500 dark:text-neutral-400">
({{ stats.total }})
</span>
</h3>
-
- <div class="flex items-center gap-2 text-xs text-neutral-500 dark:text-neutral-400 mt-1">
- <span v-if="stats.active > 0" class="text-blue-600 dark:text-blue-400">
+
+ <div
+ class="flex items-center gap-2 text-xs text-neutral-500 dark:text-neutral-400 mt-1"
+ >
+ <span
+ v-if="stats.active > 0"
+ class="text-blue-600 dark:text-blue-400"
+ >
{{ stats.active }} 进行中
</span>
- <span v-if="stats.completed > 0" class="text-green-600 dark:text-green-400">
+ <span
+ v-if="stats.completed > 0"
+ class="text-green-600 dark:text-green-400"
+ >
{{ stats.completed }} 完成
</span>
- <span v-if="stats.error > 0" class="text-red-600 dark:text-red-400">
+ <span
+ v-if="stats.error > 0"
+ class="text-red-600 dark:text-red-400"
+ >
{{ stats.error }} 失败
</span>
</div>
@@ -169,7 +183,7 @@ const clearAllFiles = () => {
>
{{ overallProgress }}%
</div>
-
+
<!-- 折叠图标 -->
<motion.div
:animate="{ rotate: isCollapsed ? 0 : 180 }"
@@ -196,7 +210,6 @@ const clearAllFiles = () => {
<UProgress
:model-value="overallProgress"
:color="statusColor"
- class="h-1.5"
/>
</motion.div>
</motion.div>
@@ -239,7 +252,7 @@ const clearAllFiles = () => {
<div class="text-xs text-neutral-500 dark:text-neutral-400">
{{ stats.completed }} 完成, {{ stats.error }} 失败
</div>
-
+
<div class="flex items-center gap-2">
<UButton
v-if="stats.completed > 0"
@@ -250,11 +263,11 @@ const clearAllFiles = () => {
>
清除已完成
</UButton>
-
+
<UButton
size="xs"
variant="ghost"
- color="red"
+ color="error"
@click="clearAllFiles"
>
清除全部
diff --git a/app/pages/dashboard/photos.vue b/app/pages/dashboard/photos.vue
index d14ffeb0..b4364306 100644
--- a/app/pages/dashboard/photos.vue
+++ b/app/pages/dashboard/photos.vue
@@ -121,10 +121,11 @@ const uploadImage = async (file: File) => {
try {
// 检查是否为MOV视频文件(通过MIME类型或文件扩展名)
- const isMovFile = file.type === 'video/quicktime' ||
- file.type === 'video/mp4' ||
- file.name.toLowerCase().endsWith('.mov')
-
+ const isMovFile =
+ file.type === 'video/quicktime' ||
+ file.type === 'video/mp4' ||
+ file.name.toLowerCase().endsWith('.mov')
+
const resp = await $fetch('/api/queue/add-task', {
method: 'POST',
body: {
@@ -266,10 +267,10 @@ const startTaskStatusCheck = (taskId: number, fileId: string) => {
await refresh()
// 2秒后从界面移除成功的任务
- setTimeout(() => {
- uploadingFiles.value.delete(fileId)
- uploadingFiles.value = new Map(uploadingFiles.value)
- }, 2000)
+ // setTimeout(() => {
+ // uploadingFiles.value.delete(fileId)
+ // uploadingFiles.value = new Map(uploadingFiles.value)
+ // }, 2000)
} else if (response.status === 'failed') {
// 任务失败
uploadingFile.status = 'error'
@@ -636,10 +637,10 @@ const handleUpload = async () => {
// 并发上传,但限制并发数量以避免资源竞争
const CONCURRENT_LIMIT = 3 // 限制同时上传的文件数量
const uploadPromises: Promise<void>[] = []
-
+
for (let i = 0; i < validFiles.length; i += CONCURRENT_LIMIT) {
const batch = validFiles.slice(i, i + CONCURRENT_LIMIT)
-
+
const batchPromises = batch.map(async (file) => {
try {
await uploadImage(file)
@@ -650,12 +651,12 @@ const handleUpload = async () => {
console.error('上传错误:', error)
}
})
-
+
uploadPromises.push(...batchPromises)
-
+
// 等待当前批次完成再处理下一批次
await Promise.allSettled(batchPromises)
-
+
// 更新进度
const processed = Math.min(i + CONCURRENT_LIMIT, validFiles.length)
toast.update(uploadToast.id, {
diff --git a/docs/retry-mechanism.md b/docs/retry-mechanism.md
new file mode 100644
index 00000000..4a9bc3f4
--- /dev/null
+++ b/docs/retry-mechanism.md
@@ -0,0 +1,252 @@
+# 重试机制使用指南
+
+## 核心特性
+
+### 1. 统一的重试工具函数
+
+使用 `withRetry` 函数包装任何需要重试的异步操作:
+
+```typescript
+import { withRetry, RetryPresets, RetryConditions } from '../utils/retry'
+
+// 基本用法
+const result = await withRetry(
+ () => someAsyncOperation(),
+ RetryPresets.standard,
+ logger
+)
+```
+
+### 2. 灵活的配置选项
+
+```typescript
+const customOptions = {
+ maxAttempts: 5, // 最大重试次数
+ baseDelay: 1000, // 基础延迟时间
+ maxDelay: 30000, // 最大延迟时间
+ timeout: 15000, // 操作超时时间
+ delayStrategy: 'exponential', // 延迟策略
+ retryCondition: (error) => !error.message.includes('400')
+}
+```
+
+### 3. 预设配置
+
+#### 重试预设 (RetryPresets)
+
+- `fast`: 快速重试,适合轻量级操作
+- `standard`: 标准重试,适合一般操作
+- `network`: 网络重试,专门针对API调用
+- `fileSystem`: 文件系统重试,处理文件操作
+- `slow`: 慢速重试,适合重量级操作
+
+#### 重试条件 (RetryConditions)
+
+- `networkErrors`: 排除4xx错误的网络重试
+- `fileSystemErrors`: 排除权限错误的文件系统重试
+- `resourceErrors`: 资源竞争错误重试
+- `always`: 始终重试
+- `never`: 从不重试
+
+## 实际应用示例
+
+### 1. EXIF 数据提取
+
+```typescript
+export const extractExifData = async (
+ imageBuffer: Buffer,
+ rawImageBuffer?: Buffer,
+ logger?: Logger[keyof Logger],
+): Promise<NeededExif | null> => {
+ try {
+ return await withRetry(async () => {
+ // 核心处理逻辑
+ const metadata = await sharp(imageBuffer).metadata()
+ // ... 其他处理
+ return result
+ }, {
+ ...RetryPresets.standard,
+ timeout: 15000,
+ retryCondition: (error) => {
+ return RetryConditions.fileSystemErrors(error) ||
+ RetryConditions.resourceErrors(error) ||
+ error.message.includes('timeout')
+ }
+ }, logger)
+ } catch (error) {
+ logger?.error('EXIF extraction failed after all retries:', error)
+ return null
+ }
+}
+```
+
+### 2. 地理编码 API 调用
+
+```typescript
+async reverseGeocode(lat: number, lon: number): Promise<LocationInfo | null> {
+ try {
+ return await withRetry(async () => {
+ await this.applyRateLimit()
+
+ const response = await fetch(url.toString())
+ if (!response.ok) {
+ throw new Error(`API error: ${response.status}`)
+ }
+
+ return processResponse(response)
+ }, {
+ ...RetryPresets.network,
+ timeout: 10000,
+ delayStrategy: 'exponential'
+ }, logger.location)
+ } catch (error) {
+ logger.location.error('Geocoding failed after retries:', error)
+ return null
+ }
+}
+```
+
+### 3. 图像处理操作
+
+```typescript
+export const generateThumbnailAndHash = async (
+ buffer: Buffer,
+ logger?: Logger[keyof Logger],
+) => {
+ return await withRetry(async () => {
+ const thumbnailBuffer = await sharp(buffer)
+ .resize(600, null)
+ .webp({ quality })
+ .toBuffer()
+
+ const thumbnailHash = await generateBlurHash(thumbnailBuffer, logger)
+
+ return { thumbnailBuffer, thumbnailHash }
+ }, {
+ ...RetryPresets.standard,
+ timeout: 15000,
+ delayStrategy: 'linear' // 图像处理适合线性退避
+ }, logger)
+}
+```
+
+## 延迟策略
+
+### 指数退避 (Exponential)
+适用于网络请求,避免对服务器造成冲击:
+- 1s → 2s → 4s → 8s...
+
+### 线性增长 (Linear)
+适用于资源竞争场景,平缓增加等待时间:
+- 1s → 2s → 3s → 4s...
+
+### 固定延迟 (Fixed)
+适用于已知的固定限制场景:
+- 1s → 1s → 1s → 1s...
+
+## 最佳实践
+
+### 1. 选择合适的预设
+
+```typescript
+// ✅ 推荐:根据操作类型选择预设
+await withRetry(fetchAPI, RetryPresets.network, logger)
+await withRetry(fileOperation, RetryPresets.fileSystem, logger)
+await withRetry(imageProcessing, RetryPresets.standard, logger)
+
+// ❌ 避免:盲目使用默认配置
+await withRetry(operation, {}, logger) // 配置不够明确
+```
+
+### 2. 自定义重试条件
+
+```typescript
+// ✅ 推荐:根据具体错误类型判断是否重试
+retryCondition: (error) => {
+ // 不重试客户端错误
+ if (error.message.includes('400') || error.message.includes('401')) {
+ return false
+ }
+ // 重试其他错误
+ return true
+}
+
+// ❌ 避免:盲目重试所有错误
+retryCondition: () => true // 可能导致无意义的重试
+```
+
+### 3. 合理设置超时时间
+
+```typescript
+// ✅ 推荐:根据操作复杂度设置超时
+const options = {
+ timeout: operation === 'thumbnail' ? 15000 : 5000
+}
+
+// ❌ 避免:超时时间过短或过长
+timeout: 1000 // 太短,可能导致正常操作超时
+timeout: 300000 // 太长,用户体验差
+```
+
+### 4. 错误处理和日志记录
+
+```typescript
+// ✅ 推荐:适当的错误处理和日志
+try {
+ return await withRetry(operation, options, logger)
+} catch (error) {
+ logger?.error('Operation failed after all retries:', error)
+ return null // 或抛出更友好的错误
+}
+
+// ❌ 避免:忽略错误或过度错误处理
+await withRetry(operation, options, logger) // 没有错误处理
+```
+
+## 监控和调试
+
+### 日志输出示例
+
+```
+[INFO] Operation attempt 1/3
+[WARN] Attempt 1 failed: Network timeout
+[INFO] Retrying in 1000ms...
+[INFO] Operation attempt 2/3
+[SUCCESS] Operation succeeded on attempt 2
+```
+
+### 性能监控
+
+建议添加以下指标监控:
+- 重试成功率
+- 平均重试次数
+- 重试操作的延迟分布
+- 最终失败率
+
+## 迁移指南
+
+### 从旧的重试逻辑迁移
+
+```typescript
+// 旧的实现 ❌
+const maxRetries = 3
+for (let attempt = 1; attempt <= maxRetries; attempt++) {
+ try {
+ const result = await operation()
+ return result
+ } catch (error) {
+ if (attempt < maxRetries) {
+ await new Promise(resolve => setTimeout(resolve, 1000 * attempt))
+ }
+ }
+}
+
+// 新的实现 ✅
+return await withRetry(
+ () => operation(),
+ RetryPresets.standard,
+ logger
+)
+```
+
+这种统一的重试机制设计不仅提高了代码的可维护性,还通过智能的重试条件和延迟策略,显著提升了系统的稳定性和用户体验。
\ No newline at end of file
diff --git a/server/services/image/exif.ts b/server/services/image/exif.ts
index 7d573b8f..a96f1d03 100644
--- a/server/services/image/exif.ts
+++ b/server/services/image/exif.ts
@@ -6,6 +6,7 @@ import type { NeededExif, PhotoInfo } from '../../../shared/types/photo'
import type { ExifDateTime, Tags } from 'exiftool-vendored'
import { exiftool } from 'exiftool-vendored'
import { noop } from 'es-toolkit'
+import { withRetry, RetryPresets, RetryConditions } from '../../utils/retry'
const neededKeys: Array<keyof Tags | (string & {})> = [
'Title',
@@ -274,84 +275,70 @@ export const extractExifData = async (
rawImageBuffer?: Buffer,
logger?: Logger[keyof Logger],
): Promise<NeededExif | null> => {
- const maxRetries = 3
- let lastError: Error | null = null
-
- for (let attempt = 1; attempt <= maxRetries; attempt++) {
- let tempImagePath: string | null = null
-
- try {
- logger?.info(`Extracting EXIF data (attempt ${attempt}/${maxRetries})...`)
+ try {
+ return await withRetry(async () => {
+ let tempImagePath: string | null = null
- let metadata = await Promise.race([
- sharp(imageBuffer).metadata(),
- new Promise<never>((_, reject) =>
- setTimeout(() => reject(new Error('Metadata extraction timeout')), 5000)
- )
- ])
-
- if (!metadata.exif && rawImageBuffer) {
- try {
- metadata = await Promise.race([
- sharp(rawImageBuffer).metadata(),
- new Promise<never>((_, reject) =>
- setTimeout(() => reject(new Error('Raw metadata extraction timeout')), 5000)
- )
- ])
- } catch (err) {
- logger?.warn('Error extracting EXIF data from raw image buffer:', err)
+ try {
+ // 提取基础元数据
+ let metadata = await sharp(imageBuffer).metadata()
+
+ // 如果主buffer没有EXIF,尝试原始buffer
+ if (!metadata.exif && rawImageBuffer) {
+ try {
+ metadata = await sharp(rawImageBuffer).metadata()
+ } catch (err) {
+ logger?.warn('Error extracting EXIF data from raw image buffer:', err)
+ }
}
- }
- if (!metadata.exif) {
- logger?.warn('No EXIF data found in image metadata')
- return null
- }
-
- logger?.info('Extracting EXIF data using exiftool...')
-
- const tempDir = path.resolve(process.cwd(), 'data/.exif_workdir')
- await mkdir(tempDir, { recursive: true })
- tempImagePath = path.resolve(tempDir, `${crypto.randomUUID()}.jpg`)
-
- await writeFile(tempImagePath, rawImageBuffer || imageBuffer)
-
- const exifData = await Promise.race([
- exiftool.read(tempImagePath),
- new Promise<never>((_, reject) =>
- setTimeout(() => reject(new Error('EXIF tool timeout')), 10000)
- )
- ])
-
- const result = processExifData(exifData, metadata)
+ if (!metadata.exif) {
+ logger?.warn('No EXIF data found in image metadata')
+ return null
+ }
- // 记录颜色空间信息的来源
- if (result.ColorSpace) {
- logger?.success(`Inferred ColorSpace: ${result.ColorSpace}`)
- } else {
- logger?.info('ColorSpace could not be determined')
- }
+ logger?.info('Extracting EXIF data using exiftool...')
+
+ // 创建临时工作目录
+ const tempDir = path.resolve(process.cwd(), 'data/.exif_workdir')
+ await mkdir(tempDir, { recursive: true })
+ tempImagePath = path.resolve(tempDir, `${crypto.randomUUID()}.jpg`)
+
+ // 写入临时文件
+ await writeFile(tempImagePath, rawImageBuffer || imageBuffer)
+
+ // 使用 exiftool 读取详细 EXIF 数据
+ const exifData = await exiftool.read(tempImagePath)
+ const result = processExifData(exifData, metadata)
+
+ // 记录颜色空间信息
+ if (result.ColorSpace) {
+ logger?.success(`Inferred ColorSpace: ${result.ColorSpace}`)
+ } else {
+ logger?.info('ColorSpace could not be determined')
+ }
- // 清理临时文件
- await unlink(tempImagePath).catch(noop)
- return result
- } catch (err) {
- lastError = err as Error
- logger?.warn(`EXIF extraction attempt ${attempt} failed:`, err)
-
- // 清理临时文件
- if (tempImagePath) {
- await unlink(tempImagePath).catch(noop)
+ return result
+ } finally {
+ // 确保清理临时文件
+ if (tempImagePath) {
+ await unlink(tempImagePath).catch(noop)
+ }
}
-
- if (attempt < maxRetries) {
- await new Promise(resolve => setTimeout(resolve, 1000 * attempt))
+ }, {
+ ...RetryPresets.standard,
+ timeout: 15000, // EXIF 处理可能需要更长时间
+ retryCondition: (error) => {
+ // 组合多种重试条件
+ return RetryConditions.fileSystemErrors(error) ||
+ RetryConditions.resourceErrors(error) ||
+ error.message.includes('timeout')
}
- }
+ }, logger)
+ } catch (error) {
+ logger?.error('EXIF extraction failed after all retries:', error)
+ return null
}
-
- logger?.error('All EXIF extraction attempts failed')
- return null
}
export const extractPhotoInfo = (
diff --git a/server/services/image/processor.ts b/server/services/image/processor.ts
index 1fcb54fa..c994d842 100644
--- a/server/services/image/processor.ts
+++ b/server/services/image/processor.ts
@@ -3,6 +3,7 @@ import bmp from '@vingle/bmp-js'
import heicConvert from 'heic-convert'
import { getStorageManager } from '~~/server/plugins/storage'
import sharp from 'sharp'
+import { withRetry, RetryPresets, RetryConditions } from '../../utils/retry'
export interface ProcessedImageData {
sharpInst: sharp.Sharp
@@ -25,19 +26,9 @@ const isBitmap = (buffer: Buffer) => {
const getMetadataWithSharp = async (
sharpInst: sharp.Sharp,
): Promise<ImageMeta | null> => {
- const maxRetries = 3
- let lastError: Error | null = null
-
- for (let attempt = 1; attempt <= maxRetries; attempt++) {
- try {
- logger.image.info(`Extracting metadata (attempt ${attempt}/${maxRetries})...`)
-
- const metadata = await Promise.race([
- sharpInst.metadata(),
- new Promise<never>((_, reject) =>
- setTimeout(() => reject(new Error('Metadata extraction timeout')), 10000)
- )
- ])
+ try {
+ return await withRetry(async () => {
+ const metadata = await sharpInst.metadata()
if (!metadata.height || !metadata.width || !metadata.format) {
logger.image.warn('Incomplete metadata received:', {
@@ -61,59 +52,42 @@ const getMetadataWithSharp = async (
height: height,
format: metadata.format,
}
- } catch (err) {
- lastError = err as Error
- logger.image.warn(`Metadata extraction attempt ${attempt} failed:`, err)
-
- if (attempt < maxRetries) {
- await new Promise(resolve => setTimeout(resolve, 500 * attempt))
- }
- }
+ }, {
+ ...RetryPresets.fast,
+ timeout: 10000,
+ retryCondition: RetryConditions.resourceErrors
+ }, logger.image)
+ } catch (error) {
+ logger.image.error('All metadata extraction attempts failed:', error)
+ return null
}
-
- logger.image.error('All metadata extraction attempts failed')
- return null
}
export const convertHeicToJpeg = async (heicBuffer: Buffer) => {
- const maxRetries = 3
- let lastError: Error | null = null
-
- for (let attempt = 1; attempt <= maxRetries; attempt++) {
- try {
- logger.image.info(`Converting HEIC to JPEG (attempt ${attempt}/${maxRetries})...`)
-
- // 检查文件大小,如果太大则降低质量
- const fileSizeMB = heicBuffer.length / (1024 * 1024)
- const quality = fileSizeMB > 10 ? 0.8 : 0.95
-
- const jpegBuffer = await Promise.race([
- heicConvert({
- // @ts-expect-error idk why there is a type error here
- buffer: heicBuffer,
- format: 'JPEG',
- quality,
- }),
- new Promise((_, reject) =>
- setTimeout(() => reject(new Error('HEIC conversion timeout')), 30000)
- )
- ])
-
- logger.image.info(`Successfully converted HEIC to JPEG (quality: ${quality})`)
- return Buffer.from(jpegBuffer as ArrayBuffer)
- } catch (err) {
- lastError = err as Error
- logger.image.warn(`HEIC conversion attempt ${attempt} failed:`, err)
-
- if (attempt < maxRetries) {
- // 等待一段时间后重试
- await new Promise(resolve => setTimeout(resolve, 1000 * attempt))
- }
+ return await withRetry(async () => {
+ // 检查文件大小,如果太大则降低质量
+ const fileSizeMB = heicBuffer.length / (1024 * 1024)
+ const quality = fileSizeMB > 10 ? 0.8 : 0.95
+
+ const jpegBuffer = await heicConvert({
+ // @ts-expect-error idk why there is a type error here
+ buffer: heicBuffer,
+ format: 'JPEG',
+ quality,
+ })
+
+ logger.image.info(`Successfully converted HEIC to JPEG (quality: ${quality})`)
+ return Buffer.from(jpegBuffer as ArrayBuffer)
+ }, {
+ ...RetryPresets.slow, // HEIC 转换是重量级操作
+ timeout: 30000,
+ retryCondition: (error) => {
+ // HEIC 转换错误通常是资源相关的
+ return RetryConditions.resourceErrors(error) ||
+ error.message.includes('memory') ||
+ error.message.includes('timeout')
}
- }
-
- logger.image.error('All HEIC conversion attempts failed')
- throw lastError || new Error('HEIC conversion failed after all retries')
+ }, logger.image)
}
export const convertBitmapToSharpInst = async (bitmapBuffer: Buffer) => {
diff --git a/server/services/image/thumbnail.ts b/server/services/image/thumbnail.ts
index c36e1208..db835573 100644
--- a/server/services/image/thumbnail.ts
+++ b/server/services/image/thumbnail.ts
@@ -1,52 +1,35 @@
import sharp from 'sharp'
import { generateBlurHash } from './blurhash'
+import { withRetry, RetryPresets } from '../../utils/retry'
export const generateThumbnailAndHash = async (
buffer: Buffer,
logger?: Logger[keyof Logger],
) => {
- const maxRetries = 3
- let lastError: Error | null = null
-
- for (let attempt = 1; attempt <= maxRetries; attempt++) {
- try {
- logger?.info(`Generating thumbnail (attempt ${attempt}/${maxRetries})...`)
-
- const sharpInst = sharp(buffer).rotate()
-
- // 根据文件大小调整缩略图质量
- const fileSizeMB = buffer.length / (1024 * 1024)
- const quality = fileSizeMB > 5 ? 85 : 100
-
- const thumbnailBuffer = await Promise.race([
- sharpInst
- .resize(600, null, {
- withoutEnlargement: true,
- fastShrinkOnLoad: false // 提高质量
- })
- .webp({ quality })
- .toBuffer(),
- new Promise<never>((_, reject) =>
- setTimeout(() => reject(new Error('Thumbnail generation timeout')), 15000)
- )
- ])
-
- logger?.info(`Successfully generated thumbnail (quality: ${quality})`)
-
- // 生成BlurHash
- const thumbnailHash = await generateBlurHash(thumbnailBuffer, logger)
-
- return { thumbnailBuffer, thumbnailHash }
- } catch (err) {
- lastError = err as Error
- logger?.warn(`Thumbnail generation attempt ${attempt} failed:`, err)
-
- if (attempt < maxRetries) {
- await new Promise(resolve => setTimeout(resolve, 500 * attempt))
- }
- }
- }
-
- logger?.error('All thumbnail generation attempts failed')
- throw lastError || new Error('Thumbnail generation failed after all retries')
+ return await withRetry(async () => {
+ const sharpInst = sharp(buffer).rotate()
+
+ // 根据文件大小调整缩略图质量
+ const fileSizeMB = buffer.length / (1024 * 1024)
+ const quality = fileSizeMB > 5 ? 85 : 100
+
+ const thumbnailBuffer = await sharpInst
+ .resize(600, null, {
+ withoutEnlargement: true,
+ fastShrinkOnLoad: false // 提高质量
+ })
+ .webp({ quality })
+ .toBuffer()
+
+ logger?.info(`Successfully generated thumbnail (quality: ${quality})`)
+
+ // 生成BlurHash
+ const thumbnailHash = await generateBlurHash(thumbnailBuffer, logger)
+
+ return { thumbnailBuffer, thumbnailHash }
+ }, {
+ ...RetryPresets.standard,
+ timeout: 15000,
+ delayStrategy: 'linear' // 图像处理适合线性退避
+ }, logger)
}
diff --git a/server/services/location/geocoding.ts b/server/services/location/geocoding.ts
index 6118c89f..f7198039 100644
--- a/server/services/location/geocoding.ts
+++ b/server/services/location/geocoding.ts
@@ -1,3 +1,5 @@
+import { withRetry, RetryPresets } from '../../utils/retry'
+
export interface LocationInfo {
latitude: number
longitude: number
@@ -25,11 +27,8 @@ export class MapboxGeocodingProvider implements GeocodingProvider {
}
async reverseGeocode(lat: number, lon: number): Promise<LocationInfo | null> {
- const maxRetries = 3
- let lastError: Error | null = null
-
- for (let attempt = 1; attempt <= maxRetries; attempt++) {
- try {
+ try {
+ return await withRetry(async () => {
// 应用速率限制
await this.applyRateLimit()
@@ -40,24 +39,12 @@ export class MapboxGeocodingProvider implements GeocodingProvider {
url.searchParams.set('types', 'address,place,district,region,country')
url.searchParams.set('language', 'zh-Hants')
- logger.location.info(`Mapbox API URL (attempt ${attempt}): ${url.toString()}`)
+ logger.location.info(`Mapbox API URL: ${url.toString()}`)
- const response = await Promise.race([
- fetch(url.toString()),
- new Promise<never>((_, reject) =>
- setTimeout(() => reject(new Error('Mapbox API timeout')), 10000)
- )
- ])
+ const response = await fetch(url.toString())
if (!response.ok) {
- logger.location.warn(
- `Mapbox API responded with status ${response.status} (attempt ${attempt})`,
- )
- if (attempt < maxRetries) {
- await new Promise(resolve => setTimeout(resolve, 1000 * attempt))
- continue
- }
- return null
+ throw new Error(`Mapbox API error: ${response.status} ${response.statusText}`)
}
const data = await response.json()
@@ -94,18 +81,15 @@ export class MapboxGeocodingProvider implements GeocodingProvider {
city,
locationName,
}
- } catch (error) {
- lastError = error as Error
- logger.location.warn(`Mapbox reverse geocoding attempt ${attempt} failed:`, error)
-
- if (attempt < maxRetries) {
- await new Promise(resolve => setTimeout(resolve, 2000 * attempt))
- }
- }
+ }, {
+ ...RetryPresets.network,
+ timeout: 10000,
+ delayStrategy: 'exponential'
+ }, logger.location)
+ } catch (error) {
+ logger.location.error('Mapbox reverse geocoding failed after all retries:', error)
+ return null
}
-
- logger.location.error('All Mapbox reverse geocoding attempts failed')
- return null
}
private async applyRateLimit(): Promise<void> {
@@ -133,64 +117,66 @@ export class NominatimGeocodingProvider implements GeocodingProvider {
async reverseGeocode(lat: number, lon: number): Promise<LocationInfo | null> {
try {
- // 应用速率限制
- await this.applyRateLimit()
-
- const url = new URL('/reverse', this.baseUrl)
- url.searchParams.set('lat', lat.toString())
- url.searchParams.set('lon', lon.toString())
- url.searchParams.set('format', 'json')
- url.searchParams.set('addressdetails', '1')
- url.searchParams.set('accept-language', 'zh-CN,zh,en')
-
- const response = await fetch(url.toString(), {
- headers: {
- 'User-Agent': this.userAgent,
- },
- })
-
- if (!response.ok) {
- logger.location.warn(
- `Nominatim API responded with status ${response.status}`,
- )
- return null
- }
+ return await withRetry(async () => {
+ // 应用速率限制
+ await this.applyRateLimit()
- const data = await response.json()
+ const url = new URL('/reverse', this.baseUrl)
+ url.searchParams.set('lat', lat.toString())
+ url.searchParams.set('lon', lon.toString())
+ url.searchParams.set('format', 'json')
+ url.searchParams.set('addressdetails', '1')
+ url.searchParams.set('accept-language', 'zh-CN,zh,en')
- if (!data || data.error) {
- logger.location.warn('Nominatim API returned error:', data?.error)
- return null
- }
+ const response = await fetch(url.toString(), {
+ headers: {
+ 'User-Agent': this.userAgent,
+ },
+ })
- const address = data.address || {}
-
- // 提取国家信息
- const country = address.country || address.country_code?.toUpperCase()
-
- // 提取城市信息(优先级:district > city > town > county > state > village > hamlet)
- // 适配中国行政区划
- const city =
- address.district ||
- address.city ||
- address.town ||
- address.county ||
- address.state ||
- address.village ||
- address.hamlet
-
- // 构建位置名称
- const locationName = data.display_name
-
- return {
- latitude: lat,
- longitude: lon,
- country,
- city,
- locationName,
- }
+ if (!response.ok) {
+ throw new Error(`Nominatim API error: ${response.status} ${response.statusText}`)
+ }
+
+ const data = await response.json()
+
+ if (!data || data.error) {
+ throw new Error(`Nominatim API returned error: ${data?.error}`)
+ }
+
+ const address = data.address || {}
+
+ // 提取国家信息
+ const country = address.country || address.country_code?.toUpperCase()
+
+ // 提取城市信息(优先级:district > city > town > county > state > village > hamlet)
+ // 适配中国行政区划
+ const city =
+ address.district ||
+ address.city ||
+ address.town ||
+ address.county ||
+ address.state ||
+ address.village ||
+ address.hamlet
+
+ // 构建位置名称
+ const locationName = data.display_name
+
+ return {
+ latitude: lat,
+ longitude: lon,
+ country,
+ city,
+ locationName,
+ }
+ }, {
+ ...RetryPresets.network,
+ timeout: 15000, // Nominatim 可能比较慢
+ delayStrategy: 'linear' // 线性退避,避免过快重试
+ }, logger.location)
} catch (error) {
- logger.location.error('Reverse geocoding failed:', error)
+ logger.location.error('Nominatim reverse geocoding failed after all retries:', error)
return null
}
}
diff --git a/server/services/pipeline-queue/manager.ts b/server/services/pipeline-queue/manager.ts
index 4893a812..29b79b32 100644
--- a/server/services/pipeline-queue/manager.ts
+++ b/server/services/pipeline-queue/manager.ts
@@ -193,7 +193,9 @@ export class QueueManager {
const shouldRetry = newAttempts < task.maxAttempts
// 计算重试延迟(指数退避)
- const retryDelay = shouldRetry ? Math.min(1000 * Math.pow(2, newAttempts - 1), 30000) : 0
+ const retryDelay = shouldRetry
+ ? Math.min(1000 * Math.pow(2, newAttempts - 1), 30000)
+ : 0
await db
.update(tables.pipelineQueue)
@@ -202,19 +204,21 @@ export class QueueManager {
attempts: newAttempts,
errorMessage: errorMessage || 'Unknown error',
// 如果重试,设置延迟重试时间
- ...(shouldRetry && retryDelay > 0 ? {
- createdAt: new Date(Date.now() + retryDelay).toISOString()
- } : {})
+ ...(shouldRetry && retryDelay > 0
+ ? {
+ createdAt: new Date(Date.now() + retryDelay),
+ }
+ : {}),
})
.where(eq(tables.pipelineQueue.id, taskId))
if (shouldRetry) {
this.logger.warn(
- `Task ${taskId} failed (attempt ${newAttempts}/${task.maxAttempts}), will retry in ${retryDelay}ms: ${errorMessage}`
+ `Task ${taskId} failed (attempt ${newAttempts}/${task.maxAttempts}), will retry in ${retryDelay}ms: ${errorMessage}`,
)
} else {
this.logger.error(
- `Task ${taskId} failed permanently after ${newAttempts} attempts: ${errorMessage}`
+ `Task ${taskId} failed permanently after ${newAttempts} attempts: ${errorMessage}`,
)
}
}
diff --git a/server/utils/retry.ts b/server/utils/retry.ts
new file mode 100644
index 00000000..123a1191
--- /dev/null
+++ b/server/utils/retry.ts
@@ -0,0 +1,220 @@
+/**
+ * 重试配置选项
+ */
+export interface RetryOptions {
+ /** 最大重试次数,默认 3 */
+ maxAttempts?: number
+ /** 基础延迟时间(毫秒),默认 1000ms */
+ baseDelay?: number
+ /** 最大延迟时间(毫秒),默认 30000ms */
+ maxDelay?: number
+ /** 操作超时时间(毫秒),默认 30000ms */
+ timeout?: number
+ /** 重试条件判断函数,返回 true 表示应该重试 */
+ retryCondition?: (error: Error) => boolean
+ /** 延迟策略:'exponential' 指数退避,'linear' 线性增长,'fixed' 固定延迟 */
+ delayStrategy?: 'exponential' | 'linear' | 'fixed'
+}
+
+/**
+ * 通用重试工具函数
+ *
+ * @param operation 要执行的异步操作
+ * @param options 重试配置选项
+ * @param logger 可选的日志记录器
+ * @returns 操作结果
+ *
+ * @example
+ * ```typescript
+ * const result = await withRetry(
+ * () => fetchDataFromAPI(),
+ * {
+ * maxAttempts: 3,
+ * timeout: 5000,
+ * retryCondition: (error) => !error.message.includes('400')
+ * },
+ * logger.api
+ * )
+ * ```
+ */
+export async function withRetry<T>(
+ operation: () => Promise<T>,
+ options: RetryOptions = {},
+ logger?: Logger[keyof Logger]
+): Promise<T> {
+ const {
+ maxAttempts = 3,
+ baseDelay = 1000,
+ maxDelay = 30000,
+ timeout = 30000,
+ retryCondition = () => true,
+ delayStrategy = 'exponential'
+ } = options
+
+ let lastError: Error
+
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
+ try {
+ logger?.info(`Operation attempt ${attempt}/${maxAttempts}`)
+
+ // 应用超时机制
+ const result = await Promise.race([
+ operation(),
+ new Promise<never>((_, reject) =>
+ setTimeout(() => reject(new Error(`Operation timeout after ${timeout}ms`)), timeout)
+ )
+ ])
+
+ if (attempt > 1) {
+ logger?.success(`Operation succeeded on attempt ${attempt}`)
+ }
+ return result
+ } catch (error) {
+ lastError = error as Error
+ logger?.warn(`Attempt ${attempt} failed:`, error)
+
+ const shouldRetry = attempt < maxAttempts && retryCondition(lastError)
+ if (shouldRetry) {
+ const delay = calculateDelay(attempt, baseDelay, maxDelay, delayStrategy)
+ logger?.info(`Retrying in ${delay}ms...`)
+ await new Promise(resolve => setTimeout(resolve, delay))
+ }
+ }
+ }
+
+ logger?.error(`All ${maxAttempts} attempts failed`)
+ throw lastError!
+}
+
+/**
+ * 计算延迟时间
+ */
+function calculateDelay(
+ attempt: number,
+ baseDelay: number,
+ maxDelay: number,
+ strategy: 'exponential' | 'linear' | 'fixed'
+): number {
+ let delay: number
+
+ switch (strategy) {
+ case 'exponential':
+ // 指数退避:1s, 2s, 4s, 8s...
+ delay = baseDelay * Math.pow(2, attempt - 1)
+ break
+ case 'linear':
+ // 线性增长:1s, 2s, 3s, 4s...
+ delay = baseDelay * attempt
+ break
+ case 'fixed':
+ // 固定延迟:1s, 1s, 1s, 1s...
+ delay = baseDelay
+ break
+ default:
+ delay = baseDelay
+ }
+
+ return Math.min(delay, maxDelay)
+}
+
+/**
+ * 常见错误的重试条件预设
+ */
+export const RetryConditions = {
+ /** 网络错误重试(排除 4xx 客户端错误) */
+ networkErrors: (error: Error) => {
+ const message = error.message.toLowerCase()
+ // 不重试客户端错误 (4xx)
+ if (message.includes('400') || message.includes('401') ||
+ message.includes('403') || message.includes('404')) {
+ return false
+ }
+ // 重试网络相关错误
+ return message.includes('timeout') ||
+ message.includes('network') ||
+ message.includes('fetch') ||
+ message.includes('econnreset') ||
+ message.includes('enotfound') ||
+ message.includes('502') ||
+ message.includes('503') ||
+ message.includes('504')
+ },
+
+ /** 文件系统错误重试(排除权限和不存在错误) */
+ fileSystemErrors: (error: Error) => {
+ const message = error.message.toLowerCase()
+ // 不重试权限和文件不存在错误
+ if (message.includes('eacces') || message.includes('enoent') ||
+ message.includes('permission denied')) {
+ return false
+ }
+ // 重试临时性文件系统错误
+ return message.includes('ebusy') ||
+ message.includes('emfile') ||
+ message.includes('enfile') ||
+ message.includes('eagain')
+ },
+
+ /** 资源竞争错误重试 */
+ resourceErrors: (error: Error) => {
+ const message = error.message.toLowerCase()
+ return message.includes('busy') ||
+ message.includes('locked') ||
+ message.includes('resource') ||
+ message.includes('memory') ||
+ message.includes('cpu')
+ },
+
+ /** 始终重试 */
+ always: () => true,
+
+ /** 从不重试 */
+ never: () => false
+}
+
+/**
+ * 重试配置预设
+ */
+export const RetryPresets = {
+ /** 快速重试:适用于轻量级操作 */
+ fast: {
+ maxAttempts: 3,
+ baseDelay: 500,
+ timeout: 5000,
+ delayStrategy: 'exponential' as const
+ },
+
+ /** 标准重试:适用于一般操作 */
+ standard: {
+ maxAttempts: 3,
+ baseDelay: 1000,
+ timeout: 10000,
+ delayStrategy: 'exponential' as const
+ },
+
+ /** 网络重试:适用于网络请求 */
+ network: {
+ maxAttempts: 3,
+ baseDelay: 1000,
+ timeout: 30000,
+ delayStrategy: 'exponential' as const,
+ retryCondition: RetryConditions.networkErrors
+ },
+
+ /** 文件操作重试:适用于文件系统操作 */
+ fileSystem: {
+ maxAttempts: 5,
+ baseDelay: 500,
+ timeout: 15000,
+ delayStrategy: 'linear' as const,
+ retryCondition: RetryConditions.fileSystemErrors
+ },
+
+ /** 慢速重试:适用于重量级操作 */
+ slow: {
+ maxAttempts: 3,
+ baseDelay: 2000,
+ timeout: 60000,
+ delayStrategy: 'exponential' as const
+ }
+}
\ No newline at end of file