amoghrajesh opened a new pull request, #48046:
URL: https://github.com/apache/airflow/pull/48046
<!--
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.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
closes: https://github.com/apache/airflow/issues/45639
@task.bash code currently requires DB interaction to run
`refresh_bash_command` and that is needed because in case of decorator, the
underlying bash_command value is only populated at runtime and we need the
command to be a runtime evaluated command.
## Testing:
DAG 1: Simpler one:
```
# 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 pendulum
from airflow.decorators import dag, task
from airflow.exceptions import AirflowSkipException
from airflow.providers.standard.operators.empty import EmptyOperator
from airflow.providers.standard.utils.weekday import WeekDay
from airflow.sdk import chain
from airflow.utils.trigger_rule import TriggerRule
@dag(schedule=None, start_date=pendulum.datetime(2023, 1, 1, tz="UTC"),
catchup=False)
def simple_bash_decorator():
@task.bash
def run_me(sleep_seconds: int, task_instance_key_str: str) -> str:
return f"echo {task_instance_key_str} && sleep {sleep_seconds}"
run_me_loop = run_me(sleep_seconds=10)
print(run_me_loop)
simple_bash_decorator()
```
<img width="866" alt="image"
src="https://github.com/user-attachments/assets/2376c8cd-9d92-4df1-bb61-ca4dc057fd6b"
/>
Value pushed as xcom:
<img width="866" alt="image"
src="https://github.com/user-attachments/assets/cf203a9a-4e06-4b1f-a6b6-2381f16fe454"
/>
RTIF table:
```
simple_bash_decorator,run_me,manual__2025-03-21T10:06:09.220669+00:00_zW7zgFQq,-1,"{""op_args"":
[], ""op_kwargs"": {""sleep_seconds"": 10}, ""bash_command"": ""echo
simple_bash_decorator__run_me__manual__2025-03-21T10:06:09.220669+00:00_zW7zgFQq
&& sleep 10"", ""env"": null, ""cwd"": null}",null
```
DAG2:
```
# 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 pendulum
from airflow.decorators import dag, task
from airflow.exceptions import AirflowSkipException
from airflow.providers.standard.operators.empty import EmptyOperator
from airflow.providers.standard.utils.weekday import WeekDay
from airflow.sdk import chain
from airflow.utils.trigger_rule import TriggerRule
@dag(schedule=None, start_date=pendulum.datetime(2023, 1, 1, tz="UTC"),
catchup=False)
def example_bash_decorator():
@task.bash
def run_me(sleep_seconds: int, task_instance_key_str: str) -> str:
return f"echo {task_instance_key_str} && sleep {sleep_seconds}"
run_me_loop = [run_me.override(task_id=f"runme_{i}")(sleep_seconds=i)
for i in range(3)]
# [START howto_decorator_bash]
@task.bash
def run_after_loop() -> str:
return "echo https://airflow.apache.org/"
run_this = run_after_loop()
# [END howto_decorator_bash]
# [START howto_decorator_bash_template]
@task.bash
def also_run_this() -> str:
return 'echo "ti_key={{ task_instance_key_str }}"'
also_this = also_run_this()
# [END howto_decorator_bash_template]
# [START howto_decorator_bash_context_vars]
@task.bash
def also_run_this_again(task_instance_key_str) -> str:
return f'echo "ti_key={task_instance_key_str}"'
also_this_again = also_run_this_again()
# [END howto_decorator_bash_context_vars]
# [START howto_decorator_bash_skip]
@task.bash
def this_will_skip() -> str:
return 'echo "hello world"; exit 99;'
this_skips = this_will_skip()
# [END howto_decorator_bash_skip]
run_this_last = EmptyOperator(task_id="run_this_last",
trigger_rule=TriggerRule.ALL_DONE)
# [START howto_decorator_bash_conditional]
@task.bash
def sleep_in(day: str) -> str:
if day in (WeekDay.SATURDAY, WeekDay.SUNDAY):
return f"sleep {60 * 60}"
else:
raise AirflowSkipException("No sleeping in today!")
sleep_in(day="{{ dag_run.logical_date.strftime('%A').lower() }}")
# [END howto_decorator_bash_conditional]
# [START howto_decorator_bash_parametrize]
@task.bash(env={"BASE_DIR": "{{
dag_run.logical_date.strftime('%Y/%m/%d') }}"}, append_env=True)
def make_dynamic_dirs(new_dirs: str) -> str:
return f"mkdir -p $AIRFLOW_HOME/$BASE_DIR/{new_dirs}"
make_dynamic_dirs(new_dirs="foo/bar/baz")
# [END howto_decorator_bash_parametrize]
# [START howto_decorator_bash_build_cmd]
def _get_files_in_cwd() -> list[str]:
from pathlib import Path
dir_contents = Path.cwd().glob("airflow/example_dags/*.py")
files = [str(elem) for elem in dir_contents if elem.is_file()]
return files
@task.bash
def get_file_stats() -> str:
from shlex import join
files = _get_files_in_cwd()
cmd = join(["stat", *files])
return cmd
get_file_stats()
# [END howto_decorator_bash_build_cmd]
chain(run_me_loop, run_this)
chain([also_this, also_this_again, this_skips, run_this], run_this_last)
example_bash_decorator()
```
<img width="866" alt="image"
src="https://github.com/user-attachments/assets/92367365-7762-4954-8df4-8713449a488a"
/>
The failure is cos of another issue, not related to this PR.
RTIF table:
example_bash_decorator,also_run_this,manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj,-1,"{""op_args"":
[], ""op_kwargs"": {}, ""bash_command"": ""echo
\""ti_key=example_bash_decorator__also_run_this__manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj\"""",
""env"": null, ""cwd"": null}",null
example_bash_decorator,also_run_this_again,manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj,-1,"{""op_args"":
[], ""op_kwargs"": {}, ""bash_command"": ""echo
\""ti_key=example_bash_decorator__also_run_this_again__manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj\"""",
""env"": null, ""cwd"": null}",null
example_bash_decorator,this_will_skip,manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj,-1,"{""op_args"":
[], ""op_kwargs"": {}, ""bash_command"": ""echo \""hello world\""; exit 99;"",
""env"": null, ""cwd"": null}",null
example_bash_decorator,runme_2,manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj,-1,"{""op_args"":
[], ""op_kwargs"": {""sleep_seconds"": 2}, ""bash_command"": ""echo
example_bash_decorator__runme_2__manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj
&& sleep 2"", ""env"": null, ""cwd"": null}",null
example_bash_decorator,runme_1,manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj,-1,"{""op_args"":
[], ""op_kwargs"": {""sleep_seconds"": 1}, ""bash_command"": ""echo
example_bash_decorator__runme_1__manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj
&& sleep 1"", ""env"": null, ""cwd"": null}",null
example_bash_decorator,runme_0,manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj,-1,"{""op_args"":
[], ""op_kwargs"": {""sleep_seconds"": 0}, ""bash_command"": ""echo
example_bash_decorator__runme_0__manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj
&& sleep 0"", ""env"": null, ""cwd"": null}",null
example_bash_decorator,get_file_stats,manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj,-1,"{""op_args"":
[], ""op_kwargs"": {}, ""bash_command"": ""stat
/opt/***/***/example_dags/example_latest_only.py
/opt/***/***/example_dags/example_local_kubernetes_executor.py
/opt/***/***/example_dags/example_task_group_decorator.py
/opt/***/***/example_dags/example_complex.py
/opt/***/***/example_dags/example_asset_with_watchers.py
/opt/***/***/example_dags/example_python_operator.py
/opt/***/***/example_dags/tutorial_taskflow_api_virtualenv.py
/opt/***/***/example_dags/example_params_trigger_ui.py
/opt/***/***/example_dags/example_external_task_marker_dag.py
/opt/***/***/example_dags/tutorial_objectstorage.py
/opt/***/***/example_dags/example_bash_operator.py
/opt/***/***/example_dags/example_short_circuit_operator.py
/opt/***/***/example_dags/example_setup_teardown.py
/opt/***/***/example_dags/example_branch_operator.py
/opt/***/***/example_dags/example_branch_labels.py /opt/***/***/examp
le_dags/tutorial_taskflow_api.py
/opt/***/***/example_dags/example_task_group.py
/opt/***/***/example_dags/tutorial.py
/opt/***/***/example_dags/example_custom_weight.py
/opt/***/***/example_dags/example_inlet_event_extra.py
/opt/***/***/example_dags/example_python_decorator.py
/opt/***/***/example_dags/example_asset_decorator.py
/opt/***/***/example_dags/example_passing_params_via_test_command.py
/opt/***/***/example_dags/example_latest_only_with_trigger.py
/opt/***/***/example_dags/example_time_delta_sensor_async.py
/opt/***/***/example_dags/example_dag_decorator.py
/opt/***/***/example_dags/__init__.py
/opt/***/***/example_dags/example_simplest_dag.py
/opt/***/***/example_dags/example_xcom.py
/opt/***/***/example_dags/example_assets.py
/opt/***/***/example_dags/example_xcomargs.py
/opt/***/***/example_dags/example_display_name.py
/opt/***/***/example_dags/example_branch_operator_decorator.py
/opt/***/***/example_dags/example_branch_day_of_week_operator.py
/opt/***/***/example_dag
s/example_skip_dag.py /opt/***/***/example_dags/example_outlet_event_extra.py
/opt/***/***/example_dags/example_short_circuit_decorator.py
/opt/***/***/example_dags/tutorial_dag.py
/opt/***/***/example_dags/example_trigger_target_dag.py
/opt/***/***/example_dags/example_bash_decorator.py
/opt/***/***/example_dags/example_workday_timetable.py
/opt/***/***/example_dags/example_sensor_decorator.py
/opt/***/***/example_dags/tutorial_taskflow_templates.py
/opt/***/***/example_dags/example_asset_alias.py
/opt/***/***/example_dags/example_kubernetes_executor.py
/opt/***/***/example_dags/example_asset_alias_with_no_taskflow.py
/opt/***/***/example_dags/example_branch_python_dop_operator_3.py
/opt/***/***/example_dags/example_sensors.py
/opt/***/***/example_dags/example_setup_teardown_taskflow.py
/opt/***/***/example_dags/example_branch_datetime_operator.py
/opt/***/***/example_dags/example_nested_branch_dag.py
/opt/***/***/example_dags/example_dynamic_task_mapping_with_no_taskflow_operators
.py /opt/***/***/example_dags/example_trigger_controller_dag.py
/opt/***/***/example_dags/example_params_ui_tutorial.py
/opt/***/***/example_dags/example_dynamic_task_mapping.py"", ""env"": null,
""cwd"": null}",null
example_bash_decorator,run_after_loop,manual__2025-03-21T09:55:37.955817+00:00_Xg7KJ8Jj,-1,"{""op_args"":
[], ""op_kwargs"": {}, ""bash_command"": ""echo https://***.apache.org/"",
""env"": null, ""cwd"": null}",null
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
for more information.
In case of fundamental code changes, an Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party
License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a
newsfragment file, named `{pr_number}.significant.rst` or
`{issue_number}.significant.rst`, in
[newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
--
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]