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. You should also know what a task run's result state looks like (succeeded, failed, excluded, canceled). Basic JSON reading is enough for the examples.
The default rule and why it is not enough
By default, a task runs only when all of its upstream tasks succeeded. If ingest fails, transform does not start, and publish does not start either. That is the right behavior for a linear pipeline, but it is wrong in several common cases:
- A cleanup task must run even when the pipeline fails, to drop temporary tables or release a lock.
- An alert task should run only when something failed, not on every success.
- A task merges results from several parallel branches and should run when at least one branch succeeded.
The run_if field on a task changes the rule. It does not change the graph. The dependencies in depends_on still decide the order. run_if decides which upstream outcomes allow the task to start.
The run_if values
A task can set run_if to one of these values.
| Value | The task runs when |
|---|---|
| `ALL_SUCCESS` | All upstream tasks succeeded. This is the default. |
| `AT_LEAST_ONE_SUCCESS` | At least one upstream task succeeded. |
| `NONE_FAILED` | No upstream task failed. Upstream tasks that were excluded or skipped do not count as failures. |
| `ALL_DONE` | All upstream tasks finished, whatever the result. |
| `AT_LEAST_ONE_FAILED` | At least one upstream task failed. |
| `ALL_FAILED` | All upstream tasks failed. |
Check the exact set of values against the current Jobs API reference for your workspace, because the list has grown over time.
Two points are easy to miss:
run_iflooks only at the direct upstream tasks listed independs_on. It does not look at tasks further up the graph.- A task with no
depends_onhas no upstream tasks, sorun_ifhas nothing to evaluate. Use it only on tasks that depend on something.
A worked example: cleanup and alerting
The following job has a main chain and two tasks that react to its outcome. The cleanup task runs after transform no matter what happened. The alert task runs only if transform failed.
{
"name": "orders-with-cleanup",
"max_concurrent_runs": 1,
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 2
}
}
],
"tasks": [
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/ingest"}
},
{
"task_key": "transform",
"depends_on": [{"task_key": "ingest"}],
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/transform"}
},
{
"task_key": "cleanup",
"depends_on": [{"task_key": "transform"}],
"run_if": "ALL_DONE",
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/cleanup"}
},
{
"task_key": "alert",
"depends_on": [{"task_key": "transform"}],
"run_if": "AT_LEAST_ONE_FAILED",
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/alert"}
}
]
}Walk through both outcomes:
- If
transformsucceeds,cleanupruns (ALL_DONEis satisfied) andalertis excluded, because its condition is not met. - If
transformfails, bothcleanupandalertrun.
The NONE_FAILED value is the usual choice for a step that follows optional work. It treats an excluded upstream as acceptable, which ALL_SUCCESS does not.
What happens to a task whose condition is not met
When the run_if condition is not met, the task does not fail. It ends in an excluded state, shown as "Excluded" in the run view. This matters for the next task in the chain.
Consider a task report that depends on alert with the default ALL_SUCCESS. When alert is excluded, report is also excluded, because its only upstream did not succeed. Exclusion propagates down the graph unless a downstream task uses a rule that tolerates it, such as NONE_FAILED.
Excluded tasks do not make the run fail. A run is judged by its tasks that actually executed. In the example above, a successful transform leaves alert excluded, and the run still ends as succeeded.
Fan-in with parallel branches
run_if is most useful where several branches meet. Suppose load_eu and load_us run in parallel and a consolidate task follows both.
[
{
"task_key": "consolidate",
"depends_on": [
{"task_key": "load_eu"},
{"task_key": "load_us"}
],
"run_if": "AT_LEAST_ONE_SUCCESS",
"notebook_task": {"notebook_path": "/Workspace/Shared/consolidate"}
}
]With the default, a failure in either region blocks consolidate. With AT_LEAST_ONE_SUCCESS, it runs as long as one region loaded. Your notebook must then handle the missing region, for example by reading a task value or checking the target table, or the output will silently be incomplete.
run_if compared with condition tasks
Both features control whether a task runs, but they look at different things.
| Question | `run_if` | If/else condition task |
|---|---|---|
| What does it evaluate? | The outcome states of upstream tasks. | A comparison of values you supply, such as a task value or a parameter. |
| Where is it set? | On the downstream task itself. | In a separate task that downstream tasks depend on. |
| Typical use | Cleanup, alerting, tolerating partial failure. | Skip work when there is no data, or choose a path by a parameter. |
Use run_if when the question is "did the upstream tasks succeed or fail?" Use a condition task when the question is about data, such as "did ingest produce any rows?" The next chapter covers condition tasks.
Common mistakes
- Expecting
run_ifto reach past direct upstream tasks. Ifcleanupshould run wheningestfails, it must listingestindepends_on, or depend on a task that itself runs under a tolerant rule. - Using
ALL_DONEfor a task that needs good data.ALL_DONEruns after failures. A publish step with this rule can publish stale or partial data. - Forgetting that a retried task is still running. Retries happen before the task reaches its final state, so
run_ifsees only the final result after retries are used up. Configure retries separately. - Treating "Excluded" as an error. It is the normal result for a branch that did not apply.
- Assuming a canceled run triggers cleanup. If you cancel a whole run manually, tasks that have not started are not guaranteed to run. Test cancellation behavior if your cleanup is critical, and also make cleanup idempotent so a later run can recover.
Checking behavior
Before relying on a rule in production, force each path once:
- Run the job with the main task succeeding and confirm which downstream tasks are excluded.
- Make the upstream task fail on purpose, for example by raising an exception in the notebook, and confirm the failure-path tasks run.
- Open the run's task graph and read the state of each task, not just the overall run state.
Exercise
A job has tasks extract_a and extract_b running in parallel, then merge, then notify_failure. You want merge to run if either extract succeeded, and notify_failure to run only if merge fails. Which run_if values do you set on merge and notify_failure?
Show answer
Set run_if to AT_LEAST_ONE_SUCCESS on merge, with both extract tasks in its depends_on. Set run_if to AT_LEAST_ONE_FAILED on notify_failure, with merge in its depends_on. If merge succeeds, notify_failure is excluded and the run still succeeds.
Exercise
A task report depends only on alert, uses the default run_if, and alert was excluded because nothing failed. What state does report end in, and which value would let report run anyway?
Show answer
report is excluded, because ALL_SUCCESS requires its upstream task to succeed and an excluded task did not. Setting run_if to NONE_FAILED lets it run, since an excluded upstream is not a failure. ALL_DONE would also run it.