2026-07-19 12:32:08 +08:00
|
|
|
import type { WorkflowTask } from '@shumai/db'
|
2026-10-19 15:35:08 +08:00
|
|
|
import {
|
|
|
|
|
ApplicationFailure,
|
|
|
|
|
getAssetActivity,
|
|
|
|
|
updateAssetStatusActivity,
|
|
|
|
|
updateTaskStatusActivity,
|
|
|
|
|
} from '@shumai/workflow-core'
|
|
|
|
|
import { cleanupTmpDirActivity } from '../activities/transcode'
|
2026-07-19 12:32:08 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
export async function startTask(task: WorkflowTask): Promise<void> {
|
|
|
|
|
await updateTaskStatusActivity({
|
2026-07-19 12:32:08 +08:00
|
|
|
taskId: task.id,
|
|
|
|
|
status: 'processing',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
/**
|
|
|
|
|
* @deprecated Use `startTask(task)` instead.
|
|
|
|
|
*/
|
|
|
|
|
export async function getWorkerQueueAndStartTask(task: WorkflowTask): Promise<string> {
|
|
|
|
|
await startTask(task)
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchAssetWithKey(
|
|
|
|
|
assetId: string,
|
|
|
|
|
): Promise<{ asset: NonNullable<Awaited<ReturnType<typeof getAssetActivity>>>; key: string }> {
|
|
|
|
|
const asset = await getAssetActivity(assetId)
|
2026-07-19 12:32:08 +08:00
|
|
|
const key = asset?.storageKey?.key
|
2026-09-19 10:40:49 +08:00
|
|
|
if (!asset || !key || asset.status === 'pending_purge') {
|
2026-07-19 12:32:08 +08:00
|
|
|
throw ApplicationFailure.create({
|
2026-09-19 10:40:49 +08:00
|
|
|
message: 'Asset not found, has no key, or is being purged',
|
2026-07-19 12:32:08 +08:00
|
|
|
nonRetryable: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return { asset, key }
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
export async function completeTask(taskId: string, output?: Record<string, unknown>) {
|
|
|
|
|
await updateTaskStatusActivity({
|
2026-07-19 12:32:08 +08:00
|
|
|
taskId,
|
|
|
|
|
status: 'completed',
|
|
|
|
|
...(output ? { output } : {}),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
export async function failTask(taskId: string, err: unknown, assetId?: string) {
|
|
|
|
|
const errorMsg = err instanceof Error ? err.message : String(err)
|
|
|
|
|
await updateTaskStatusActivity({
|
|
|
|
|
taskId,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
output: { error: errorMsg },
|
|
|
|
|
})
|
|
|
|
|
if (assetId) {
|
|
|
|
|
try {
|
|
|
|
|
await updateAssetStatusActivity({
|
|
|
|
|
assetId,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
error: errorMsg,
|
|
|
|
|
})
|
|
|
|
|
} catch (assetErr) {
|
|
|
|
|
console.error('Failed to update asset status to failed:', assetErr)
|
2026-10-02 20:09:32 +08:00
|
|
|
}
|
2026-07-19 12:32:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
export async function cleanupTmpDir(tmpDir: string | undefined) {
|
|
|
|
|
if (tmpDir) {
|
2026-07-19 12:32:08 +08:00
|
|
|
try {
|
2026-10-19 15:35:08 +08:00
|
|
|
await cleanupTmpDirActivity({ tmpDir })
|
2026-07-19 12:32:08 +08:00
|
|
|
} catch (cleanupErr) {
|
|
|
|
|
console.error('Failed to cleanup tmp dir:', cleanupErr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|