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

Consolidate hook management in AnalyticDBSparkSensor #34435

Merged
merged 2 commits into from
Sep 18, 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
17 changes: 10 additions & 7 deletions airflow/providers/alibaba/cloud/sensors/analyticdb_spark.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from functools import cached_property
from typing import TYPE_CHECKING, Any, Sequence

from deprecated.classic import deprecated

from airflow.exceptions import AirflowProviderDeprecationWarning
from airflow.providers.alibaba.cloud.hooks.analyticdb_spark import AnalyticDBSparkHook, AppState
from airflow.sensors.base import BaseSensorOperator

Expand Down Expand Up @@ -50,19 +53,19 @@ def __init__(
self.app_id = app_id
self._region = region
self._adb_spark_conn_id = adb_spark_conn_id
self._adb_spark_hook: AnalyticDBSparkHook | None = None

@cached_property
def hook(self) -> AnalyticDBSparkHook:
"""Get valid hook."""
return AnalyticDBSparkHook(adb_spark_conn_id=self._adb_spark_conn_id, region=self._region)

@deprecated(reason="use `hook` property instead.", category=AirflowProviderDeprecationWarning)
def get_hook(self) -> AnalyticDBSparkHook:
"""Get valid hook."""
if self._adb_spark_hook is None or not isinstance(self._adb_spark_hook, AnalyticDBSparkHook):
self._adb_spark_hook = AnalyticDBSparkHook(
adb_spark_conn_id=self._adb_spark_conn_id, region=self._region
)
return self._adb_spark_hook
return self.hook

def poke(self, context: Context) -> bool:
app_id = self.app_id

state = self.get_hook.get_spark_state(app_id)
state = self.hook.get_spark_state(app_id)
return AppState(state) in AnalyticDBSparkHook.TERMINAL_STATES
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def setup_method(self):
@mock.patch(ADB_SPARK_SENSOR_STRING.format("AnalyticDBSparkHook"))
def test_get_hook(self, mock_service):
"""Test get_hook function works as expected."""
self.sensor.get_hook()
self.sensor.hook
mock_service.assert_called_once_with(adb_spark_conn_id=MOCK_ADB_SPARK_CONN_ID, region=MOCK_REGION)

@mock.patch(ADB_SPARK_SENSOR_STRING.format("AnalyticDBSparkSensor.get_hook"))
@mock.patch(ADB_SPARK_SENSOR_STRING.format("AnalyticDBSparkSensor.hook"))
def test_poke_terminal_state(self, mock_service):
"""Test poke_terminal_state works as expected with COMPLETED application."""
# Given
Expand All @@ -58,7 +58,7 @@ def test_poke_terminal_state(self, mock_service):
assert res is True
mock_service.get_spark_state.assert_called_once_with(MOCK_ADB_SPARK_ID)

@mock.patch(ADB_SPARK_SENSOR_STRING.format("AnalyticDBSparkSensor.get_hook"))
@mock.patch(ADB_SPARK_SENSOR_STRING.format("AnalyticDBSparkSensor.hook"))
def test_poke_non_terminal_state(self, mock_service):
"""Test poke_terminal_state works as expected with RUNNING application."""
# Given
Expand Down