248 lines
6.7 KiB
TypeScript
248 lines
6.7 KiB
TypeScript
/**
|
||
* 临时文件自动清理模块
|
||
*
|
||
* 功能:
|
||
* 1. 应用启动时清理过期的 temp 目录
|
||
* 2. 定时清理(每小时检查一次)
|
||
* 3. 提供手动清理接口
|
||
*
|
||
* 清理规则:
|
||
* - 超过 8 小时的 temp 目录
|
||
* - 且在 Redis 中没有活跃会话
|
||
*/
|
||
|
||
import { promises as fs } from 'fs'
|
||
import path from 'path'
|
||
import {
|
||
getUploadSessionPresence,
|
||
type UploadSessionPresence,
|
||
} from './upload-sessions'
|
||
import {
|
||
getLatestUploadActivityAt,
|
||
shouldCleanupUploadTempDirectory,
|
||
} from './upload-temp-cleanup-policy'
|
||
|
||
const UPLOAD_DIR = process.env.UPLOAD_DIR || './uploads'
|
||
const TEMP_DIR = path.join(UPLOAD_DIR, 'temp')
|
||
const MAX_AGE_HOURS = 8 // 8小时后清理
|
||
const MAX_AGE_MS = MAX_AGE_HOURS * 60 * 60 * 1000
|
||
const CLEANUP_INTERVAL_MS = 60 * 60 * 1000 // 每小时检查一次
|
||
|
||
let cleanupTimer: NodeJS.Timeout | null = null
|
||
let isCleanupRunning = false
|
||
|
||
/**
|
||
* 查询上传会话状态。任何无法确认的异常都返回 unavailable,禁止据此删除目录。
|
||
*/
|
||
async function getSessionPresence(uploadId: string): Promise<UploadSessionPresence> {
|
||
try {
|
||
return await getUploadSessionPresence(uploadId)
|
||
} catch (error) {
|
||
// 防御性保守处理:即便状态查询自身出现意外,也绝不能当成“不存在”。
|
||
console.warn('[TempCleanup] 检查会话失败:', error)
|
||
return 'unavailable'
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 清理单个 temp 目录
|
||
*/
|
||
async function cleanupTempDir(dirPath: string): Promise<{ success: boolean; freedBytes: number }> {
|
||
let freedBytes = 0
|
||
|
||
try {
|
||
// 计算目录大小
|
||
const files = await fs.readdir(dirPath)
|
||
for (const file of files) {
|
||
try {
|
||
const filePath = path.join(dirPath, file)
|
||
const stat = await fs.stat(filePath)
|
||
if (stat.isFile()) {
|
||
freedBytes += stat.size
|
||
}
|
||
} catch {
|
||
// 忽略单个文件错误
|
||
}
|
||
}
|
||
|
||
// 删除目录及内容
|
||
await fs.rm(dirPath, { recursive: true, force: true })
|
||
return { success: true, freedBytes }
|
||
} catch (error) {
|
||
console.warn(`[TempCleanup] 删除目录失败: ${dirPath}`, error)
|
||
return { success: false, freedBytes: 0 }
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 执行清理任务
|
||
*/
|
||
export async function runCleanup(): Promise<{
|
||
cleaned: number
|
||
failed: number
|
||
freedBytes: number
|
||
skippedActive: number
|
||
skippedUnavailable: number
|
||
}> {
|
||
if (isCleanupRunning) {
|
||
console.log('[TempCleanup] 清理任务正在运行,跳过')
|
||
return {
|
||
cleaned: 0,
|
||
failed: 0,
|
||
freedBytes: 0,
|
||
skippedActive: 0,
|
||
skippedUnavailable: 0,
|
||
}
|
||
}
|
||
|
||
isCleanupRunning = true
|
||
const startTime = Date.now()
|
||
|
||
let cleaned = 0
|
||
let failed = 0
|
||
let freedBytes = 0
|
||
let skippedActive = 0
|
||
let skippedUnavailable = 0
|
||
let sessionStoreUnavailable = false
|
||
|
||
try {
|
||
// 检查 temp 目录是否存在
|
||
try {
|
||
await fs.access(TEMP_DIR)
|
||
} catch {
|
||
console.log('[TempCleanup] temp 目录不存在,跳过清理')
|
||
return { cleaned, failed, freedBytes, skippedActive, skippedUnavailable }
|
||
}
|
||
|
||
// 遍历 temp 目录
|
||
const dirs = (await fs.readdir(TEMP_DIR)).sort()
|
||
const now = Date.now()
|
||
|
||
console.log(`[TempCleanup] 检查 ${dirs.length} 个目录...`)
|
||
|
||
for (const dir of dirs) {
|
||
const dirPath = path.join(TEMP_DIR, dir)
|
||
|
||
try {
|
||
const stat = await fs.stat(dirPath)
|
||
if (!stat.isDirectory()) continue
|
||
|
||
let metadata: { createdAt?: unknown; lastActivityAt?: unknown } | null = null
|
||
try {
|
||
metadata = JSON.parse(await fs.readFile(path.join(dirPath, 'meta.json'), 'utf8'))
|
||
} catch {
|
||
// 历史目录可能没有元数据,继续使用目录修改时间。
|
||
}
|
||
|
||
const lastActivityAt = getLatestUploadActivityAt(stat.mtimeMs, metadata)
|
||
const age = now - lastActivityAt
|
||
const isExpired = age >= MAX_AGE_MS
|
||
if (!isExpired) {
|
||
continue // 还没过期,跳过
|
||
}
|
||
|
||
if (sessionStoreUnavailable) {
|
||
skippedUnavailable++
|
||
continue
|
||
}
|
||
|
||
// 只允许删除“已过期且存储明确确认不存在”的目录。
|
||
const presence = await getSessionPresence(dir)
|
||
if (presence === 'present') {
|
||
skippedActive++
|
||
continue
|
||
}
|
||
if (presence === 'unavailable') {
|
||
// Redis 通常是全局故障;本轮剩余目录不再逐个等待命令超时。
|
||
sessionStoreUnavailable = true
|
||
skippedUnavailable++
|
||
continue
|
||
}
|
||
if (!shouldCleanupUploadTempDirectory(isExpired, presence)) {
|
||
continue
|
||
}
|
||
|
||
// 执行清理
|
||
const result = await cleanupTempDir(dirPath)
|
||
if (result.success) {
|
||
cleaned++
|
||
freedBytes += result.freedBytes
|
||
} else {
|
||
failed++
|
||
}
|
||
} catch (error) {
|
||
failed++
|
||
console.warn(`[TempCleanup] 处理目录失败: ${dir}`, error)
|
||
}
|
||
}
|
||
|
||
const duration = Date.now() - startTime
|
||
const freedMB = (freedBytes / 1024 / 1024).toFixed(2)
|
||
console.log(`[TempCleanup] 清理完成: 删除 ${cleaned} 个目录,释放 ${freedMB} MB,跳过活跃 ${skippedActive} 个,状态不明 ${skippedUnavailable} 个,耗时 ${duration}ms`)
|
||
|
||
} finally {
|
||
isCleanupRunning = false
|
||
}
|
||
|
||
return { cleaned, failed, freedBytes, skippedActive, skippedUnavailable }
|
||
}
|
||
|
||
/**
|
||
* 启动定时清理任务
|
||
*/
|
||
export function startCleanupScheduler(): void {
|
||
if (cleanupTimer) {
|
||
console.log('[TempCleanup] 定时任务已在运行')
|
||
return
|
||
}
|
||
|
||
console.log('[TempCleanup] 启动定时清理任务(每小时执行)')
|
||
|
||
// 立即执行一次
|
||
runCleanup().catch(err => {
|
||
console.error('[TempCleanup] 启动时清理失败:', err)
|
||
})
|
||
|
||
// 设置定时任务
|
||
cleanupTimer = setInterval(() => {
|
||
runCleanup().catch(err => {
|
||
console.error('[TempCleanup] 定时清理失败:', err)
|
||
})
|
||
}, CLEANUP_INTERVAL_MS)
|
||
|
||
// 确保定时器不阻止进程退出
|
||
cleanupTimer.unref()
|
||
}
|
||
|
||
/**
|
||
* 停止定时清理任务
|
||
*/
|
||
export function stopCleanupScheduler(): void {
|
||
if (cleanupTimer) {
|
||
clearInterval(cleanupTimer)
|
||
cleanupTimer = null
|
||
console.log('[TempCleanup] 定时清理任务已停止')
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 清理指定的上传会话临时目录
|
||
* 用于上传完成或取消时立即清理
|
||
*/
|
||
export async function cleanupUploadSession(uploadId: string): Promise<boolean> {
|
||
const dirPath = path.join(TEMP_DIR, uploadId)
|
||
|
||
try {
|
||
await fs.access(dirPath)
|
||
} catch {
|
||
// 目录不存在,视为已清理
|
||
return true
|
||
}
|
||
|
||
const result = await cleanupTempDir(dirPath)
|
||
if (result.success) {
|
||
console.log(`[TempCleanup] 已清理上传会话: ${uploadId}`)
|
||
}
|
||
return result.success
|
||
}
|