RiyazAliM
Honored Contributor

hi @minhhung0507 

Please check if the below code using rest api is able to cater to your needs.

 

list_runs_url = f"{DATABRICKS_INSTANCE}/api/2.1/jobs/runs/list"
params = {
"job_id": JOB_ID,
"limit": 1,
"active_only": False
}
response = requests.get(list_runs_url, headers=headers, params=params)
latest_run_id = response.json()["runs"][0]["run_id"]
 
run_details_url = f"{DATABRICKS_INSTANCE}/api/2.1/jobs/runs/get"
response = requests.get(run_details_url, headers=headers, params={"run_id": latest_run_id})
tasks = response.json().get("tasks", [])
 
failed_tasks = [task["task_key"] for task in tasks if task["state"]["result_state"] == "FAILED"]
print(failed_tasks)
 
if not failed_tasks:
print("No failed tasks found.")
else:
print(f"Retrying failed tasks: {failed_tasks}")

# Submit new run for each failed task (assuming same notebook and cluster setup)
for task in tasks:
if task["task_key"] in failed_tasks:
submit_url = f"{DATABRICKS_INSTANCE}/api/2.1/jobs/runs/submit"
payload = {
"run_name": f"Rerun failed task: {task['task_key']}",
"tasks": [
{
"task_key": task["task_key"],
"notebook_task": task["notebook_task"],
"existing_cluster_id": task["existing_cluster_id"]
}
]
}
submit_resp = requests.post(submit_url, headers=headers, data=json.dumps(payload))
print(f"Submitted rerun for task {task['task_key']}: {submit_resp.status_code} - {submit_resp.text}")
 
Riz