mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +00:00
fix(transcode): preserve trashed status during background jobs and handle permanent purge (#416)
* fix(transcode): preserve trashed status during background jobs and handle permanent purge * fix(workflow): clear heartbeat and add info logs on task cancellation * fix(transcode): terminate media processes and guard task activities on cancellation - Pass AbortSignal to ffmpeg and pdftoppm child processes in transcodeService to terminate processes on task cancellation - Add local task AbortController context and cancellation guards in executeActivity to prevent subsequent workflow activities - Forward taskId and cancellation signals in transcode, screenshot, and PDF activities - Use updateMany instead of update in task activities to eliminate Prisma P2025 errors on purged task records * fix(workflow): allow cleanup activities in finally blocks during task cancellation Remove blanket cancellation check in executeActivity so that workflows can execute cleanup and failure handling activities in catch and finally blocks upon cancellation, while relying on AbortSignal in active activities to abort execution. * docs(changelog): improve user friendliness of changelog entries and formatting example * docs(agents): update changelog guidelines to emphasize end-user friendly descriptions * fix(transcode): handle queued cancellation and delete races in media workflow
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import { cancelWorkflowTask, prisma } from '@shumai/db'
|
||||
import {
|
||||
AncestorFolder,
|
||||
AssetInfo,
|
||||
@@ -28,7 +28,15 @@ import {
|
||||
type ExportOptions,
|
||||
type ExportResult,
|
||||
} from './comments-export'
|
||||
import { type Asset, AssetStatus, AssetType, Prisma, type StorageKey } from '@shumai/db'
|
||||
import {
|
||||
type Asset,
|
||||
AssetStatus,
|
||||
AssetType,
|
||||
Prisma,
|
||||
type StorageKey,
|
||||
WorkflowTaskStatus,
|
||||
WorkflowTaskType,
|
||||
} from '@shumai/db'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { PaginatedData, paginateQuery, PaginationParams } from '@shumai/core/src/pagination'
|
||||
@@ -1343,7 +1351,7 @@ export class AssetService {
|
||||
private async cascadeStatusToPendingPurge(rootIds: string[]): Promise<void> {
|
||||
if (rootIds.length === 0) return
|
||||
|
||||
await this.prismaClient.$executeRaw`
|
||||
const rows = await this.prismaClient.$queryRaw<{ id: string }[]>`
|
||||
WITH RECURSIVE descendant AS (
|
||||
SELECT id FROM assets WHERE id = ANY(${rootIds})
|
||||
UNION ALL
|
||||
@@ -1358,15 +1366,35 @@ export class AssetService {
|
||||
WHERE s.type = 'symlink' AND s.target_id IN (SELECT id FROM all_purging_targets)
|
||||
)
|
||||
UPDATE assets SET status = 'pending_purge', updated_at = NOW()
|
||||
WHERE id IN (SELECT id FROM all_purging_targets) OR id IN (SELECT id FROM all_affected_symlinks);
|
||||
WHERE id IN (SELECT id FROM all_purging_targets) OR id IN (SELECT id FROM all_affected_symlinks)
|
||||
RETURNING id;
|
||||
`
|
||||
|
||||
const affectedIds = rows.map((r) => r.id)
|
||||
if (affectedIds.length > 0) {
|
||||
const inFlightTasks = await this.prismaClient.workflowTask.findMany({
|
||||
where: {
|
||||
assetId: { in: affectedIds },
|
||||
status: { in: [WorkflowTaskStatus.pending, WorkflowTaskStatus.processing] },
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
for (const task of inFlightTasks) {
|
||||
try {
|
||||
logger.info({ taskId: task.id }, 'Cancelling in-flight workflow task for purging asset')
|
||||
await cancelWorkflowTask(task.id)
|
||||
} catch (err) {
|
||||
logger.warn({ taskId: task.id, err }, 'Failed to cancel workflow task during purge')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async emptyTrash(projectId: string): Promise<void> {
|
||||
const trashedRoots = await this.prismaClient.asset.findMany({
|
||||
where: {
|
||||
projectId,
|
||||
status: 'trashed',
|
||||
isDeleted: true,
|
||||
},
|
||||
select: { id: true },
|
||||
@@ -1418,9 +1446,9 @@ export class AssetService {
|
||||
// Find up to 100 expired roots, ordered by oldest first to avoid starvation
|
||||
const expiredRoots = await this.prismaClient.asset.findMany({
|
||||
where: {
|
||||
status: 'trashed',
|
||||
deletedAt: { lt: thirtyDaysAgo },
|
||||
isDeleted: true,
|
||||
status: { not: AssetStatus.pending_purge },
|
||||
deletedAt: { lt: thirtyDaysAgo },
|
||||
},
|
||||
orderBy: { deletedAt: 'asc' },
|
||||
select: { id: true },
|
||||
@@ -1435,6 +1463,35 @@ export class AssetService {
|
||||
* Stage 2: Delete DB records for assets marked as 'pending_purge'.
|
||||
*/
|
||||
private async purgePendingAssets() {
|
||||
const pendingAssets = await this.prismaClient.asset.findMany({
|
||||
where: { status: AssetStatus.pending_purge },
|
||||
select: { id: true },
|
||||
})
|
||||
const pendingAssetIds = pendingAssets.map((a) => a.id)
|
||||
|
||||
if (pendingAssetIds.length > 0) {
|
||||
const inFlightTasks = await this.prismaClient.workflowTask.findMany({
|
||||
where: {
|
||||
assetId: { in: pendingAssetIds },
|
||||
status: { in: [WorkflowTaskStatus.pending, WorkflowTaskStatus.processing] },
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
for (const task of inFlightTasks) {
|
||||
try {
|
||||
logger.info({ taskId: task.id }, 'Cancelling in-flight workflow task before purge')
|
||||
await cancelWorkflowTask(task.id)
|
||||
} catch (err) {
|
||||
logger.warn({ taskId: task.id, err }, 'Failed to cancel workflow task during purge')
|
||||
}
|
||||
}
|
||||
|
||||
await this.prismaClient.workflowTask.deleteMany({
|
||||
where: { assetId: { in: pendingAssetIds } },
|
||||
})
|
||||
}
|
||||
|
||||
const { count } = await this.prismaClient.asset.deleteMany({
|
||||
where: { status: AssetStatus.pending_purge },
|
||||
})
|
||||
@@ -1577,11 +1634,30 @@ export class AssetService {
|
||||
}
|
||||
}
|
||||
|
||||
const activeTask = await tx.workflowTask.findFirst({
|
||||
where: {
|
||||
assetId: a.id,
|
||||
status: { in: [WorkflowTaskStatus.pending, WorkflowTaskStatus.processing] },
|
||||
type: {
|
||||
in: [
|
||||
WorkflowTaskType.transcode,
|
||||
WorkflowTaskType.transcode_video,
|
||||
WorkflowTaskType.transcode_image,
|
||||
WorkflowTaskType.transcode_pdf,
|
||||
WorkflowTaskType.transcode_pdf_pages,
|
||||
WorkflowTaskType.transcode_screenshot,
|
||||
WorkflowTaskType.transcode_image_annotation,
|
||||
WorkflowTaskType.transcode_watermark,
|
||||
],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await tx.asset.update({
|
||||
where: { id: a.id },
|
||||
data: {
|
||||
isDeleted: false,
|
||||
status: 'processed',
|
||||
status: activeTask ? AssetStatus.processing : AssetStatus.processed,
|
||||
deletedAt: null,
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user