Make parent jobs fail immediately when specific children fail
In certain workflows, you may need a parent job to fail immediately if any of its child jobs fail. The failParentOnFailure option allows you to achieve this behavior. When set to true on a child job, it ensures that if the child fails, its parent job is also marked as failed.
The failure can cascade through multiple levels of the hierarchy:
const flow = new FlowProducer({ connection });const originalTree = await flow.add({ name: 'root-job', queueName: 'topQueueName', data: {}, children: [ { name: 'child-job', data: { idx: 0, foo: 'bar' }, queueName: 'childrenQueueName', // This child will fail its parent if it fails opts: { failParentOnFailure: true }, children: [ { name: 'grandchild-job-1', data: { idx: 1, foo: 'bah' }, queueName: 'grandChildrenQueueName', // This grandchild will fail its parent if it fails opts: { failParentOnFailure: true }, }, { name: 'grandchild-job-2', data: { idx: 2, foo: 'baz' }, queueName: 'grandChildrenQueueName', // No failParentOnFailure; its failure won't affect the parent }, ], }, { name: 'child-job-2', data: { idx: 3, foo: 'foo' }, queueName: 'childrenQueueName', // No failParentOnFailure; its failure won't affect the parent }, ],});
As soon as a child with this option fails, the parent job will be marked as failed lazily. A worker must process the parent job before it transitions to the failed state. The failure will result in an UnrecoverableError with the message “child failed”.
failParentOnFailure takes precedence over other failure handling options like removeDependencyOnFailure and ignoreDependencyOnFailure. If a child has failParentOnFailure: true, the parent will fail regardless of other options.
You can combine different strategies for different children: