forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AIP-72: Support DAG parsing context in Task SDK (apache#45694)
- Loading branch information
Showing
15 changed files
with
214 additions
and
53 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
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,51 @@ | ||
``get_parsing_context`` have been moved to Task SDK | ||
|
||
As part of AIP-72: Task SDK, the function ``get_parsing_context`` has been moved to ``airflow.sdk`` module. | ||
Previously, it was located in ``airflow.utils.dag_parsing_context`` module. | ||
|
||
This function is used to optimize DAG parsing during execution when DAGs are generated dynamically. | ||
|
||
Before: | ||
|
||
.. code-block:: python | ||
from airflow.models.dag import DAG | ||
from airflow.utils.dag_parsing_context import get_parsing_context | ||
current_dag_id = get_parsing_context().dag_id | ||
for thing in list_of_things: | ||
dag_id = f"generated_dag_{thing}" | ||
if current_dag_id is not None and current_dag_id != dag_id: | ||
continue # skip generation of non-selected DAG | ||
with DAG(dag_id=dag_id, ...): | ||
... | ||
After: | ||
|
||
.. code-block:: python | ||
from airflow.sdk import get_parsing_context | ||
current_dag_id = get_parsing_context().dag_id | ||
# The rest of the code remains the same | ||
* Types of change | ||
|
||
* [x] DAG changes | ||
* [ ] Config changes | ||
* [ ] API changes | ||
* [ ] CLI changes | ||
* [ ] Behaviour changes | ||
* [ ] Plugin changes | ||
* [ ] Dependency change | ||
|
||
* Migration rules needed | ||
|
||
* ruff | ||
|
||
* AIR302 | ||
|
||
* [ ] ``airflow.utils.dag_parsing_context.get_parsing_context`` -> ``airflow.sdk.get_parsing_context`` |
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
37 changes: 37 additions & 0 deletions
37
task_sdk/src/airflow/sdk/definitions/_internal/dag_parsing_context.py
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,37 @@ | ||
# 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. | ||
from __future__ import annotations | ||
|
||
import os | ||
from contextlib import contextmanager | ||
|
||
from airflow.sdk.definitions.context import _AIRFLOW_PARSING_CONTEXT_DAG_ID, _AIRFLOW_PARSING_CONTEXT_TASK_ID | ||
|
||
|
||
@contextmanager | ||
def _airflow_parsing_context_manager(dag_id: str | None = None, task_id: str | None = None): | ||
old_dag_id = os.environ.get(_AIRFLOW_PARSING_CONTEXT_DAG_ID) | ||
old_task_id = os.environ.get(_AIRFLOW_PARSING_CONTEXT_TASK_ID) | ||
if dag_id is not None: | ||
os.environ[_AIRFLOW_PARSING_CONTEXT_DAG_ID] = dag_id | ||
if task_id is not None: | ||
os.environ[_AIRFLOW_PARSING_CONTEXT_TASK_ID] = task_id | ||
yield | ||
if old_task_id is not None: | ||
os.environ[_AIRFLOW_PARSING_CONTEXT_TASK_ID] = old_task_id | ||
if old_dag_id is not None: | ||
os.environ[_AIRFLOW_PARSING_CONTEXT_DAG_ID] = old_dag_id |
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,36 @@ | ||
# 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. | ||
from __future__ import annotations | ||
|
||
from datetime import datetime | ||
|
||
from airflow.sdk import DAG, BaseOperator, get_parsing_context | ||
|
||
DAG_ID = "dag_parsing_context_test" | ||
|
||
current_dag_id = get_parsing_context().dag_id | ||
|
||
with DAG( | ||
DAG_ID, | ||
start_date=datetime(2024, 2, 21), | ||
schedule=None, | ||
) as the_dag: | ||
BaseOperator(task_id="visible_task") | ||
|
||
if current_dag_id == DAG_ID: | ||
# this task will be invisible if the DAG ID is not properly set in the parsing context. | ||
BaseOperator(task_id="conditional_task") |
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