Workflows Overview
Workflows are tasks that enable you to chain and reuse other tasks to build comprehensive APIs. They provide a flexible way to orchestrate complex processes by combining smaller, modular tasks.
The key addition on TaskExecutor interface is the Flow object which is passed as a third param to processTask method. The
Flow object empowers you to:
- Execute other tasks: Trigger nested tasks within the workflow.
- Deliver intermediate results: Send partial results to the client before the entire task execution is complete
- Support flexible configurations: Easily create and integrate different configurations for a workflow.
Invoking Other Tasks
Below is an example of how to use the Flow object to invoke a nested task within a TaskExecutor implementation:
@Executor("example-task")
export class ExampleExecutor implements TaskExecutor {
async processTask(command: any, metadata: TaskMetadata, flow: Flow): Promise<ExampleResponse> {
const {result} = await flow.executeTask("example-nested-task", {
name: "Joe"
});
return {
result
}
}
}
Publishing Progress
@Executor("example-task")
export class ExampleExecutor implements TaskExecutor {
async processTask(command: any, metadata: TaskMetadata, flow: Flow): Promise<ExampleResponse> {
const {result} = await flow.publishProgress({
name: "Joe"
});
sleep(5000);
return {
name: 'Joe',
surname: `Doe`
}
}
}
Versioned Workflows
Workflows and task executors can now support versioning, allowing you to run different versions of a workflow side by side (e.g., for upgrades, experiments, or backward compatibility). Versioning applies to both the API endpoint and the executor registration in code.
Registering a Versioned Task
You can define a versioned or multi-part task by including the version (or path segments) in the @Executor() decorator:
@Executor("v2/example-task")
export class ExampleTaskV2Executor implements TaskExecutor {
async processTask(command: any, metadata: TaskMetadata, flow: Flow): Promise<any> {
return { result: "This is version 2!" };
}
}