Running Child Workflows
Hatchet supports running child workflows from within a parent workflow. This allows you to create complex, dynamic workflows that don't map to the concept of a DAG.
To run a child workflow, you can use the context.spawnWorkflow
method. For example:
child-workflow.ts
const parentWorkflow: Workflow = {
id: "parent-workflow",
description: "Example workflow for spawning child workflows",
on: {
event: "fanout:create",
},
steps: [
{
name: "parent-spawn",
timeout: "10s",
run: async (ctx) => {
const promises: Promise<string>[] = [];
for (let i = 0; i < 5; i++) {
promises.push(
ctx
.spawnWorkflow("child-workflow", {
input: `child-input-${i}`,
})
.result(),
);
}
const results = await Promise.all(promises);
return {
results,
};
},
},
],
};
Usage
Type Parameters
Q
: The type of the input data for the workflow. Default isJsonValue
.P
: The type of the output data from the workflow. Default isJsonValue
.
Parameters
workflowName
(string
): The name of the workflow to be spawned. This will be concatenated with the client's namespace to form the full workflow name.input
(Q
): The input data for the workflow. The type of this data is specified by the generic type parameterQ
.key
(string
, optional): A caching key for the child workflow. If this is not set, the child workflow will be cached on the index that it was triggered at. The cache is used on retries of the parent workflow so that child workflows which were already triggered are skipped.
Returns
WorkflowRunRef
: A reference to the workflow run, with the output data type specified by the generic type parameterP
.
Exceptions
HatchetError
: Thrown if there is any error during the workflow spawning process, with the error message provided.