Sub topic 9
Controlling downstream tasks with run_if
This chapter assumes you can build a multi-task job with depends_on, as covered in the chapter on task dependencies. It also assumes you know what a task run's status is (succeeded, failed, excluded, and so on).
The default rule
By default, a task runs only when all of its upstream tasks succeed. If ingest fails, transform does not run. Databricks marks transform as "Upstream failed" and skips it.
That default is often right. Sometimes it is not. A cleanup task should run even when the pipeline fails. A alerting task should run only when something fails. The run_if setting changes the rule for one task.
The run_if values
run_if is set on the downstream task. It looks at the outcome of the tasks listed in depends_on and decides whether to run.
| Value | The task runs when |
|---|---|
| `ALL_SUCCESS` | All dependencies succeeded. This is the default. |
| `AT_LEAST_ONE_SUCCESS` | At least one dependency succeeded. |
| `NONE_FAILED` | No dependency failed. Skipped or excluded dependencies are fine. |
| `ALL_DONE` | All dependencies finished, whatever the result. |
| `AT_LEAST_ONE_FAILED` | At least one dependency failed. |
| `ALL_FAILED` | All dependencies failed. |
Example: a failure handler and a cleanup task
The job below runs ingest and transform in order. The alert task runs only if one of them fails. The cleanup task always runs once both have finished.
[
{
"task_key": "ingest",
"notebook_task": {"notebook_path": "/Workspace/Shared/ingest"}
},
{
"task_key": "transform",
"depends_on": [{"task_key": "ingest"}],
"notebook_task": {"notebook_path": "/Workspace/Shared/transform"}
},
{
"task_key": "alert",
"depends_on": [{"task_key": "ingest"}, {"task_key": "transform"}],
"run_if": "AT_LEAST_ONE_FAILED",
"notebook_task": {"notebook_path": "/Workspace/Shared/alert"}
},
{
"task_key": "cleanup",
"depends_on": [{"task_key": "ingest"}, {"task_key": "transform"}],
"run_if": "ALL_DONE",
"notebook_task": {"notebook_path": "/Workspace/Shared/cleanup"}
}
]This fragment shows only the tasks array. Add compute settings to each task before you deploy it.
Common errors
- Expecting a skipped task to count as a failure. If
ingestfails,transformis skipped, not failed.alertstill runs becauseingestfailed. Ifalertdepended only ontransform, it would not run underAT_LEAST_ONE_FAILED, becausetransformnever failed. - Listing too few dependencies.
run_ifonly looks at the tasks independs_on. A task that does not list a failing task cannot react to it. - Using
ALL_DONEand expecting a green run. The job's overall result still reflects the failure.run_ifchanges which tasks run. It does not hide failures. - Confusing
run_ifwith a condition task.run_ifreacts to task status. To branch on data, such as a row count, use an If/else condition task, covered in the next chapter.
Exercise
A task report depends on load_a and load_b, which load two independent sources. You want report to run if at least one source loaded. Which run_if value do you use, and why not the default?
Show answer
Use AT_LEAST_ONE_SUCCESS. The default, ALL_SUCCESS, would skip report whenever either load failed.