-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Support remote workspace for RunJobTask (#140)
* run job * task type override * patch * codegen tests * tests * docs * todo * add tests --------- Co-authored-by: Maxim Mityutko <[email protected]>
- Loading branch information
1 parent
d579492
commit 6141b5c
Showing
15 changed files
with
320 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import Union | ||
from pydantic import SecretStr | ||
|
||
from databricks.sdk import WorkspaceClient | ||
from brickflow.context import ctx | ||
from brickflow.engine.utils import get_job_id | ||
|
||
|
||
class RunJobInRemoteWorkspace: | ||
""" | ||
Currently Databricks does not natively support running a job in a remote workspace via the RunJobTask. | ||
This plugin adds this functionality. However, it aims to be a temporary solution until Databricks adds this | ||
functionality natively. | ||
The plugin does not support neither passing the parameters to the remote job, nor waiting for the job to finish. | ||
Examples | ||
-------- | ||
service_principle_pat = ctx.dbutils.secrets.get("scope", "service_principle_id") | ||
WorkflowDependencySensor( | ||
databricks_host=https://your_workspace_url.cloud.databricks.com, | ||
databricks_token=service_principle_pat, | ||
job_name="foo", | ||
) | ||
In the above snippet Databricks secrets are used as a secure service to store the databricks token. | ||
If you get your token from another secret management service, like AWS Secrets Manager, GCP Secret Manager | ||
or Azure Key Vault, just pass it in the databricks_token argument. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
databricks_host: str, | ||
databricks_token: Union[str, SecretStr], | ||
job_name: str, | ||
): | ||
self.databricks_host = databricks_host | ||
self.databricks_token = ( | ||
databricks_token | ||
if isinstance(databricks_token, SecretStr) | ||
else SecretStr(databricks_token) | ||
) | ||
self.job_name = job_name | ||
self._workspace_obj = WorkspaceClient( | ||
host=self.databricks_host, token=self.databricks_token.get_secret_value() | ||
) | ||
|
||
def execute(self): | ||
job_id = get_job_id( | ||
host=self.databricks_host, | ||
token=self.databricks_token, | ||
job_name=self.job_name, | ||
) | ||
# TODO: add support for passing parameters to the remote job | ||
# TODO: wait for the job to finish | ||
run = self._workspace_obj.jobs.run_now(job_id) | ||
ctx.log.info("Job run status: %s", run.response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import re | ||
|
||
import pytest | ||
from requests_mock.mocker import Mocker as RequestsMocker | ||
|
||
from brickflow.engine.utils import ctx | ||
from brickflow_plugins.databricks.run_job import RunJobInRemoteWorkspace | ||
|
||
|
||
class TestRunJob: | ||
workspace_url = "https://42.cloud.databricks.com" | ||
endpoint_url = f"{workspace_url}/api/.*/jobs/run-now" | ||
response = {"run_id": 37, "number_in_job": 42} | ||
|
||
ctx.log.propagate = True | ||
|
||
@pytest.fixture(autouse=True) | ||
def mock_get_job_id(self, mocker): | ||
mocker.patch( | ||
"brickflow_plugins.databricks.run_job.get_job_id", | ||
return_value=1, | ||
) | ||
|
||
@pytest.fixture(autouse=True, name="api") | ||
def mock_api(self): | ||
rm = RequestsMocker() | ||
rm.post(re.compile(self.endpoint_url), json=self.response, status_code=int(200)) | ||
yield rm | ||
|
||
def test_run_job(self, api, caplog): | ||
with api: | ||
RunJobInRemoteWorkspace( | ||
databricks_host=self.workspace_url, | ||
databricks_token="token", | ||
job_name="foo", | ||
).execute() | ||
|
||
assert "RunNowResponse(number_in_job=42, run_id=37)" in caplog.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.