fix(transcode): ensure durable file exchange and crash recovery across workflow steps (#484)

* fix(transcode): ensure durable file exchange and crash recovery across workflow steps

* fix(transcode): recognize AWS SDK NoSuchKey by name and add workflow E2E recovery test
This commit is contained in:
Yiling-J
2026-10-20 11:29:22 +08:00
committed by GitHub
parent 176b7f0eab
commit 1fcd7245ea
12 changed files with 377 additions and 31 deletions
@@ -25,6 +25,7 @@ vi.mock('@shumai/core/src/turntable/turntable', () => ({
vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
putObject: vi.fn(),
downloadToFile: vi.fn(),
},
}))
@@ -120,6 +121,46 @@ describe('3D Transcode Activities', () => {
expect(res.modelMetadata).toEqual({ format: 'glb', vertices: 120 })
})
it('recovers 3D model from S3 assetKey when local file is missing', async () => {
vi.mocked(turntableService.uploadFile).mockResolvedValue({
id: 'f_input',
name: 'model.glb',
size: 1024,
})
vi.mocked(turntableService.startRender).mockResolvedValue({
taskId: 'task_render_1',
status: 'queued',
poster: {
fileId: 'f_poster',
contentType: 'image/png',
width: 1080,
height: 1080,
size: 5000,
},
metadata: { format: 'glb', vertices: 120 },
})
vi.mocked(turntableService.downloadFile).mockResolvedValue(Buffer.from('poster-raw-bytes'))
const targetPath = `/tmp/test-3d-model-${Date.now()}.glb`
await start3dRenderActivity({
taskId: 'wf-task-1',
teamId: 'team-1',
assetId: 'asset-1',
assetKey: 'files/asset-1/model.glb',
filePath: targetPath,
filename: 'model.glb',
targetPosterKey: 'files/asset-1/poster.webp',
})
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
'shumai',
'files/asset-1/model.glb',
targetPath,
)
})
it('throws ApplicationFailure when poster file ID is missing', async () => {
vi.mocked(turntableService.uploadFile).mockResolvedValue({
id: 'f_input',
@@ -326,6 +367,24 @@ describe('3D Transcode Activities', () => {
)
expect(res.spriteKey).toBe('files/asset-1/sprite.webp')
})
it('recovers turntable video from S3 videoKey when local file is missing', async () => {
const targetPath = `/tmp/test-turntable-${Date.now()}.mp4`
const res = await generate3dSpriteActivity({
taskId: 'wf-task-3',
assetKey: 'files/asset-1/model.glb',
videoKey: 'files/asset-1/turntable.mp4',
videoFilePath: targetPath,
spriteKey: 'files/asset-1/sprite.webp',
})
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
'shumai',
'files/asset-1/turntable.mp4',
targetPath,
)
expect(res.spriteKey).toBe('files/asset-1/sprite.webp')
})
})
describe('generate3dViewsActivity', () => {
@@ -351,5 +410,23 @@ describe('3D Transcode Activities', () => {
)
expect(res.viewsKey).toBe('files/asset-1/views.webp')
})
it('recovers turntable video from S3 videoKey when local file is missing', async () => {
const targetPath = `/tmp/test-turntable-views-${Date.now()}.mp4`
const res = await generate3dViewsActivity({
taskId: 'wf-task-4',
assetKey: 'files/asset-1/model.glb',
videoKey: 'files/asset-1/turntable.mp4',
videoFilePath: targetPath,
viewsKey: 'files/asset-1/views.webp',
})
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
'shumai',
'files/asset-1/turntable.mp4',
targetPath,
)
expect(res.viewsKey).toBe('files/asset-1/views.webp')
})
})
})
@@ -12,6 +12,7 @@ vi.mock('child_process', () => ({
execFile: vi.fn(),
}))
import {
ensureLocalMediaFile,
getMediaInfoActivity,
overlayAnnotationsActivity,
renderPdfPagesActivity,
@@ -107,6 +108,7 @@ describe('Transcode Activities', () => {
vi.clearAllMocks()
const fs = await import('fs')
vi.mocked(fs.readFileSync).mockImplementation(() => Buffer.from('fake data') as never)
vi.mocked(fs.existsSync).mockReturnValue(true)
})
it('should convert CSV via Gotenberg in landscape mode when maxCols > 5', async () => {
@@ -941,14 +943,43 @@ describe('Transcode Activities', () => {
})
describe('Non-retryable Error Handling', () => {
it('should throw non-retryable ApplicationFailure when downloadMediaToTmpActivity fails with ENOENT', async () => {
it('should throw non-retryable ApplicationFailure when downloadMediaToTmpActivity fails with ENOENT or AWS SDK NoSuchKey', async () => {
vi.mocked(s3Service.downloadToFile).mockRejectedValue({
code: 'ENOENT',
message: 'ENOENT: no such file or directory',
name: 'NoSuchKey',
message: 'The specified key does not exist.',
})
await expect(downloadMediaToTmpActivity({ assetKey: 'missing.mp4' })).rejects.toThrowError(
/Failed to download media to tmp/,
await expect(downloadMediaToTmpActivity({ assetKey: 'missing.mp4' })).rejects.toMatchObject({
message: expect.stringMatching(/Failed to download media to tmp/),
nonRetryable: true,
})
})
it('should throw non-retryable ApplicationFailure when ensureLocalMediaFile fails with ENOENT or AWS SDK NoSuchKey', async () => {
vi.mocked(fs.existsSync).mockReturnValue(false)
vi.mocked(s3Service.downloadToFile).mockRejectedValue({
name: 'NoSuchKey',
message: 'The specified key does not exist.',
})
const targetPath = `/tmp/test-recovery-${Date.now()}.mp4`
await expect(ensureLocalMediaFile('missing.mp4', targetPath)).rejects.toMatchObject({
message: expect.stringMatching(/Failed to download media to local file/),
nonRetryable: true,
})
})
it('should recover missing local file from S3 via ensureLocalMediaFile', async () => {
vi.mocked(fs.existsSync).mockReturnValue(false)
vi.mocked(s3Service.downloadToFile).mockResolvedValue()
const targetPath = `/tmp/test-recovery-${Date.now()}.mp4`
const result = await ensureLocalMediaFile('asset.mp4', targetPath)
expect(result).toBe(targetPath)
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
expect.any(String),
'asset.mp4',
targetPath,
)
})
@@ -1142,6 +1173,37 @@ describe('Transcode Activities', () => {
)
})
it('generateSpriteActivity recovers local file from inputKey when provided and missing on disk', async () => {
vi.mocked(fs.existsSync).mockReturnValue(false)
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
vi.mocked(s3Service.downloadToFile).mockResolvedValue()
vi.mocked(transcodeService.generateSprite).mockImplementation(
async (_in, spriteOut, posterOut) => {
const fs = await import('fs')
fs.writeFileSync(spriteOut, 'fake-sprite')
fs.writeFileSync(posterOut, 'fake-poster')
},
)
const targetPath = `/tmp/test-sprite-${Date.now()}.pdf`
await generateSpriteActivity({
assetKey: 'files/asset-1/doc.docx',
inputKey: 'files/asset-1/proxy.pdf',
filePath: targetPath,
mediaInfo: {
duration: 10,
} as unknown as PrismaJson.MediaInfo,
spriteSpec: { key: 'files/asset-1/sprite.webp', frames: 100, tileX: 10, tileY: 10 },
posterSpec: { key: 'files/asset-1/poster.webp' },
})
expect(s3Service.downloadToFile).toHaveBeenCalledWith(
expect.any(String),
'files/asset-1/proxy.pdf',
targetPath,
)
})
it('should throw non-retryable ApplicationFailure when takeScreenshotsActivity fails with error', async () => {
vi.mocked(transcodeService.takeScreenshots).mockRejectedValue(new Error('FFmpeg failed'))
+70 -26
View File
@@ -22,38 +22,19 @@ import {
isMarkdownDocument,
isOfficeDocument,
} from '@shumai/core/src/utils/mime'
import { logger } from '@shumai/core/src/logger'
import { ApplicationFailure, defineStep, getLocalTaskAbortSignal } from '@shumai/workflow-core'
function getActivityCancellationSignal(taskId?: string): AbortSignal | undefined {
return getLocalTaskAbortSignal(taskId)
}
export async function ensureLocalMediaFile(assetKey: string, filePath: string): Promise<string> {
if (fs.existsSync(filePath)) {
return filePath
}
const dir = path.dirname(filePath)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
const bucket = process.env.S3_BUCKET || 'shumai'
await s3Service.downloadToFile(bucket, assetKey, filePath)
return filePath
}
import * as path from 'path'
import * as fs from 'fs'
import * as os from 'os'
import { execFile } from 'child_process'
import { promisify } from 'util'
import { ulid } from 'ulid'
import { logger } from '@shumai/core/src/logger'
import { ApplicationFailure, defineStep, getLocalTaskAbortSignal } from '@shumai/workflow-core'
const execFileAsync = promisify(execFile)
export interface GetMediaInfoActivityParams {
assetId: string
assetKey: string
mediaType: string
function getActivityCancellationSignal(taskId?: string): AbortSignal | undefined {
return getLocalTaskAbortSignal(taskId)
}
function getErrorDetails(err: unknown): { code?: string; message: string; name?: string } {
@@ -75,6 +56,46 @@ function getErrorDetails(err: unknown): { code?: string; message: string; name?:
}
}
export async function ensureLocalMediaFile(assetKey: string, filePath: string): Promise<string> {
if (fs.existsSync(filePath)) {
return filePath
}
const dir = path.dirname(filePath)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
const bucket = process.env.S3_BUCKET || 'shumai'
try {
await s3Service.downloadToFile(bucket, assetKey, filePath)
} catch (err) {
const { code, message, name } = getErrorDetails(err)
const lowerMsg = message.toLowerCase()
const lowerCode = (code || '').toLowerCase()
const lowerName = (name || '').toLowerCase()
if (
lowerCode === 'enoent' ||
lowerCode === 'nosuchkey' ||
lowerName === 'nosuchkey' ||
lowerMsg.includes('enoent') ||
lowerMsg.includes('nosuchkey')
) {
throw ApplicationFailure.create({
message: `Failed to download media to local file: ${message}`,
nonRetryable: true,
cause: err instanceof Error ? err : undefined,
})
}
throw err
}
return filePath
}
export interface GetMediaInfoActivityParams {
assetId: string
assetKey: string
mediaType: string
}
async function ensureAssetNotPurging(assetKey: string): Promise<void> {
const sk = await prisma.storageKey.findUnique({
where: { key: assetKey },
@@ -129,6 +150,8 @@ export const getMediaInfoActivity = defineStep(async function getMediaInfoActivi
nonRetryable: true,
})
}
} else if (params.assetKey) {
mediaSource = await ensureLocalMediaFile(params.assetKey, mediaSource)
}
const fileType = isVideo
@@ -846,6 +869,7 @@ export const transcodeImageActivity = defineStep(async function transcodeImageAc
export interface GenerateSpriteActivityParams {
taskId?: string
assetKey: string
inputKey?: string
filePath: string
mediaInfo: PrismaJson.MediaInfo
spriteSpec: PrismaJson.SpriteInfo
@@ -872,7 +896,7 @@ export const generateSpriteActivity = defineStep(async function generateSpriteAc
// Not found
}
await ensureLocalMediaFile(params.assetKey, params.filePath)
await ensureLocalMediaFile(params.inputKey || params.assetKey, params.filePath)
const tmpDir = path.dirname(params.filePath)
const spriteFile = path.join(tmpDir, 'sprite.webp')
const posterFile = path.join(tmpDir, 'poster.webp')
@@ -979,6 +1003,8 @@ export interface GeneratePdfProxyActivityParams {
export const generatePdfProxyActivity = defineStep(async function generatePdfProxyActivity(
params: GeneratePdfProxyActivityParams,
): Promise<{ pdfProxyKey: string; pdfFilePath: string }> {
await ensureLocalMediaFile(params.assetKey, params.filePath)
const isOffice = isOfficeDocument(params.mediaType, params.filename)
const isHtml = isHtmlDocument(params.mediaType, params.filename)
const isMd = isMarkdownDocument(params.mediaType, params.filename)
@@ -1107,9 +1133,17 @@ export const downloadMediaToTmpActivity = defineStep(
try {
await s3Service.downloadToFile(bucket, params.assetKey, filePath)
} catch (err) {
const { code, message } = getErrorDetails(err)
const { code, message, name } = getErrorDetails(err)
const lowerMsg = message.toLowerCase()
if (code === 'ENOENT' || lowerMsg.includes('enoent') || lowerMsg.includes('nosuchkey')) {
const lowerCode = (code || '').toLowerCase()
const lowerName = (name || '').toLowerCase()
if (
lowerCode === 'enoent' ||
lowerCode === 'nosuchkey' ||
lowerName === 'nosuchkey' ||
lowerMsg.includes('enoent') ||
lowerMsg.includes('nosuchkey')
) {
throw ApplicationFailure.create({
message: `Failed to download media to tmp: ${message}`,
nonRetryable: true,
@@ -1765,6 +1799,8 @@ export const start3dRenderActivity = defineStep(async function start3dRenderActi
})
}
await ensureLocalMediaFile(params.assetKey, params.filePath)
const bucket = process.env.S3_BUCKET || 'shumai'
const tmpDir = path.dirname(params.filePath)
const posterFilePath = path.join(tmpDir, 'poster.webp')
@@ -1987,6 +2023,7 @@ export const render3dModelActivity = defineStep(async function render3dModelActi
export interface Generate3dSpriteActivityParams {
taskId: string
assetKey: string
videoKey?: string
videoFilePath: string
spriteKey: string
}
@@ -2002,6 +2039,9 @@ export const generate3dSpriteActivity = defineStep(async function generate3dSpri
})
}
const downloadKey = params.videoKey || params.assetKey
await ensureLocalMediaFile(downloadKey, params.videoFilePath)
const bucket = process.env.S3_BUCKET || 'shumai'
const tmpDir = path.dirname(params.videoFilePath)
const spriteFilePath = path.join(tmpDir, 'sprite.webp')
@@ -2035,6 +2075,7 @@ export const generate3dSpriteActivity = defineStep(async function generate3dSpri
export interface Generate3dViewsActivityParams {
taskId: string
assetKey: string
videoKey?: string
videoFilePath: string
viewsKey: string
}
@@ -2050,6 +2091,9 @@ export const generate3dViewsActivity = defineStep(async function generate3dViews
})
}
const downloadKey = params.videoKey || params.assetKey
await ensureLocalMediaFile(downloadKey, params.videoFilePath)
const bucket = process.env.S3_BUCKET || 'shumai'
const tmpDir = path.dirname(params.videoFilePath)
const viewsFilePath = path.join(tmpDir, 'views.webp')
@@ -165,6 +165,7 @@ describe('transcode3dWorkflow', () => {
expect(mockActivities.generate3dSpriteActivity).toHaveBeenCalledWith({
taskId: 'task-3d',
assetKey: 'files/asset-3d/robot.glb',
videoKey: 'files/asset-3d/turntable.mp4',
videoFilePath: '/tmp/turntable.mp4',
spriteKey: 'files/asset-3d/sprite.webp',
})
@@ -172,6 +173,7 @@ describe('transcode3dWorkflow', () => {
expect(mockActivities.generate3dViewsActivity).toHaveBeenCalledWith({
taskId: 'task-3d',
assetKey: 'files/asset-3d/robot.glb',
videoKey: 'files/asset-3d/turntable.mp4',
videoFilePath: '/tmp/turntable.mp4',
viewsKey: 'files/asset-3d/views.webp',
})
@@ -99,6 +99,7 @@ export async function transcode3dWorkflow(task: WorkflowTask): Promise<void> {
await generate3dSpriteActivity({
taskId: task.id,
assetKey: key,
videoKey,
videoFilePath: renderVideo.videoFilePath,
spriteKey,
})
@@ -113,6 +114,7 @@ export async function transcode3dWorkflow(task: WorkflowTask): Promise<void> {
await generate3dViewsActivity({
taskId: task.id,
assetKey: key,
videoKey,
videoFilePath: renderVideo.videoFilePath,
viewsKey,
})
@@ -118,6 +118,13 @@ describe('transcodeImageWorkflow', () => {
await transcodeImageWorkflow(task)
expect(mockActivities.getMediaInfoActivity).toHaveBeenCalledWith(
expect.objectContaining({
assetKey: 'image.jpg',
filePath: '/tmp/image.jpg',
}),
)
expect(mockActivities.updateAssetStatusActivity).toHaveBeenCalledWith({
assetId: 'asset-image',
status: AssetStatus.processing,
@@ -33,6 +33,7 @@ export async function transcodeImageWorkflow(task: WorkflowTask): Promise<void>
const spec = task.payload?.transcode || {}
const mediaInfo = await getMediaInfoActivity({
assetKey: key,
filePath,
assetId: asset.id,
proxyType: 'image',
@@ -128,8 +128,17 @@ describe('transcodePdfWorkflow', () => {
status: AssetStatus.processing,
})
expect(mockActivities.getMediaInfoActivity).toHaveBeenCalledWith({
assetKey: 'document.pdf',
filePath: '/tmp/doc.pdf',
assetId: 'asset-pdf',
proxyType: 'pdf',
mediaType: 'application/pdf',
})
expect(mockActivities.generateSpriteActivity).toHaveBeenCalledWith({
assetKey: 'document.pdf',
inputKey: 'document.pdf',
filePath: '/tmp/doc.pdf',
spriteSpec: expect.objectContaining({ key: 'sprite.webp' }),
posterSpec: expect.objectContaining({ key: 'poster.webp' }),
@@ -45,6 +45,7 @@ export async function transcodePdfWorkflow(task: WorkflowTask): Promise<void> {
const spec = task.payload?.transcode || {}
const mediaInfo = await getMediaInfoActivity({
assetKey: pdfProxy.pdfProxyKey,
filePath: currentFilePath,
assetId: asset.id,
proxyType: 'pdf',
@@ -76,6 +77,7 @@ export async function transcodePdfWorkflow(task: WorkflowTask): Promise<void> {
const spriteResult = await generateSpriteActivity({
assetKey: key,
inputKey: pdfProxy.pdfProxyKey,
filePath: currentFilePath,
spriteSpec,
posterSpec,