burakyilmaz321 commented on code in PR #23712: URL: https://github.com/apache/airflow/pull/23712#discussion_r875320949
########## airflow/providers/databricks/sensors/databricks.py: ########## @@ -0,0 +1,103 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Databricks sensors""" + +from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence, Union + +from airflow.exceptions import AirflowException +from airflow.providers.databricks.hooks.databricks import DatabricksHook +from airflow.sensors.base import BaseSensorOperator +from airflow.utils.context import Context + +if TYPE_CHECKING: + from airflow.sensors.base import PokeReturnValue + + +class DatabricksJobRunSensor(BaseSensorOperator): + """ + Check for the state of a submitted Databricks job run or specific task of a job run. + + :param run_id: Id of the submitted Databricks job run or specific task of a job run. (templated) + :param databricks_conn_id: Reference to the :ref:`Databricks connection <howto/connection:databricks>`. + By default and in the common case this will be ``databricks_default``. To use + token based authentication, provide the key ``token`` in the extra field for the + connection and create the key ``host`` and leave the ``host`` field empty. + :param retry_limit: Amount of times retry if the Databricks backend is + unreachable. Its value must be greater than or equal to 1. + :param retry_delay_seconds: Number of seconds to wait between retries (it + might be a floating point number). + :param databricks_retry_args: An optional dictionary with arguments passed to ``tenacity.Retrying`` class. + """ + + template_fields: Sequence[str] = ('run_id',) Review Comment: I think this could be useful when we submit a multi-task job and listen each task separately from Airflow. I have a use case like this: I'm submitting multiple tasks to a single job cluster. I want to track each task's status from Airflow. I'm using this as following: ```python # Proof of concept dag that submits multiple tasks to a single # job cluster and watches every task's status separately @dag(schedule_interval=None, start_date=datetime(2021, 1, 1)) def dbx_poc(): tasks_keys = ["t1", "t2"] @task def submit_job_run_and_get_tasks(job_id: int) -> Dict[str, str]: hook = DatabricksHook() run_id = hook.run_now({"job_id": job_id}) response = hook._do_api_call(GET_RUN_ENDPOINT, {"run_id": run_id}) tasks = {task["task_key"]: task["run_id"] for task in response["tasks"]} return tasks tasks = submit_job_run_and_get_tasks(job_id=1234) for task_key in tasks_keys: tasks >> DatabricksJobRunSensor(task_id=f"wait_{task_key}", run_id=tasks[task_key]) dag = dbx_poc() ``` Basically, I'm submitting a multi-task job, get the run ids for each task and push them to XCOM, and create multiple sensors for each task using the templated run_id field. Does that make sense? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
