mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-06 16:24:17 +00:00
refactor(project): address cleanup job robustness feedback from pr review
This commit is contained in:
+20
-12
@@ -757,27 +757,29 @@ export class AssetService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
startCleanupJob() {
|
startCleanupJob() {
|
||||||
// Run cleanup every 1 second
|
// Run cleanup every 1 second using recursive setTimeout to prevent overlapping
|
||||||
setInterval(async () => {
|
const run = async () => {
|
||||||
try {
|
try {
|
||||||
await this.cleanupOldAssets()
|
await this.cleanupOldAssets()
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
console.error('Error in asset cleanup job:', e)
|
console.error('Error in asset cleanup job:', e)
|
||||||
}
|
}
|
||||||
}, 1000)
|
setTimeout(run, 1000)
|
||||||
|
}
|
||||||
|
run()
|
||||||
}
|
}
|
||||||
|
|
||||||
private async cleanupOldAssets() {
|
private async cleanupOldAssets() {
|
||||||
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000)
|
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000)
|
||||||
|
|
||||||
// 1. Lock 100 assets using raw SQL FOR UPDATE SKIP LOCKED and mark them as 'removed'
|
// 1. Lock 100 assets using raw SQL FOR UPDATE SKIP LOCKED and mark them as 'removed'
|
||||||
// This immediately releases the DB lock as soon as the status is updated.
|
// We include a timeout check (1 hour) to allow picking up stalled tasks if a server crashed.
|
||||||
const lockedAssets = await this.prismaClient.$queryRaw<{ id: string; key: string | null }[]>`
|
const lockedAssets = await this.prismaClient.$queryRaw<{ id: string; key: string | null }[]>`
|
||||||
WITH to_delete AS (
|
WITH to_delete AS (
|
||||||
SELECT id FROM assets
|
SELECT id FROM assets
|
||||||
WHERE removed = true
|
WHERE removed = true
|
||||||
AND deleted_at < ${thirtyDaysAgo}
|
AND deleted_at < ${thirtyDaysAgo}
|
||||||
AND status != 'removed'
|
AND (status != 'removed' OR updated_at < NOW() - INTERVAL '1 hour')
|
||||||
LIMIT 100
|
LIMIT 100
|
||||||
FOR UPDATE SKIP LOCKED
|
FOR UPDATE SKIP LOCKED
|
||||||
)
|
)
|
||||||
@@ -790,24 +792,30 @@ export class AssetService {
|
|||||||
|
|
||||||
// 2. Physical file deletion from storage
|
// 2. Physical file deletion from storage
|
||||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||||
await Promise.allSettled(
|
const results = await Promise.all(
|
||||||
lockedAssets.map(async (a) => {
|
lockedAssets.map(async (a) => {
|
||||||
if (a.key) {
|
if (a.key) {
|
||||||
try {
|
try {
|
||||||
await s3Service.deleteObject(bucket, a.key)
|
await s3Service.deleteObject(bucket, a.key)
|
||||||
|
return a.id
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
console.error(`Failed to delete storage object ${a.key}:`, e)
|
console.error(`Failed to delete storage object ${a.key}:`, e)
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Folders or assets without keys are considered processed
|
||||||
|
return a.id
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
// 3. Hard delete from database
|
// 3. Hard delete from database ONLY for assets successfully removed from storage
|
||||||
// Cascade FKs will handle related comments, metadata, etc.
|
// This prevents orphaning files if S3 deletion fails.
|
||||||
const ids = lockedAssets.map((a) => a.id)
|
const successfulIds = results.filter((id): id is string => id !== null)
|
||||||
await this.prismaClient.asset.deleteMany({
|
if (successfulIds.length > 0) {
|
||||||
where: { id: { in: ids } },
|
await this.prismaClient.asset.deleteMany({
|
||||||
})
|
where: { id: { in: successfulIds } },
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async restoreAssets(ids: string[]): Promise<void> {
|
async restoreAssets(ids: string[]): Promise<void> {
|
||||||
|
|||||||
@@ -230,7 +230,7 @@ export class ProjectService {
|
|||||||
})
|
})
|
||||||
if (!project) throw new Error('Project not found')
|
if (!project) throw new Error('Project not found')
|
||||||
|
|
||||||
const sixtyDaysAgo = new Date(Date.now() - 60 * 24 * 60 * 60 * 1000)
|
const immediateCleanupDate = new Date(0)
|
||||||
|
|
||||||
await prisma.$transaction(async (tx) => {
|
await prisma.$transaction(async (tx) => {
|
||||||
// 1. Unlink root folders from project to avoid cycle during deletion
|
// 1. Unlink root folders from project to avoid cycle during deletion
|
||||||
@@ -248,7 +248,7 @@ export class ProjectService {
|
|||||||
parentId: null,
|
parentId: null,
|
||||||
targetId: null,
|
targetId: null,
|
||||||
removed: true,
|
removed: true,
|
||||||
deletedAt: sixtyDaysAgo,
|
deletedAt: immediateCleanupDate,
|
||||||
status: 'trashed',
|
status: 'trashed',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user