The task run logic. It is similar to the run
function in TaskOptions but it
returns the output and children tasks to be run in parallel after the run function completes.
Optional
finalize?: Function or task to run after the runParent function and children tasks finish. This is useful for combining the output of the run function and children tasks. It is called even if the children tasks fail.
If it is a function, it is called without any durability guarantees and retries. It should not be a long running function.
const extractFileTitle = executor
.inputSchema(Schema.Struct({ filePath: Schema.String }))
.task({
id: 'extractFileTitle',
timeoutMs: 30_000, // 30 seconds
run: async (ctx, input) => {
// ... extract the file title
return {
title: 'File Title',
}
},
})
const summarizeFile = executor
.validateInput(async (input: { filePath: string }) => {
// Example validation function - implement your own validation logic
if (!isValidFilePath(input.filePath)) {
throw new Error('Invalid file path')
}
return {
filePath: input.filePath,
}
})
.task({
id: 'summarizeFile',
timeoutMs: 30_000, // 30 seconds
run: async (ctx, input) => {
// ... summarize the file
return {
summary: 'File summary',
}
},
})
const uploadFile = executor
.inputSchema(Schema.Struct({ filePath: Schema.String, uploadUrl: Schema.String }))
.parentTask({
id: 'uploadFile',
timeoutMs: 60_000, // 1 minute
runParent: async (ctx, input) => {
// ... upload file to the given uploadUrl
// Extract the file title and summarize the file in parallel
return {
output: {
filePath: input.filePath,
uploadUrl: input.uploadUrl,
fileSize: 100,
},
children: [
childTask(extractFileTitle, { filePath: input.filePath }),
childTask(summarizeFile, { filePath: input.filePath }),
],
}
},
finalize: {
id: 'uploadFileFinalize',
timeoutMs: 60_000, // 1 minute
run: async (ctx, { output, children }) => {
// ... combine the output of the run function and children tasks
return {
filePath: output.filePath,
uploadUrl: output.uploadUrl,
fileSize: 100,
title: 'File Title',
summary: 'File summary',
}
}
},
})
Options for a parent task that can be run using a durable executor. It is similar to TaskOptions but it returns children tasks to be run in parallel after the run function completes, along with the output of the parent task.
The
runParent
function is similar to therun
function in TaskOptions, but the output is of the form{ output: TRunOutput, children: ReadonlyArray<ChildTask> }
where the children are the tasks to be run in parallel after the run function completes.The
finalize
function or task is run after the runParent function and all the children tasks finish. It is useful for combining the output of the runParent function and children tasks. It is called even if the children tasks fail. Its input has the following properties:output
: The output of the runParent functionchildren
: The finished children task executions (includes both successful and failed children)Important: The
finalize
function or task receives outputs from ALL children, including those that have failed. This behaves similar toPromise.allSettled()
- you get the results regardless of individual child success or failure. This allows you to implement custom error handling logic.If
finalize
is provided, the output of the whole task is the output of thefinalize
function or task. If it is not provided, the output of the whole task is of the form{ output: TRunOutput, children: ReadonlyArray<FinishedChildTaskExecution> }
.See the task examples section for more details on creating tasks.