Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cancel all runs functionality to Databricks hook #31038

Merged
merged 3 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions airflow/providers/databricks/hooks/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
DELETE_RUN_ENDPOINT = ("POST", "api/2.1/jobs/runs/delete")
REPAIR_RUN_ENDPOINT = ("POST", "api/2.1/jobs/runs/repair")
OUTPUT_RUNS_JOB_ENDPOINT = ("GET", "api/2.1/jobs/runs/get-output")
CANCEL_ALL_RUNS_ENDPOINT = ("POST", "api/2.1/jobs/runs/cancel-all")

INSTALL_LIBS_ENDPOINT = ("POST", "api/2.0/libraries/install")
UNINSTALL_LIBS_ENDPOINT = ("POST", "api/2.0/libraries/uninstall")
Expand Down Expand Up @@ -353,6 +354,15 @@ def cancel_run(self, run_id: int) -> None:
json = {"run_id": run_id}
self._do_api_call(CANCEL_RUN_ENDPOINT, json)

def cancel_all_runs(self, job_id: int) -> None:
"""
Cancels all active runs of a job. The runs are canceled asynchronously.
:param job_id: The canonical identifier of the job to cancel all runs of
"""
json = {"job_id": job_id}
self._do_api_call(CANCEL_ALL_RUNS_ENDPOINT, json)

def delete_run(self, run_id: int) -> None:
"""
Deletes a non-active run.
Expand Down
24 changes: 23 additions & 1 deletion tests/providers/databricks/hooks/test_databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,18 @@ def get_run_output_endpoint(host):

def cancel_run_endpoint(host):
"""
Utility function to generate the get run endpoint given the host.
Utility function to generate the cancel run endpoint given the host.
"""
return f"https://{host}/api/2.1/jobs/runs/cancel"


def cancel_all_runs_endpoint(host):
"""
Utility function to generate the cancel all runs endpoint given the host.
"""
return f"https://{host}/api/2.1/jobs/runs/cancel-all"


def delete_run_endpoint(host):
"""
Utility function to generate delete run endpoint given the host.
Expand Down Expand Up @@ -535,6 +542,21 @@ def test_cancel_run(self, mock_requests):
timeout=self.hook.timeout_seconds,
)

@mock.patch("airflow.providers.databricks.hooks.databricks_base.requests")
def test_cancel_all_runs(self, mock_requests):
mock_requests.post.return_value.json.return_value = {}

self.hook.cancel_all_runs(JOB_ID)

mock_requests.post.assert_called_once_with(
cancel_all_runs_endpoint(HOST),
json={"job_id": JOB_ID},
params=None,
auth=HTTPBasicAuth(LOGIN, PASSWORD),
headers=self.hook.user_agent_header,
timeout=self.hook.timeout_seconds,
)

@mock.patch("airflow.providers.databricks.hooks.databricks_base.requests")
def test_delete_run(self, mock_requests):
mock_requests.post.return_value.json.return_value = {}
Expand Down