64 lines
2.5 KiB
TypeScript
64 lines
2.5 KiB
TypeScript
import { Prisma } from '@prisma/client'
|
|
import { findPurgeFileReferences, purgeFileIdentity, removePurgeFile, type PurgeFile } from '@/lib/material-purge-files'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { deleteStoredUploadFileFromRemote } from '@/lib/remote-upload-sync'
|
|
import { buildStoredUploadPath } from '@/lib/upload-paths'
|
|
|
|
type ClaimedCleanup = { id: string; files: Prisma.JsonValue; attempts: number }
|
|
|
|
const SHARED_FILE_RECHECK_MS = 60 * 60_000
|
|
const MAX_FAILURE_RETRY_MS = 10 * 60_000
|
|
|
|
export async function processNextTaskDetailZipCleanup(id?: string): Promise<boolean> {
|
|
// 领取和锁一起提交,崩溃后等待租期届满即可重试。
|
|
const [job] = await prisma.$queryRaw<ClaimedCleanup[]>`
|
|
WITH due AS (
|
|
SELECT id FROM task_detail_zip_cleanups
|
|
WHERE next_attempt_at <= CURRENT_TIMESTAMP
|
|
${id ? Prisma.sql`AND id = ${id}` : Prisma.empty}
|
|
ORDER BY next_attempt_at, id LIMIT 1 FOR UPDATE SKIP LOCKED
|
|
)
|
|
UPDATE task_detail_zip_cleanups AS cleanup
|
|
SET attempts = cleanup.attempts + 1, next_attempt_at = CURRENT_TIMESTAMP + interval '10 minutes'
|
|
FROM due WHERE cleanup.id = due.id
|
|
RETURNING cleanup.id, cleanup.files, cleanup.attempts
|
|
`
|
|
if (!job) return false
|
|
|
|
const files = job.files as unknown as PurgeFile[]
|
|
let pending = files
|
|
let retryMs = SHARED_FILE_RECHECK_MS
|
|
const retryOnFailure = (error: unknown) => {
|
|
retryMs = Math.min(job.attempts * 30_000, MAX_FAILURE_RETRY_MS)
|
|
console.warn('[TaskDetailZipCleanup] 旧包清理将重试:', job.id, error)
|
|
}
|
|
try {
|
|
const references = await findPurgeFileReferences(prisma, files)
|
|
const results = await Promise.allSettled(files.map(async (file) => {
|
|
if (references.has(purgeFileIdentity(file))) return false
|
|
await removePurgeFile(file)
|
|
await deleteStoredUploadFileFromRemote(buildStoredUploadPath(file.key))
|
|
return true
|
|
}))
|
|
pending = files.filter((_, index) => {
|
|
const result = results[index]
|
|
if (result.status === 'rejected') retryOnFailure(result.reason)
|
|
return result.status === 'rejected' || !result.value
|
|
})
|
|
} catch (error) {
|
|
retryOnFailure(error)
|
|
}
|
|
|
|
// 已被其他进程重新领取的任务由新的领取者写回。
|
|
const where = { id: job.id, attempts: job.attempts }
|
|
if (pending.length) {
|
|
await prisma.taskDetailZipCleanup.updateMany({
|
|
where,
|
|
data: { files: pending as unknown as Prisma.InputJsonValue, nextAttemptAt: new Date(Date.now() + retryMs) },
|
|
})
|
|
} else {
|
|
await prisma.taskDetailZipCleanup.deleteMany({ where })
|
|
}
|
|
return true
|
|
}
|