|
7 | 7 |
|
8 | 8 | from airflow.triggers.base import BaseTrigger, TriggerEvent |
9 | 9 | from asgiref.sync import sync_to_async |
10 | | -from packaging.version import Version |
11 | 10 |
|
12 | | -from cosmos.constants import _DBT_STARTUP_EVENTS_XCOM_KEY, AIRFLOW_VERSION |
| 11 | +from cosmos.constants import _DBT_STARTUP_EVENTS_XCOM_KEY, AIRFLOW_VERSION, airflow_supports_task_sdk_runtime |
13 | 12 | from cosmos.listeners.dag_run_listener import EventStatus |
14 | 13 | from cosmos.log import get_logger |
15 | 14 | from cosmos.operators._watcher.state import ( |
@@ -79,25 +78,13 @@ def serialize(self) -> tuple[str, dict[str, Any]]: |
79 | 78 | async def get_xcom_val_af3(self, key: str) -> Any | None: |
80 | 79 | from airflow.sdk.execution_time.xcom import XCom |
81 | 80 |
|
82 | | - try: |
83 | | - return await sync_to_async(XCom.get_one)( |
84 | | - run_id=self.run_id, |
85 | | - key=key, |
86 | | - task_id=self.producer_task_id, |
87 | | - dag_id=self.dag_id, |
88 | | - map_index=self.map_index, |
89 | | - ) |
90 | | - except ImportError: |
91 | | - # On Airflow 3.0, ``XCom.get_one`` unconditionally imports ``SUPERVISOR_COMMS`` |
92 | | - # from ``airflow.sdk.execution_time.task_runner`` -- a symbol that only exists on |
93 | | - # Airflow 3.1+. Outside a task-SDK supervisor process (most notably ``dag.test()``, |
94 | | - # which runs deferred triggers inline) that import raises, the deferred task is |
95 | | - # re-queued forever, and the DAG run hangs silently. A real triggerer process has the |
96 | | - # supervisor environment, so the SDK path works there and stays primary; only the |
97 | | - # non-supervisor case falls back to the direct-DB ORM path, which the ``ti.xcom_pull`` |
98 | | - # implementation on AF 3.0 reads straight from ``XComModel`` (no supervisor needed). |
99 | | - # See apache/airflow#51816 and apache/airflow#59093. |
100 | | - return await self.get_xcom_val_af2(key) |
| 81 | + return await sync_to_async(XCom.get_one)( |
| 82 | + run_id=self.run_id, |
| 83 | + key=key, |
| 84 | + task_id=self.producer_task_id, |
| 85 | + dag_id=self.dag_id, |
| 86 | + map_index=self.map_index, |
| 87 | + ) |
101 | 88 |
|
102 | 89 | async def get_xcom_val_af2(self, key: str) -> Any | None: |
103 | 90 | from airflow.models import TaskInstance |
@@ -129,10 +116,12 @@ async def get_xcom_val(self, key: str) -> Any | None: |
129 | 116 | self.run_id, |
130 | 117 | self.map_index, |
131 | 118 | ) |
132 | | - if AIRFLOW_VERSION < Version("3.0.0"): |
133 | | - return await self.get_xcom_val_af2(key) |
134 | | - else: |
| 119 | + # Airflow 3.0's task-SDK XCom API raises outside a supervisor (e.g. dag.test()), so only |
| 120 | + # use it on 3.1+; Airflow < 3.1 reads XCom directly from the metadata DB. See #51816. |
| 121 | + if airflow_supports_task_sdk_runtime(AIRFLOW_VERSION): |
135 | 122 | return await self.get_xcom_val_af3(key) |
| 123 | + else: |
| 124 | + return await self.get_xcom_val_af2(key) |
136 | 125 |
|
137 | 126 | async def _get_node_status(self) -> Any | None: |
138 | 127 | """Return the dbt node status from XCom. |
|
0 commit comments