chore: commit all changes
This commit is contained in:
54
app/workflows/task-runner.ts
Normal file
54
app/workflows/task-runner.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { sleep } from 'workflow';
|
||||
import { start } from 'workflow/api';
|
||||
import { runTaskProcessor } from '@/lib/server/task-processors';
|
||||
import {
|
||||
claimQueuedTask,
|
||||
completeTask,
|
||||
markTaskFailure
|
||||
} from '@/lib/server/repos/tasks';
|
||||
import type { Task } from '@/lib/types';
|
||||
|
||||
export async function runTaskWorkflow(taskId: string) {
|
||||
'use workflow';
|
||||
|
||||
const task = await claimQueuedTaskStep(taskId);
|
||||
if (!task) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await processTaskStep(task);
|
||||
await completeTaskStep(task.id, result);
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error
|
||||
? error.message
|
||||
: 'Task failed unexpectedly';
|
||||
|
||||
const nextState = await markTaskFailureStep(task.id, reason);
|
||||
|
||||
if (nextState.shouldRetry) {
|
||||
await sleep('1200ms');
|
||||
await start(runTaskWorkflow, [task.id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function claimQueuedTaskStep(taskId: string) {
|
||||
'use step';
|
||||
return await claimQueuedTask(taskId);
|
||||
}
|
||||
|
||||
async function processTaskStep(task: Task) {
|
||||
'use step';
|
||||
return await runTaskProcessor(task);
|
||||
}
|
||||
|
||||
async function completeTaskStep(taskId: string, result: Record<string, unknown>) {
|
||||
'use step';
|
||||
await completeTask(taskId, result);
|
||||
}
|
||||
|
||||
async function markTaskFailureStep(taskId: string, reason: string) {
|
||||
'use step';
|
||||
return await markTaskFailure(taskId, reason);
|
||||
}
|
||||
Reference in New Issue
Block a user