mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-05 23:54:25 +00:00
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:
@@ -442,6 +442,10 @@ In multi-replica deployments (e.g., multiple transcode or agent worker instances
|
|||||||
- In Docker/container environments, `HOSTNAME` (container ID) is automatically used.
|
- In Docker/container environments, `HOSTNAME` (container ID) is automatically used.
|
||||||
- On bare metal/multi-process hosts, `process.pid` and an instance suffix are appended to guarantee uniqueness.
|
- On bare metal/multi-process hosts, `process.pid` and an instance suffix are appended to guarantee uniqueness.
|
||||||
|
|
||||||
|
### Durable Step File Exchange
|
||||||
|
|
||||||
|
Intermediate files passed across steps must be saved to durable storage (S3) by the producing step, consuming steps must use `ensureLocalMediaFile` (or S3 fetch) rather than assuming local `/tmp` presence, and any purely temporary S3 scratch files must be cleaned up in a `finally` block.
|
||||||
|
|
||||||
## Import conventions
|
## Import conventions
|
||||||
|
|
||||||
- When importing across workspace packages, always use absolute workspace imports (e.g. `import { prisma } from '@shumai/db'`, `import { teamService } from '@shumai/core/src/team/team'`).
|
- When importing across workspace packages, always use absolute workspace imports (e.g. `import { prisma } from '@shumai/db'`, `import { teamService } from '@shumai/core/src/team/team'`).
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
* **transcode**: Fix an issue where PDF, 3D model, and image background transcode workflows could fail with missing temporary file errors if a worker restarted or recovered midway through processing
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
* **workflow**: Migrate the background workflow engine to DBOS, unifying all-in-one and dedicated worker deployments using the existing PostgreSQL database without requiring a separate Temporal cluster
|
* **workflow**: Migrate the background workflow engine to DBOS, unifying all-in-one and dedicated worker deployments using the existing PostgreSQL database without requiring a separate Temporal cluster
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { setupTestDbHooks } from '@shumai/db/test'
|
|||||||
import { workflowService, TaskQueueTranscode } from '@shumai/workflow-core'
|
import { workflowService, TaskQueueTranscode } from '@shumai/workflow-core'
|
||||||
import { initTranscodeWorkflows } from '@shumai/transcode'
|
import { initTranscodeWorkflows } from '@shumai/transcode'
|
||||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||||
|
import { transcodeService } from '@shumai/core/src/transcode/transcode'
|
||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
@@ -289,4 +290,137 @@ describe('Workflow E2E - transcodePdfWorkflow', () => {
|
|||||||
expect(mediaInfo.sprite?.key).toContain('sprite.webp')
|
expect(mediaInfo.sprite?.key).toContain('sprite.webp')
|
||||||
expect(mediaInfo.frames).toBeGreaterThan(0)
|
expect(mediaInfo.frames).toBeGreaterThan(0)
|
||||||
}, 50000)
|
}, 50000)
|
||||||
|
|
||||||
|
it('should recover and complete workflow when intermediate local proxy is deleted before downstream steps', async () => {
|
||||||
|
// 1. Create Team, Project, Asset & StorageKey
|
||||||
|
const team = await prisma.team.create({
|
||||||
|
data: { name: 'E2E Recovery Transcode Team' },
|
||||||
|
})
|
||||||
|
|
||||||
|
const project = await prisma.project.create({
|
||||||
|
data: { name: 'E2E Recovery Transcode Project', teamId: team.id },
|
||||||
|
})
|
||||||
|
|
||||||
|
const storageKey = await prisma.storageKey.create({
|
||||||
|
data: {
|
||||||
|
key: 'projects/e2e/recovery-doc.md',
|
||||||
|
status: 'active',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const asset = await prisma.asset.create({
|
||||||
|
data: {
|
||||||
|
name: 'recovery-doc.md',
|
||||||
|
type: 'file',
|
||||||
|
status: 'uploaded',
|
||||||
|
mediaType: 'text/markdown',
|
||||||
|
projectId: project.id,
|
||||||
|
storageKeyId: storageKey.id,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// 2. Seed S3 Storage with Markdown text
|
||||||
|
const mdText = '# Recovery Test\n\nVerifying that intermediate proxy.pdf recovers from S3.'
|
||||||
|
const mdBuffer = Buffer.from(mdText, 'utf-8')
|
||||||
|
await s3Service.putObject(
|
||||||
|
'shumai-e2e-test-bucket-transcode',
|
||||||
|
'projects/e2e/recovery-doc.md',
|
||||||
|
mdBuffer,
|
||||||
|
mdBuffer.length,
|
||||||
|
'text/markdown',
|
||||||
|
)
|
||||||
|
|
||||||
|
// 3. Spy on createTempDir and s3Service to intercept proxy.pdf upload and delete it locally
|
||||||
|
let capturedTmpDir: string | undefined
|
||||||
|
const origCreateTempDir = transcodeService.createTempDir.bind(transcodeService)
|
||||||
|
const createTempDirSpy = vi
|
||||||
|
.spyOn(transcodeService, 'createTempDir')
|
||||||
|
.mockImplementation((prefix: string) => {
|
||||||
|
const dir = origCreateTempDir(prefix)
|
||||||
|
if (prefix.startsWith('transcode-')) {
|
||||||
|
capturedTmpDir = dir
|
||||||
|
}
|
||||||
|
return dir
|
||||||
|
})
|
||||||
|
|
||||||
|
const origPutObject = s3Service.putObject.bind(s3Service)
|
||||||
|
const origDownloadToFile = s3Service.downloadToFile.bind(s3Service)
|
||||||
|
const downloadedKeys: string[] = []
|
||||||
|
|
||||||
|
const downloadSpy = vi
|
||||||
|
.spyOn(s3Service, 'downloadToFile')
|
||||||
|
.mockImplementation(async (bucket, key, localPath) => {
|
||||||
|
downloadedKeys.push(key)
|
||||||
|
return origDownloadToFile(bucket, key, localPath)
|
||||||
|
})
|
||||||
|
|
||||||
|
const putSpy = vi.spyOn(s3Service, 'putObject').mockImplementation(async (...args) => {
|
||||||
|
const result = await origPutObject(...args)
|
||||||
|
const key = String(args[1] || '')
|
||||||
|
if (key.endsWith('proxy.pdf') && capturedTmpDir) {
|
||||||
|
// Once proxy.pdf has been uploaded to S3 by generatePdfProxyActivity,
|
||||||
|
// delete the local file to simulate empty filesystem on a recovery/worker node.
|
||||||
|
const localProxyPath = path.join(capturedTmpDir, 'proxy.pdf')
|
||||||
|
if (fs.existsSync(localProxyPath)) {
|
||||||
|
fs.unlinkSync(localProxyPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
})
|
||||||
|
|
||||||
|
// 4. Create Workflow Task
|
||||||
|
const task = await prisma.workflowTask.create({
|
||||||
|
data: {
|
||||||
|
type: 'transcode_pdf',
|
||||||
|
status: 'pending',
|
||||||
|
assetId: asset.id,
|
||||||
|
projectId: project.id,
|
||||||
|
teamId: team.id,
|
||||||
|
payload: {
|
||||||
|
projectId: project.id,
|
||||||
|
transcode: {
|
||||||
|
poster: true,
|
||||||
|
sprite: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log(
|
||||||
|
`Submitted E2E Recovery Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`,
|
||||||
|
)
|
||||||
|
const completedTask = await workflowService.executeWait(task, 45000)
|
||||||
|
|
||||||
|
// 5. Verification
|
||||||
|
expect(completedTask.status).toBe('completed')
|
||||||
|
|
||||||
|
const updatedAsset = await prisma.asset.findUnique({
|
||||||
|
where: { id: asset.id },
|
||||||
|
})
|
||||||
|
expect(updatedAsset?.status).toBe(AssetStatus.processed)
|
||||||
|
|
||||||
|
const mediaInfo = updatedAsset?.media as unknown as {
|
||||||
|
proxyType?: string
|
||||||
|
pdfTranscode?: { key: string }
|
||||||
|
poster?: { key: string }
|
||||||
|
sprite?: { key: string }
|
||||||
|
frames?: number
|
||||||
|
}
|
||||||
|
expect(mediaInfo).toBeDefined()
|
||||||
|
expect(mediaInfo.proxyType).toBe('pdf')
|
||||||
|
expect(mediaInfo.pdfTranscode?.key).toBe('projects/e2e/proxy.pdf')
|
||||||
|
expect(mediaInfo.poster?.key).toContain('poster.webp')
|
||||||
|
expect(mediaInfo.sprite?.key).toContain('sprite.webp')
|
||||||
|
expect(mediaInfo.frames).toBeGreaterThan(0)
|
||||||
|
|
||||||
|
// Verify that proxy.pdf was re-downloaded from S3 via ensureLocalMediaFile
|
||||||
|
const proxyDownloadCall = downloadedKeys.find((k) => k.endsWith('proxy.pdf'))
|
||||||
|
expect(proxyDownloadCall).toBeDefined()
|
||||||
|
} finally {
|
||||||
|
createTempDirSpy.mockRestore()
|
||||||
|
downloadSpy.mockRestore()
|
||||||
|
putSpy.mockRestore()
|
||||||
|
}
|
||||||
|
}, 50000)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ vi.mock('@shumai/core/src/turntable/turntable', () => ({
|
|||||||
vi.mock('@shumai/core/src/s3/s3', () => ({
|
vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||||
s3Service: {
|
s3Service: {
|
||||||
putObject: vi.fn(),
|
putObject: vi.fn(),
|
||||||
|
downloadToFile: vi.fn(),
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
|
|
||||||
@@ -120,6 +121,46 @@ describe('3D Transcode Activities', () => {
|
|||||||
expect(res.modelMetadata).toEqual({ format: 'glb', vertices: 120 })
|
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 () => {
|
it('throws ApplicationFailure when poster file ID is missing', async () => {
|
||||||
vi.mocked(turntableService.uploadFile).mockResolvedValue({
|
vi.mocked(turntableService.uploadFile).mockResolvedValue({
|
||||||
id: 'f_input',
|
id: 'f_input',
|
||||||
@@ -326,6 +367,24 @@ describe('3D Transcode Activities', () => {
|
|||||||
)
|
)
|
||||||
expect(res.spriteKey).toBe('files/asset-1/sprite.webp')
|
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', () => {
|
describe('generate3dViewsActivity', () => {
|
||||||
@@ -351,5 +410,23 @@ describe('3D Transcode Activities', () => {
|
|||||||
)
|
)
|
||||||
expect(res.viewsKey).toBe('files/asset-1/views.webp')
|
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(),
|
execFile: vi.fn(),
|
||||||
}))
|
}))
|
||||||
import {
|
import {
|
||||||
|
ensureLocalMediaFile,
|
||||||
getMediaInfoActivity,
|
getMediaInfoActivity,
|
||||||
overlayAnnotationsActivity,
|
overlayAnnotationsActivity,
|
||||||
renderPdfPagesActivity,
|
renderPdfPagesActivity,
|
||||||
@@ -107,6 +108,7 @@ describe('Transcode Activities', () => {
|
|||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
const fs = await import('fs')
|
const fs = await import('fs')
|
||||||
vi.mocked(fs.readFileSync).mockImplementation(() => Buffer.from('fake data') as never)
|
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 () => {
|
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', () => {
|
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({
|
vi.mocked(s3Service.downloadToFile).mockRejectedValue({
|
||||||
code: 'ENOENT',
|
name: 'NoSuchKey',
|
||||||
message: 'ENOENT: no such file or directory',
|
message: 'The specified key does not exist.',
|
||||||
})
|
})
|
||||||
|
|
||||||
await expect(downloadMediaToTmpActivity({ assetKey: 'missing.mp4' })).rejects.toThrowError(
|
await expect(downloadMediaToTmpActivity({ assetKey: 'missing.mp4' })).rejects.toMatchObject({
|
||||||
/Failed to download media to tmp/,
|
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 () => {
|
it('should throw non-retryable ApplicationFailure when takeScreenshotsActivity fails with error', async () => {
|
||||||
vi.mocked(transcodeService.takeScreenshots).mockRejectedValue(new Error('FFmpeg failed'))
|
vi.mocked(transcodeService.takeScreenshots).mockRejectedValue(new Error('FFmpeg failed'))
|
||||||
|
|
||||||
|
|||||||
@@ -22,38 +22,19 @@ import {
|
|||||||
isMarkdownDocument,
|
isMarkdownDocument,
|
||||||
isOfficeDocument,
|
isOfficeDocument,
|
||||||
} from '@shumai/core/src/utils/mime'
|
} 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 path from 'path'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
import { execFile } from 'child_process'
|
import { execFile } from 'child_process'
|
||||||
import { promisify } from 'util'
|
import { promisify } from 'util'
|
||||||
import { ulid } from 'ulid'
|
import { ulid } from 'ulid'
|
||||||
|
import { logger } from '@shumai/core/src/logger'
|
||||||
|
import { ApplicationFailure, defineStep, getLocalTaskAbortSignal } from '@shumai/workflow-core'
|
||||||
|
|
||||||
const execFileAsync = promisify(execFile)
|
const execFileAsync = promisify(execFile)
|
||||||
|
|
||||||
export interface GetMediaInfoActivityParams {
|
function getActivityCancellationSignal(taskId?: string): AbortSignal | undefined {
|
||||||
assetId: string
|
return getLocalTaskAbortSignal(taskId)
|
||||||
assetKey: string
|
|
||||||
mediaType: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getErrorDetails(err: unknown): { code?: string; message: string; name?: string } {
|
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> {
|
async function ensureAssetNotPurging(assetKey: string): Promise<void> {
|
||||||
const sk = await prisma.storageKey.findUnique({
|
const sk = await prisma.storageKey.findUnique({
|
||||||
where: { key: assetKey },
|
where: { key: assetKey },
|
||||||
@@ -129,6 +150,8 @@ export const getMediaInfoActivity = defineStep(async function getMediaInfoActivi
|
|||||||
nonRetryable: true,
|
nonRetryable: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
} else if (params.assetKey) {
|
||||||
|
mediaSource = await ensureLocalMediaFile(params.assetKey, mediaSource)
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileType = isVideo
|
const fileType = isVideo
|
||||||
@@ -846,6 +869,7 @@ export const transcodeImageActivity = defineStep(async function transcodeImageAc
|
|||||||
export interface GenerateSpriteActivityParams {
|
export interface GenerateSpriteActivityParams {
|
||||||
taskId?: string
|
taskId?: string
|
||||||
assetKey: string
|
assetKey: string
|
||||||
|
inputKey?: string
|
||||||
filePath: string
|
filePath: string
|
||||||
mediaInfo: PrismaJson.MediaInfo
|
mediaInfo: PrismaJson.MediaInfo
|
||||||
spriteSpec: PrismaJson.SpriteInfo
|
spriteSpec: PrismaJson.SpriteInfo
|
||||||
@@ -872,7 +896,7 @@ export const generateSpriteActivity = defineStep(async function generateSpriteAc
|
|||||||
// Not found
|
// Not found
|
||||||
}
|
}
|
||||||
|
|
||||||
await ensureLocalMediaFile(params.assetKey, params.filePath)
|
await ensureLocalMediaFile(params.inputKey || params.assetKey, params.filePath)
|
||||||
const tmpDir = path.dirname(params.filePath)
|
const tmpDir = path.dirname(params.filePath)
|
||||||
const spriteFile = path.join(tmpDir, 'sprite.webp')
|
const spriteFile = path.join(tmpDir, 'sprite.webp')
|
||||||
const posterFile = path.join(tmpDir, 'poster.webp')
|
const posterFile = path.join(tmpDir, 'poster.webp')
|
||||||
@@ -979,6 +1003,8 @@ export interface GeneratePdfProxyActivityParams {
|
|||||||
export const generatePdfProxyActivity = defineStep(async function generatePdfProxyActivity(
|
export const generatePdfProxyActivity = defineStep(async function generatePdfProxyActivity(
|
||||||
params: GeneratePdfProxyActivityParams,
|
params: GeneratePdfProxyActivityParams,
|
||||||
): Promise<{ pdfProxyKey: string; pdfFilePath: string }> {
|
): Promise<{ pdfProxyKey: string; pdfFilePath: string }> {
|
||||||
|
await ensureLocalMediaFile(params.assetKey, params.filePath)
|
||||||
|
|
||||||
const isOffice = isOfficeDocument(params.mediaType, params.filename)
|
const isOffice = isOfficeDocument(params.mediaType, params.filename)
|
||||||
const isHtml = isHtmlDocument(params.mediaType, params.filename)
|
const isHtml = isHtmlDocument(params.mediaType, params.filename)
|
||||||
const isMd = isMarkdownDocument(params.mediaType, params.filename)
|
const isMd = isMarkdownDocument(params.mediaType, params.filename)
|
||||||
@@ -1107,9 +1133,17 @@ export const downloadMediaToTmpActivity = defineStep(
|
|||||||
try {
|
try {
|
||||||
await s3Service.downloadToFile(bucket, params.assetKey, filePath)
|
await s3Service.downloadToFile(bucket, params.assetKey, filePath)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const { code, message } = getErrorDetails(err)
|
const { code, message, name } = getErrorDetails(err)
|
||||||
const lowerMsg = message.toLowerCase()
|
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({
|
throw ApplicationFailure.create({
|
||||||
message: `Failed to download media to tmp: ${message}`,
|
message: `Failed to download media to tmp: ${message}`,
|
||||||
nonRetryable: true,
|
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 bucket = process.env.S3_BUCKET || 'shumai'
|
||||||
const tmpDir = path.dirname(params.filePath)
|
const tmpDir = path.dirname(params.filePath)
|
||||||
const posterFilePath = path.join(tmpDir, 'poster.webp')
|
const posterFilePath = path.join(tmpDir, 'poster.webp')
|
||||||
@@ -1987,6 +2023,7 @@ export const render3dModelActivity = defineStep(async function render3dModelActi
|
|||||||
export interface Generate3dSpriteActivityParams {
|
export interface Generate3dSpriteActivityParams {
|
||||||
taskId: string
|
taskId: string
|
||||||
assetKey: string
|
assetKey: string
|
||||||
|
videoKey?: string
|
||||||
videoFilePath: string
|
videoFilePath: string
|
||||||
spriteKey: 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 bucket = process.env.S3_BUCKET || 'shumai'
|
||||||
const tmpDir = path.dirname(params.videoFilePath)
|
const tmpDir = path.dirname(params.videoFilePath)
|
||||||
const spriteFilePath = path.join(tmpDir, 'sprite.webp')
|
const spriteFilePath = path.join(tmpDir, 'sprite.webp')
|
||||||
@@ -2035,6 +2075,7 @@ export const generate3dSpriteActivity = defineStep(async function generate3dSpri
|
|||||||
export interface Generate3dViewsActivityParams {
|
export interface Generate3dViewsActivityParams {
|
||||||
taskId: string
|
taskId: string
|
||||||
assetKey: string
|
assetKey: string
|
||||||
|
videoKey?: string
|
||||||
videoFilePath: string
|
videoFilePath: string
|
||||||
viewsKey: 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 bucket = process.env.S3_BUCKET || 'shumai'
|
||||||
const tmpDir = path.dirname(params.videoFilePath)
|
const tmpDir = path.dirname(params.videoFilePath)
|
||||||
const viewsFilePath = path.join(tmpDir, 'views.webp')
|
const viewsFilePath = path.join(tmpDir, 'views.webp')
|
||||||
|
|||||||
@@ -165,6 +165,7 @@ describe('transcode3dWorkflow', () => {
|
|||||||
expect(mockActivities.generate3dSpriteActivity).toHaveBeenCalledWith({
|
expect(mockActivities.generate3dSpriteActivity).toHaveBeenCalledWith({
|
||||||
taskId: 'task-3d',
|
taskId: 'task-3d',
|
||||||
assetKey: 'files/asset-3d/robot.glb',
|
assetKey: 'files/asset-3d/robot.glb',
|
||||||
|
videoKey: 'files/asset-3d/turntable.mp4',
|
||||||
videoFilePath: '/tmp/turntable.mp4',
|
videoFilePath: '/tmp/turntable.mp4',
|
||||||
spriteKey: 'files/asset-3d/sprite.webp',
|
spriteKey: 'files/asset-3d/sprite.webp',
|
||||||
})
|
})
|
||||||
@@ -172,6 +173,7 @@ describe('transcode3dWorkflow', () => {
|
|||||||
expect(mockActivities.generate3dViewsActivity).toHaveBeenCalledWith({
|
expect(mockActivities.generate3dViewsActivity).toHaveBeenCalledWith({
|
||||||
taskId: 'task-3d',
|
taskId: 'task-3d',
|
||||||
assetKey: 'files/asset-3d/robot.glb',
|
assetKey: 'files/asset-3d/robot.glb',
|
||||||
|
videoKey: 'files/asset-3d/turntable.mp4',
|
||||||
videoFilePath: '/tmp/turntable.mp4',
|
videoFilePath: '/tmp/turntable.mp4',
|
||||||
viewsKey: 'files/asset-3d/views.webp',
|
viewsKey: 'files/asset-3d/views.webp',
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ export async function transcode3dWorkflow(task: WorkflowTask): Promise<void> {
|
|||||||
await generate3dSpriteActivity({
|
await generate3dSpriteActivity({
|
||||||
taskId: task.id,
|
taskId: task.id,
|
||||||
assetKey: key,
|
assetKey: key,
|
||||||
|
videoKey,
|
||||||
videoFilePath: renderVideo.videoFilePath,
|
videoFilePath: renderVideo.videoFilePath,
|
||||||
spriteKey,
|
spriteKey,
|
||||||
})
|
})
|
||||||
@@ -113,6 +114,7 @@ export async function transcode3dWorkflow(task: WorkflowTask): Promise<void> {
|
|||||||
await generate3dViewsActivity({
|
await generate3dViewsActivity({
|
||||||
taskId: task.id,
|
taskId: task.id,
|
||||||
assetKey: key,
|
assetKey: key,
|
||||||
|
videoKey,
|
||||||
videoFilePath: renderVideo.videoFilePath,
|
videoFilePath: renderVideo.videoFilePath,
|
||||||
viewsKey,
|
viewsKey,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -118,6 +118,13 @@ describe('transcodeImageWorkflow', () => {
|
|||||||
|
|
||||||
await transcodeImageWorkflow(task)
|
await transcodeImageWorkflow(task)
|
||||||
|
|
||||||
|
expect(mockActivities.getMediaInfoActivity).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
assetKey: 'image.jpg',
|
||||||
|
filePath: '/tmp/image.jpg',
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
expect(mockActivities.updateAssetStatusActivity).toHaveBeenCalledWith({
|
expect(mockActivities.updateAssetStatusActivity).toHaveBeenCalledWith({
|
||||||
assetId: 'asset-image',
|
assetId: 'asset-image',
|
||||||
status: AssetStatus.processing,
|
status: AssetStatus.processing,
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ export async function transcodeImageWorkflow(task: WorkflowTask): Promise<void>
|
|||||||
|
|
||||||
const spec = task.payload?.transcode || {}
|
const spec = task.payload?.transcode || {}
|
||||||
const mediaInfo = await getMediaInfoActivity({
|
const mediaInfo = await getMediaInfoActivity({
|
||||||
|
assetKey: key,
|
||||||
filePath,
|
filePath,
|
||||||
assetId: asset.id,
|
assetId: asset.id,
|
||||||
proxyType: 'image',
|
proxyType: 'image',
|
||||||
|
|||||||
@@ -128,8 +128,17 @@ describe('transcodePdfWorkflow', () => {
|
|||||||
status: AssetStatus.processing,
|
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({
|
expect(mockActivities.generateSpriteActivity).toHaveBeenCalledWith({
|
||||||
assetKey: 'document.pdf',
|
assetKey: 'document.pdf',
|
||||||
|
inputKey: 'document.pdf',
|
||||||
filePath: '/tmp/doc.pdf',
|
filePath: '/tmp/doc.pdf',
|
||||||
spriteSpec: expect.objectContaining({ key: 'sprite.webp' }),
|
spriteSpec: expect.objectContaining({ key: 'sprite.webp' }),
|
||||||
posterSpec: expect.objectContaining({ key: 'poster.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 spec = task.payload?.transcode || {}
|
||||||
const mediaInfo = await getMediaInfoActivity({
|
const mediaInfo = await getMediaInfoActivity({
|
||||||
|
assetKey: pdfProxy.pdfProxyKey,
|
||||||
filePath: currentFilePath,
|
filePath: currentFilePath,
|
||||||
assetId: asset.id,
|
assetId: asset.id,
|
||||||
proxyType: 'pdf',
|
proxyType: 'pdf',
|
||||||
@@ -76,6 +77,7 @@ export async function transcodePdfWorkflow(task: WorkflowTask): Promise<void> {
|
|||||||
|
|
||||||
const spriteResult = await generateSpriteActivity({
|
const spriteResult = await generateSpriteActivity({
|
||||||
assetKey: key,
|
assetKey: key,
|
||||||
|
inputKey: pdfProxy.pdfProxyKey,
|
||||||
filePath: currentFilePath,
|
filePath: currentFilePath,
|
||||||
spriteSpec,
|
spriteSpec,
|
||||||
posterSpec,
|
posterSpec,
|
||||||
|
|||||||
Reference in New Issue
Block a user