feat(trash): implement two-stage trashcan and project cleanup

This commit is contained in:
yiling
2026-05-23 23:26:43 +08:00
parent f05247f372
commit effec44870
15 changed files with 272 additions and 171 deletions
+4 -4
View File
@@ -412,12 +412,12 @@ describe('AssetService', () => {
const folderA = await prisma.asset.findUnique({
where: { id: assets.folderA.id },
})
expect(folderA?.removed).toBe(true)
expect(folderA?.isDeleted).toBe(true)
const fileA1 = await prisma.asset.findUnique({
where: { id: assets.fileA1.id },
})
expect(fileA1?.removed).toBe(true)
expect(fileA1?.isDeleted).toBe(true)
// Verify root size decreased by folderA's size
await verifyAsset(assets.root.id, {
@@ -432,14 +432,14 @@ describe('AssetService', () => {
const folderRestoredA = await prisma.asset.findUnique({
where: { id: assets.folderA.id },
})
expect(folderRestoredA?.removed).toBe(false)
expect(folderRestoredA?.isDeleted).toBe(false)
expect(folderRestoredA?.deletedAt).toBeNull()
const fileA1Restored = await prisma.asset.findUnique({
where: { id: assets.fileA1.id },
})
expect(fileA1Restored?.removed).toBe(false)
expect(fileA1Restored?.isDeleted).toBe(false)
// Verify root restored properly
await verifyAsset(assets.root.id, {
+83 -29
View File
@@ -359,16 +359,16 @@ export class AssetService {
let where: Prisma.AssetWhereInput = {}
if (req.showRemoved) {
if (req.showDeleted) {
if (!req.projectId) {
throw new Error('ProjectID is required when ShowRemoved is true')
throw new Error('ProjectID is required when ShowDeleted is true')
}
const thirtyDaysAgo = new Date()
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30)
where = {
projectId: req.projectId,
removed: true,
isDeleted: true,
deletedAt: { gte: thirtyDaysAgo },
OR: [
{ type: { in: typesToQuery } },
@@ -377,7 +377,7 @@ export class AssetService {
}
} else {
if (!req.assetId) {
throw new Error('AssetId is required when ShowRemoved is false')
throw new Error('AssetId is required when ShowDeleted is false')
}
let parentId = req.assetId
const asset = await this.prismaClient.asset.findUnique({
@@ -389,7 +389,7 @@ export class AssetService {
where = {
parentId,
removed: false,
isDeleted: false,
OR: [
{ type: { in: typesToQuery } },
{ type: AssetType.symlink, target: { type: { in: typesToQuery } } },
@@ -637,7 +637,7 @@ export class AssetService {
if (currentType === AssetType.version_stack) {
const latestChild = await this.prismaClient.asset.findFirst({
where: { parentId: currentId, removed: false },
where: { parentId: currentId, isDeleted: false },
orderBy: { sortIndex: 'desc' },
select: { id: true },
})
@@ -742,13 +742,17 @@ export class AssetService {
if (descendantIds.length > 0) {
await tx.asset.updateMany({
where: { id: { in: descendantIds } },
data: { removed: true },
data: { isDeleted: true },
})
}
await tx.asset.update({
where: { id: a.id },
data: { removed: true, deletedAt: new Date() },
data: {
isDeleted: true,
status: 'trashed',
deletedAt: new Date(),
},
})
await this.updateAncestorsSize(tx, a.parentId, -a.sizeByte)
@@ -757,34 +761,81 @@ export class AssetService {
}
startCleanupJob() {
// Run cleanup every 1 second using recursive setTimeout to prevent overlapping
const run = async () => {
// Stage 1: Expiration Job - Move expired trashed roots to pending_purge
const runExpiration = async () => {
try {
await this.cleanupOldAssets()
await this.expireTrashedAssets()
} catch (e: unknown) {
console.error('Error in asset cleanup job:', e)
console.error('Error in asset expiration job:', e)
}
setTimeout(run, 1000)
setTimeout(runExpiration, 5000) // Run every 5 seconds, less frequent
}
run()
// Stage 2: Physical Purge Job - Delete files from S3 and wipe from DB
const runPurge = async () => {
try {
await this.purgePendingAssets()
} catch (e: unknown) {
console.error('Error in asset purge job:', e)
}
setTimeout(runPurge, 1000) // Run every 1 second
}
runExpiration()
runPurge()
}
private async cleanupOldAssets() {
/**
* Stage 1: Find root assets that have been in the trash for > 30 days.
* Cascade the 'pending_purge' status to all their descendants.
*/
private async expireTrashedAssets() {
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'
// We include a timeout check (1 hour) to allow picking up stalled tasks if a server crashed.
// Find up to 100 expired roots
const expiredRoots = await this.prismaClient.asset.findMany({
where: {
status: 'trashed',
deletedAt: { lt: thirtyDaysAgo },
isDeleted: true,
},
select: { id: true },
take: 100,
})
for (const root of expiredRoots) {
await this.prismaClient.$transaction(async (tx) => {
// Use recursive CTE to find all descendants and mark them all as pending_purge
await tx.$executeRaw`
WITH RECURSIVE descendant AS (
SELECT id FROM assets WHERE id = ${root.id}
UNION ALL
SELECT a.id FROM assets a
INNER JOIN descendant d ON a.parent_id = d.id
)
UPDATE assets SET status = 'pending_purge', updated_at = NOW()
WHERE id IN (SELECT id FROM descendant);
`
})
}
}
/**
* Stage 2: Physically delete files and DB records for assets marked as 'pending_purge'.
*/
private async purgePendingAssets() {
// 1. Lock 100 assets using raw SQL FOR UPDATE SKIP LOCKED and mark them as 'purging'
// We include a timeout check (1 hour) to allow picking up stalled tasks.
const lockedAssets = await this.prismaClient.$queryRaw<{ id: string; key: string | null }[]>`
WITH to_delete AS (
WITH to_purge AS (
SELECT id FROM assets
WHERE removed = true
AND deleted_at < ${thirtyDaysAgo}
AND (status != 'removed' OR updated_at < NOW() - INTERVAL '1 hour')
WHERE status = 'pending_purge'
OR (status = 'purging' AND updated_at < NOW() - INTERVAL '1 hour')
LIMIT 100
FOR UPDATE SKIP LOCKED
)
UPDATE assets SET status = 'removed', updated_at = NOW()
WHERE id IN (SELECT id FROM to_delete)
UPDATE assets SET status = 'purging', updated_at = NOW()
WHERE id IN (SELECT id FROM to_purge)
RETURNING id, key;
`
@@ -803,13 +854,12 @@ export class AssetService {
return null
}
}
// Folders or assets without keys are considered processed
// Assets without keys (folders, etc.) are considered processed
return a.id
}),
)
// 3. Hard delete from database ONLY for assets successfully removed from storage
// This prevents orphaning files if S3 deletion fails.
const successfulIds = results.filter((id): id is string => id !== null)
if (successfulIds.length > 0) {
await this.prismaClient.asset.deleteMany({
@@ -847,13 +897,17 @@ export class AssetService {
if (descendantIds.length > 0) {
await tx.asset.updateMany({
where: { id: { in: descendantIds } },
data: { removed: false },
data: { isDeleted: false },
})
}
await tx.asset.update({
where: { id: a.id },
data: { removed: false, deletedAt: null },
data: {
isDeleted: false,
status: 'processed',
deletedAt: null,
},
})
await this.updateAncestorsSize(tx, a.parentId, a.sizeByte)
@@ -1065,7 +1119,7 @@ export class AssetService {
const versionsMap = new Map<string, AssetWithIncludes['children'][0][]>()
if (stackIds.size > 0) {
const allVersions = await this.prismaClient.asset.findMany({
where: { parentId: { in: Array.from(stackIds) }, removed: false },
where: { parentId: { in: Array.from(stackIds) }, isDeleted: false },
include: { creator: true, metadataValues: true },
})
for (const v of allVersions) {
@@ -1222,7 +1276,7 @@ export class AssetService {
}>
> {
const versions = await this.prismaClient.asset.findMany({
where: { parentId: stackId, removed: false },
where: { parentId: stackId, isDeleted: false },
include: { creator: true },
})
+5 -3
View File
@@ -261,7 +261,7 @@ describe('ProjectService', () => {
// Verify asset is soft-deleted and detached
const a = await prisma.asset.findUnique({ where: { id: asset.id } })
expect(a).toBeDefined()
expect(a?.removed).toBe(true)
expect(a?.isDeleted).toBe(true)
expect(a?.projectId).toBeNull()
expect(a?.parentId).toBeNull()
@@ -333,9 +333,11 @@ describe('ProjectService', () => {
// Delete project
await projectService.deleteProject(project.id)
// Manually trigger the private cleanup method for testing
// Manually trigger the private cleanup methods for testing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await (assetService as any).cleanupOldAssets()
await (assetService as any).expireTrashedAssets()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await (assetService as any).purgePendingAssets()
// Verify everything is wiped from DB
expect(await prisma.project.findUnique({ where: { id: project.id } })).toBeNull()
+27 -17
View File
@@ -233,28 +233,38 @@ export class ProjectService {
const immediateCleanupDate = new Date(0)
await prisma.$transaction(async (tx) => {
// 1. Unlink root folders from project to avoid cycle during deletion
// 1. Mark ALL project assets as isDeleted and detach projectId
// We keep parentId intact so the folder structure remains for Stage 1 cleanup.
await tx.asset.updateMany({
where: { projectId },
data: {
isDeleted: true,
projectId: null,
},
})
// 2. Mark root folders as 'trashed' with deletedAt = 0 to trigger immediate background purge
const rootIds = [project.rootFolderId, project.shareRootId].filter(
(id): id is string => id !== null,
)
if (rootIds.length > 0) {
await tx.asset.updateMany({
where: { id: { in: rootIds } },
data: {
status: 'trashed',
deletedAt: immediateCleanupDate,
},
})
}
// 3. Unlink root folders from project record to avoid FK cycles during project deletion
await tx.project.update({
where: { id: projectId },
data: { rootFolderId: null, shareRootId: null },
})
// 2. Detach ALL assets (files and folders) from project and soft-delete them
// This queues them for the background cleanup job.
await tx.asset.updateMany({
where: { projectId },
data: {
projectId: null,
parentId: null,
targetId: null,
removed: true,
deletedAt: immediateCleanupDate,
status: 'trashed',
},
})
// 3. Delete the project itself
// Thanks to cascade deletes, project members, invites, etc. will be cleaned up.
// 4. Delete the project itself
// Cascade deletes will handle project members, invites, notifications, etc.
await tx.project.delete({
where: { id: projectId },
})
+1 -1
View File
@@ -22,7 +22,7 @@ export class SearchService {
}
const where: Prisma.AssetWhereInput = {
removed: false,
isDeleted: false,
}
if (targetFolderIds.length > 0) {