Retrieve job hierarchies with all children and grandchildren
In some situations, you need to get a job and all of its children, grandchildren, and so on. The getFlow method allows you to retrieve the entire job hierarchy.
The getFlow method accepts a NodeOpts interface with the following properties:
interface NodeOpts { /** * Root job queue name. */ queueName: string; /** * Prefix included in job key. */ prefix?: string; /** * Root job id. */ id: string; /** * Maximum depth or levels to visit in the tree. */ depth?: number; /** * Maximum quantity of children per type (processed, unprocessed). */ maxChildren?: number;}
You may need to limit the information retrieved if you have many children for one of the job nodes:
const limitedTree = await flow.getFlow({ id: topJob.id, queueName: 'topQueueName', depth: 1, // get only the first level of children maxChildren: 2, // get only 2 children per node});const { children, job } = limitedTree;
Retrieve the entire flow tree to monitor the progress of a complex workflow:
const tree = await flow.getFlow({ id: rootJobId, queueName: 'workflow',});function printTree(node: JobNode, level = 0) { const indent = ' '.repeat(level); console.log(`${indent}${node.job.name}: ${node.job.getState()}`); if (node.children) { for (const child of node.children) { printTree(child, level + 1); } }}printTree(tree);
Debugging Failed Flows
Get the complete flow tree to identify which jobs failed:
const tree = await flow.getFlow({ id: failedJobId, queueName: 'processing',});async function findFailedJobs(node: JobNode): Promise<Job[]> { const failed: Job[] = []; const state = await node.job.getState(); if (state === 'failed') { failed.push(node.job); } if (node.children) { for (const child of node.children) { const childFailed = await findFailedJobs(child); failed.push(...childFailed); } } return failed;}const failedJobs = await findFailedJobs(tree);
Paginating Large Trees
Use depth and maxChildren to paginate through large job hierarchies:
// Get only the first level with 10 childrenconst firstLevel = await flow.getFlow({ id: rootJobId, queueName: 'large-workflow', depth: 1, maxChildren: 10,});// Then get deeper levels for specific branchesif (firstLevel.children) { for (const child of firstLevel.children) { const subTree = await flow.getFlow({ id: child.job.id, queueName: child.job.queueName, depth: 2, maxChildren: 10, }); }}