refactor(monorepo): decouple and split background workflow workers into packages

Splits monolithic workflow workers into four decoupled workspaces under the Bun workspaces monorepo:
- @shumai/workflow-core containing the core execution engine and utilities.
- @shumai/agent containing agent-specific logic, workflows, and activities.
- @shumai/transcode containing transcoding workflows, transcoder, and transcode activities.
- @shumai/worker-db containing database activities.

Introduces a dynamic workflow and activity registration pattern on LocalExecutor to eliminate circular dependencies. Updates worker bootstrapping and test suites to cleanly leverage generic workspace modules.
This commit is contained in:
yiling
2026-06-01 23:29:11 +08:00
parent cb80822637
commit e3f4fa219e
66 changed files with 421 additions and 150 deletions
@@ -0,0 +1,237 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { transcodeMedia } from './transcode'
import { WorkflowTask, WorkflowTaskStatus, WorkflowTaskType, AssetStatus } from '@shumai/db'
import * as workflowUtils from '@shumai/workflow-core'
vi.mock('@shumai/workflow-core', async (importOriginal) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const actual = (await importOriginal()) as any
return {
...actual,
getActivities: vi.fn(),
executeActivity: vi.fn(),
sleep: vi.fn(),
}
})
describe('Transcode Workflow', () => {
const mockActivities = {
updateTaskStatusActivity: Object.assign(vi.fn(), {
_activityName: 'updateTaskStatusActivity',
}),
updateAssetStatusActivity: Object.assign(vi.fn(), {
_activityName: 'updateAssetStatusActivity',
}),
getAssetActivity: Object.assign(vi.fn(), { _activityName: 'getAssetActivity' }),
getMediaInfoActivity: Object.assign(vi.fn(), { _activityName: 'getMediaInfoActivity' }),
transcodeVideoActivity: Object.assign(vi.fn(), { _activityName: 'transcodeVideoActivity' }),
transcodeImageActivity: Object.assign(vi.fn(), { _activityName: 'transcodeImageActivity' }),
generateSpriteActivity: Object.assign(vi.fn(), { _activityName: 'generateSpriteActivity' }),
updateAssetMediaActivity: Object.assign(vi.fn(), {
_activityName: 'updateAssetMediaActivity',
}),
getTranscodeWorkerQueueActivity: Object.assign(vi.fn(), {
_activityName: 'getTranscodeWorkerQueueActivity',
}),
downloadMediaToTmpActivity: Object.assign(vi.fn(), {
_activityName: 'downloadMediaToTmpActivity',
}),
cleanupTmpDirActivity: Object.assign(vi.fn(), { _activityName: 'cleanupTmpDirActivity' }),
}
beforeEach(() => {
vi.clearAllMocks()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(workflowUtils.getActivities as any).mockReturnValue(mockActivities)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(workflowUtils.executeActivity as any).mockImplementation(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(_queue: string, fn: any, ...args: any[]) => {
if (typeof fn !== 'function') {
throw new Error(`fn is not a function in executeActivity. Queue: ${_queue}`)
}
return fn(...args)
},
)
mockActivities.getTranscodeWorkerQueueActivity.mockResolvedValue('transcode_worker_queue')
mockActivities.downloadMediaToTmpActivity.mockResolvedValue({
filePath: '/tmp/video.mp4',
tmpDir: '/tmp',
})
})
it('should process video transcode successfully', async () => {
const task: WorkflowTask = {
id: 'task-1',
assetId: 'asset-1',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
output: null,
payload: {
projectId: 'proj-1',
transcode: {
videoStrategy: 'best_match',
thumbnail: true,
},
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockResolvedValue({
id: 'asset-1',
storageKey: { key: 'video.mp4' },
mediaType: 'video/mp4',
})
mockActivities.getMediaInfoActivity.mockResolvedValue({
mimeType: 'video/mp4',
metadata: { originalWidth: 1920, originalHeight: 1080, duration: 10, frameRate: 30 },
videoTranscodes: [],
imageTranscodes: [],
})
mockActivities.transcodeVideoActivity.mockResolvedValue({
key: 'v.mp4',
width: 1920,
height: 1080,
})
mockActivities.transcodeImageActivity.mockResolvedValue({
key: 't.webp',
width: 480,
height: 270,
})
await transcodeMedia(task)
expect(mockActivities.updateAssetStatusActivity).toHaveBeenCalledWith({
assetId: 'asset-1',
status: AssetStatus.processing,
})
expect(mockActivities.updateAssetMediaActivity).toHaveBeenCalledWith({
assetId: 'asset-1',
mediaInfo: expect.objectContaining({
thumbnail: expect.objectContaining({
key: 't.webp',
}),
}),
})
expect(mockActivities.updateAssetStatusActivity).toHaveBeenCalledWith({
assetId: 'asset-1',
status: AssetStatus.processed,
})
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: 'task-1',
status: WorkflowTaskStatus.completed,
})
})
it('should process image transcode and thumbnail successfully', async () => {
const task: WorkflowTask = {
id: 'task-image',
assetId: 'asset-image',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
output: null,
payload: {
projectId: 'proj-1',
transcode: {
thumbnail: true,
},
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockResolvedValue({
id: 'asset-image',
storageKey: { key: 'image.jpg' },
mediaType: 'image/jpeg',
})
mockActivities.getMediaInfoActivity.mockResolvedValue({
mimeType: 'image/jpeg',
metadata: { originalWidth: 1000, originalHeight: 1000 },
videoTranscodes: [],
imageTranscodes: [],
})
mockActivities.transcodeImageActivity.mockResolvedValue({
key: 't.webp',
width: 480,
height: 480,
format: 'webp',
})
await transcodeMedia(task)
expect(mockActivities.updateAssetStatusActivity).toHaveBeenCalledWith({
assetId: 'asset-image',
status: AssetStatus.processing,
})
expect(mockActivities.updateAssetMediaActivity).toHaveBeenCalledWith({
assetId: 'asset-image',
mediaInfo: expect.objectContaining({
thumbnail: expect.objectContaining({
key: 't.webp',
width: 480,
height: 480,
}),
}),
})
expect(mockActivities.updateAssetStatusActivity).toHaveBeenCalledWith({
assetId: 'asset-image',
status: AssetStatus.processed,
})
})
it('should handle failures and update task status with error', async () => {
const task: WorkflowTask = {
id: 'task-fail',
assetId: 'asset-1',
type: WorkflowTaskType.transcode,
status: WorkflowTaskStatus.pending,
output: null,
payload: {
projectId: 'proj-1',
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockRejectedValue(new Error('FFmpeg failed'))
await expect(transcodeMedia(task)).rejects.toThrow('FFmpeg failed')
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: 'task-fail',
status: WorkflowTaskStatus.failed,
output: { error: 'FFmpeg failed' },
})
})
})
@@ -0,0 +1,313 @@
import type { WorkflowTask } from '@shumai/db'
import { ApplicationFailure } from '@temporalio/workflow'
import {
getActivities,
executeActivity,
TaskQueueDb,
TaskQueueTranscode,
} from '@shumai/workflow-core'
export async function transcodeMedia(task: WorkflowTask): Promise<void> {
const {
updateTaskStatusActivity,
updateAssetStatusActivity,
getAssetActivity,
getMediaInfoActivity,
transcodeVideoActivity,
transcodeImageActivity,
generateSpriteActivity,
updateAssetMediaActivity,
getTranscodeWorkerQueueActivity,
downloadMediaToTmpActivity,
cleanupTmpDirActivity,
} = getActivities()
let tmpDir: string | undefined
let workerQueue = ''
try {
// 1. Update status to processing
await executeActivity(TaskQueueDb, updateTaskStatusActivity, {
taskId: task.id,
status: 'processing',
})
await executeActivity(TaskQueueDb, updateAssetStatusActivity, {
assetId: task.assetId,
status: 'processing',
})
// 2. Get Asset
const asset = await executeActivity(TaskQueueDb, getAssetActivity, task.assetId)
const key = asset?.storageKey?.key
if (!asset || !key) {
throw ApplicationFailure.create({
message: 'Asset not found or has no key',
nonRetryable: true,
})
}
// 3. Get Worker-Specific Queue for Transcode
workerQueue = await executeActivity(TaskQueueTranscode, getTranscodeWorkerQueueActivity)
// 4. Download Media to Tmp
const download = await executeActivity(workerQueue, downloadMediaToTmpActivity, {
assetKey: key,
})
const { filePath } = download
tmpDir = download.tmpDir
// 5. Get Media Info
const spec = task.payload?.transcode || {}
const mediaInfo = await executeActivity(workerQueue, getMediaInfoActivity, {
filePath,
assetId: asset.id,
mediaType: asset.mediaType || '',
})
mediaInfo.original = {
key,
downloadUrl: '',
filesizeInBytes: 0,
codec: '',
}
const mimeType = mediaInfo.mimeType
const metadata = mediaInfo.metadata
// 6. Video Transcoding
if (mimeType.startsWith('video/') && metadata) {
const videoResolutions = getTargetVideoResolutions(
spec.videoStrategy || 'best_match',
metadata.originalWidth,
metadata.originalHeight,
)
for (const res of videoResolutions) {
const [width, height] = resolutionToDimensions(
res,
metadata.originalWidth,
metadata.originalHeight,
)
const videoSpec: PrismaJson.VideoTranscode = {
resolution: res,
width,
height,
}
const videoTranscode = await executeActivity(workerQueue, transcodeVideoActivity, {
assetKey: key,
filePath,
videoSpec,
duration: metadata.duration,
originalFps: metadata.frameRate,
})
if (res === '180p') {
mediaInfo.videoPreview = videoTranscode
} else {
mediaInfo.videoTranscodes.push(videoTranscode)
}
}
// Always keep raw
mediaInfo.videoTranscodes.push({
key: mediaInfo.original?.key,
width: metadata.originalWidth,
height: metadata.originalHeight,
isRaw: true,
})
}
// 7. Image Transcoding
const isImage = mimeType.startsWith('image/')
const isPsd = isMimePsd(mimeType)
if ((isImage || isPsd) && metadata) {
const imageSpec: PrismaJson.ImageTranscode = {
width: metadata.originalWidth,
height: metadata.originalHeight,
quality: 90,
format: 'webp',
}
const imageTranscode = await executeActivity(workerQueue, transcodeImageActivity, {
assetKey: key,
filePath,
imageSpec,
})
mediaInfo.imageTranscodes.push(imageTranscode)
}
// 9. System: Thumbnail
if (spec.thumbnail) {
const thumbTranscode = await executeActivity(workerQueue, transcodeImageActivity, {
assetKey: key,
filePath,
imageSpec: { width: 480, height: 0, quality: 80, format: 'webp' },
})
mediaInfo.thumbnail = thumbTranscode
}
// 10. System: Sprite/Poster (Video only usually)
if (spec.sprite || spec.poster) {
const lastSlashIndex = key.lastIndexOf('/')
const assetDir = lastSlashIndex === -1 ? '' : key.substring(0, lastSlashIndex)
const spriteSpec: PrismaJson.SpriteInfo = {
key: assetDir ? `${assetDir}/sprite.webp` : 'sprite.webp',
frames: 100,
tileX: 10,
tileY: 10,
}
const posterSpec: PrismaJson.PosterInfo = {
key: assetDir ? `${assetDir}/poster.webp` : 'poster.webp',
}
const spriteResult = await executeActivity(workerQueue, generateSpriteActivity, {
assetKey: key,
filePath,
spriteSpec,
posterSpec,
mediaInfo,
})
mediaInfo.sprite = spriteResult.sprite
mediaInfo.poster = spriteResult.poster
}
// 11. Update Asset Media and Status
await executeActivity(TaskQueueDb, updateAssetMediaActivity, {
assetId: asset.id,
mediaInfo,
})
await executeActivity(TaskQueueDb, updateAssetStatusActivity, {
assetId: asset.id,
status: 'processed',
})
// 12. Update Task Status
await executeActivity(TaskQueueDb, updateTaskStatusActivity, {
taskId: task.id,
status: 'completed',
})
} catch (err) {
console.error(`TranscodeMedia failed for task ${task.id}:`, err)
// Update status to failed
await executeActivity(TaskQueueDb, updateTaskStatusActivity, {
taskId: task.id,
status: 'failed',
output: { error: err instanceof Error ? err.message : String(err) },
})
throw err
} finally {
if (tmpDir && workerQueue) {
try {
await executeActivity(workerQueue, cleanupTmpDirActivity, { tmpDir })
} catch (cleanupErr) {
console.error('Failed to cleanup tmp dir:', cleanupErr)
}
}
}
}
function isMimePsd(mimeType: string): boolean {
switch (mimeType) {
case 'image/vnd.adobe.photoshop':
case 'image/x-photoshop':
case 'application/x-photoshop':
case 'image/psd':
return true
}
return false
}
const RESOLUTION_LONG_SIDES: Record<string, number> = {
'2160p': 3840,
'1080p': 1920,
'720p': 1280,
'540p': 960,
'360p': 640,
'180p': 320,
}
function resolutionToDimensions(
resolution: string,
originalWidth: number,
originalHeight: number,
): [number, number] {
const targetLongSide = RESOLUTION_LONG_SIDES[resolution]
if (!targetLongSide) return [0, 0]
let width = 0
let height = 0
if (originalWidth >= originalHeight) {
// Landscape or square: width is the long side
width = targetLongSide
height = Math.round(width * (originalHeight / originalWidth))
} else {
// Portrait: height is the long side
height = targetLongSide
width = Math.round(height * (originalWidth / originalHeight))
}
// Ensure even dimensions for H.264
if (width % 2 !== 0) width++
if (height % 2 !== 0) height++
return [width, height]
}
const TARGET_RESOLUTIONS = [
{ name: '2160p', longSide: 3840 },
{ name: '1080p', longSide: 1920 },
{ name: '720p', longSide: 1280 },
{ name: '540p', longSide: 960 },
{ name: '360p', longSide: 640 },
]
function getBestMatchResolution(
originalWidth: number,
originalHeight: number,
): { name: string; longSide: number } {
const rawLongSide = Math.max(originalWidth, originalHeight)
const lower = TARGET_RESOLUTIONS.filter((r) => r.longSide <= rawLongSide)
if (lower.length > 0) {
lower.sort((a, b) => b.longSide - a.longSide)
return lower[0]
}
return TARGET_RESOLUTIONS[TARGET_RESOLUTIONS.length - 1] // 360p (longSide 640)
}
function getTargetVideoResolutions(
strategy: PrismaJson.VideoTranscodeStrategy,
originalWidth: number,
originalHeight: number,
): string[] {
const resolutions = ['180p']
// Normalize legacy strategies for backward compatibility
let normalizedStrategy: string = strategy
const stratStr = strategy as string
if (stratStr === 'single' || stratStr === 'disable') {
normalizedStrategy = 'best_match'
} else if (stratStr === 'full') {
normalizedStrategy = 'all'
}
const bestMatch = getBestMatchResolution(originalWidth, originalHeight)
if (normalizedStrategy === 'best_match') {
resolutions.push(bestMatch.name)
} else if (normalizedStrategy === 'all') {
const bestMatchIndex = TARGET_RESOLUTIONS.findIndex((r) => r.name === bestMatch.name)
if (bestMatchIndex !== -1) {
for (let i = bestMatchIndex; i < TARGET_RESOLUTIONS.length; i++) {
resolutions.push(TARGET_RESOLUTIONS[i].name)
}
}
}
return resolutions
}
export const transcodeWorkflow = transcodeMedia