Sub topic 16
Composing workflows with Run Job tasks
A Run Job task is a task whose work is to start another job and wait for it to finish. It lets you build a large workflow from smaller jobs that you can develop, schedule, and monitor on their own. This chapter assumes you know how to define tasks and dependencies, pass job parameters, and configure retries. Those topics are covered in earlier chapters.
Why compose jobs
A single job with dozens of tasks gets hard to read and hard to change. Composition solves three problems:
- Reuse. A "refresh dimension tables" job can be called from several pipelines without copying its tasks.
- Ownership. Different teams can own different child jobs, each with its own permissions, compute, and notifications.
- Isolation. A child job has its own run history, so you can inspect or repair it without opening a very large parent graph.
Composition has a cost. Each child run is a separate run with separate compute startup, and a failure now has two places to look. If the parts will never be used separately, plain tasks in one job are simpler.
How a Run Job task behaves
The parent job holds a task of type run_job_task. It points at a child job by its job ID and can pass job parameters. When the task starts, Databricks launches a new run of the child job. The task stays in the running state until the child run ends. The task's result then matches the child's result: success if the child succeeded, failure if it failed.
Several consequences follow from this:
- The child has its own compute. The Run Job task needs no cluster. The child's tasks use the compute defined in the child job.
- The child ignores its own schedule for this run. The child's cron schedule or trigger is irrelevant to a run started by a parent. The schedule still fires independently if it is unpaused, so pause it if the child should only run when called.
- The child's concurrency limit still applies. If the child has
max_concurrent_runsset to 1 and another run is active, the parent's launch is affected. Check the child's limit when several parents may call it. - Dependencies work as usual. Downstream tasks in the parent use
depends_on,run_if, and condition tasks exactly as they do for notebook tasks.
A parent job definition
The following Jobs API 2.1 definition calls two existing jobs and then runs a notebook. Replace the job IDs with IDs from your workspace. The final task uses a job cluster, so the parent defines compute only for the work it does itself.
{
"name": "nightly-orchestrator",
"max_concurrent_runs": 1,
"schedule": {
"quartz_cron_expression": "0 0 2 * * ?",
"timezone_id": "UTC",
"pause_status": "UNPAUSED"
},
"job_clusters": [
{
"job_cluster_key": "small_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 1
}
}
],
"tasks": [
{
"task_key": "ingest_all",
"run_job_task": {
"job_id": 111111111111,
"job_parameters": {"run_date": "{{job.start_time.iso_date}}"}
}
},
{
"task_key": "refresh_models",
"depends_on": [{"task_key": "ingest_all"}],
"run_job_task": {
"job_id": 222222222222,
"job_parameters": {"run_date": "{{job.start_time.iso_date}}"}
}
},
{
"task_key": "publish",
"depends_on": [{"task_key": "refresh_models"}],
"job_cluster_key": "small_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/publish"}
}
]
}The job_parameters object sets values for the child job's parameters. The child must declare a parameter with the same name, such as run_date. A key that the child does not declare has nothing to bind to, so check the names on both sides. Dynamic value references such as {{job.start_time.iso_date}} are resolved in the parent before the value is handed to the child.
Passing data between parent and child
Parameters flow down from parent to child. Sending results back up is harder. Do not assume that task values set inside the child are readable by parent tasks the way sibling task values are. A reliable pattern is to write the result to a Delta table or a known path. The parent's downstream tasks then read it from there. If you need a count or a status flag, a small control table keyed by run date works well.
Retries, timeouts, and failure
Retry settings on a Run Job task apply to the task as a whole. If the child fails and the task retries, Databricks starts a new child run from the beginning. Consider this before setting max_retries on a task that wraps a long, expensive child. Retrying at the parent repeats work that the child's own tasks, with their own retries, may already have attempted.
A sound split is:
- Put retries on the individual tasks inside the child, where a transient error can be fixed cheaply.
- Leave the Run Job task with zero or one retry, or none.
- Use a timeout on the Run Job task as a safety limit for the whole child run.
Cancelling the parent run cancels the child runs it started. A failed child makes the Run Job task fail, which marks downstream tasks as upstream failed unless a run_if rule says otherwise.
Repair works at the level of the parent's tasks. Repairing a failed Run Job task starts a fresh run of the child. It does not resume the child at its failed task. If you want to rerun only the failed tasks inside the child, repair the child run from its own run page.
Permissions and the run identity
The parent's run-as identity must be allowed to run the child job. Without that permission, the Run Job task fails when it launches the child. Grant the identity at least the permission to run the job. Also check that the identity has access to the data and compute the child needs, since those are governed by the child job's own settings.
Limits and gotchas
- No cycles. A job cannot call itself, directly or through other jobs. Databricks rejects circular dependencies.
- Nesting depth is limited. Keep hierarchies shallow, usually one or two levels. Deep trees are hard to debug, and the platform sets a maximum depth that you should look up in the current documentation.
- Job IDs are workspace-specific. A hard-coded ID breaks when you deploy to another workspace. In Databricks Asset Bundles, reference the other job by its resource key so that the ID is filled in at deploy time.
- Parallel calls to one child. If two parent tasks start the same child at once, the child's
max_concurrent_runsdecides whether both can run. - Cost visibility. Each child run has its own compute startup, and a deep hierarchy of small jobs spends more on cluster startup than a single job that reuses one job cluster.
When to choose a Run Job task
| Situation | Better choice |
|---|---|
| Several pipelines share the same preparation steps | Run Job task calling one shared job |
| Different teams own different stages | Run Job task with separate permissions per child |
| Steps always run together and share one cluster | Plain tasks in one job |
| The same logic runs once per input value | For each task |
Exercise
A parent job has a Run Job task with max_retries set to 3. The child job it calls takes two hours and has already retried its own failing task twice. What happens if the child still fails, and what change would you consider?
Show answer
The Run Job task fails, and Databricks retries it by starting a new child run from the beginning, up to three more times. That could repeat up to two hours of work each time, and the child's own task retries run again inside each new child run. Consider setting the Run Job task's retries to zero or one, relying on the retries inside the child for transient errors, and adding a timeout on the Run Job task as a limit on total runtime.
Exercise
You deploy a parent job to a staging workspace and its Run Job task fails to start, although it worked in development. Name two likely causes.
Show answer
First, the job_id in the definition is the development workspace's ID, which does not exist in staging. Referencing the child by its bundle resource key avoids this. Second, the parent's run-as identity may lack permission to run the child job in staging.