mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 10:36:52 +00:00
* 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
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
import type { WorkflowTask } from '@shumai/db'
|
|
import { executeActivity, getActivities, TaskQueueTranscode } from '@shumai/workflow-core'
|
|
import { ApplicationFailure } from '@temporalio/workflow'
|
|
|
|
export async function getWorkerQueueAndStartTask(task: WorkflowTask): Promise<string> {
|
|
const { getTranscodeWorkerQueueActivity, updateTaskStatusActivity } = getActivities()
|
|
const workerQueue = await executeActivity(TaskQueueTranscode, getTranscodeWorkerQueueActivity)
|
|
await executeActivity(workerQueue, updateTaskStatusActivity, {
|
|
taskId: task.id,
|
|
status: 'processing',
|
|
})
|
|
return workerQueue
|
|
}
|
|
|
|
export async function fetchAssetWithKey(workerQueue: string, assetId: string) {
|
|
const { getAssetActivity } = getActivities()
|
|
const asset = await executeActivity(workerQueue, getAssetActivity, assetId)
|
|
const key = asset?.storageKey?.key
|
|
if (!asset || !key || asset.status === 'pending_purge') {
|
|
throw ApplicationFailure.create({
|
|
message: 'Asset not found, has no key, or is being purged',
|
|
nonRetryable: true,
|
|
})
|
|
}
|
|
return { asset, key }
|
|
}
|
|
|
|
export async function completeTask(
|
|
workerQueue: string,
|
|
taskId: string,
|
|
output?: Record<string, unknown>,
|
|
) {
|
|
const { updateTaskStatusActivity } = getActivities()
|
|
await executeActivity(workerQueue, updateTaskStatusActivity, {
|
|
taskId,
|
|
status: 'completed',
|
|
...(output ? { output } : {}),
|
|
})
|
|
}
|
|
|
|
export async function failTask(workerQueue: string, taskId: string, err: unknown) {
|
|
const { updateTaskStatusActivity } = getActivities()
|
|
if (workerQueue) {
|
|
await executeActivity(workerQueue, updateTaskStatusActivity, {
|
|
taskId,
|
|
status: 'failed',
|
|
output: { error: err instanceof Error ? err.message : String(err) },
|
|
})
|
|
}
|
|
}
|
|
|
|
export async function cleanupTmpDir(workerQueue: string, tmpDir: string | undefined) {
|
|
if (tmpDir && workerQueue) {
|
|
const { cleanupTmpDirActivity } = getActivities()
|
|
try {
|
|
await executeActivity(workerQueue, cleanupTmpDirActivity, { tmpDir })
|
|
} catch (cleanupErr) {
|
|
console.error('Failed to cleanup tmp dir:', cleanupErr)
|
|
}
|
|
}
|
|
}
|