You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ds...@apache.org on 2023/03/02 14:54:51 UTC

[airflow] branch main updated: Separate classic and taskflow example dags for setup/teardown (#29824)

This is an automated email from the ASF dual-hosted git repository.

dstandish pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new c917c9de3d Separate classic and taskflow example dags for setup/teardown (#29824)
c917c9de3d is described below

commit c917c9de3db125cac1beb0a58ac81f56830fb9a5
Author: Jed Cunningham <66...@users.noreply.github.com>
AuthorDate: Thu Mar 2 08:54:37 2023 -0600

    Separate classic and taskflow example dags for setup/teardown (#29824)
---
 airflow/example_dags/example_setup_teardown.py     | 49 +++-------------------
 ...rdown.py => example_setup_teardown_taskflow.py} | 42 ++++++++++++-------
 tests/www/views/test_views_acl.py                  |  1 +
 3 files changed, 35 insertions(+), 57 deletions(-)

diff --git a/airflow/example_dags/example_setup_teardown.py b/airflow/example_dags/example_setup_teardown.py
index 43b78ea778..9b2b6b6814 100644
--- a/airflow/example_dags/example_setup_teardown.py
+++ b/airflow/example_dags/example_setup_teardown.py
@@ -20,7 +20,6 @@ from __future__ import annotations
 
 import pendulum
 
-from airflow.decorators import setup, task, task_group, teardown
 from airflow.models.dag import DAG
 from airflow.operators.bash import BashOperator
 from airflow.utils.task_group import TaskGroup
@@ -36,46 +35,10 @@ with DAG(
     BashOperator.as_teardown(task_id="root_teardown", bash_command="echo 'Goodbye from root_teardown'")
 
     with TaskGroup("section_1") as section_1:
+        BashOperator.as_setup(task_id="taskgroup_setup", bash_command="echo 'Hello from taskgroup_setup'")
+        BashOperator(task_id="normal", bash_command="echo 'I am just a normal task'")
+        BashOperator.as_setup(
+            task_id="taskgroup_teardown", bash_command="echo 'Hello from taskgroup_teardown'"
+        )
 
-        @setup
-        @task
-        def my_setup():
-            print("I set up")
-
-        @task
-        def hello():
-            print("I say hello")
-
-        @teardown
-        @task
-        def my_teardown():
-            print("I tear down")
-
-        my_setup()
-        hello()
-        my_teardown()
-
-    with TaskGroup("section_2") as section_2:
-
-        @setup
-        @task_group
-        def my_setup_taskgroup():
-            @task
-            def first_setup():
-                print("I set some stuff up")
-
-            @task
-            def second_setup():
-                print("I set some other stuff up")
-
-            first_setup()
-            second_setup()
-
-        @task
-        def hello():
-            print("I say hello")
-
-        my_setup_taskgroup()
-        hello()
-
-    normal >> section_1 >> section_2
+    normal >> section_1
diff --git a/airflow/example_dags/example_setup_teardown.py b/airflow/example_dags/example_setup_teardown_taskflow.py
similarity index 71%
copy from airflow/example_dags/example_setup_teardown.py
copy to airflow/example_dags/example_setup_teardown_taskflow.py
index 43b78ea778..eb8383c09b 100644
--- a/airflow/example_dags/example_setup_teardown.py
+++ b/airflow/example_dags/example_setup_teardown_taskflow.py
@@ -22,41 +22,53 @@ import pendulum
 
 from airflow.decorators import setup, task, task_group, teardown
 from airflow.models.dag import DAG
-from airflow.operators.bash import BashOperator
-from airflow.utils.task_group import TaskGroup
 
 with DAG(
-    dag_id="example_setup_teardown",
+    dag_id="example_setup_teardown_taskflow",
     start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
     catchup=False,
     tags=["example"],
 ) as dag:
-    BashOperator.as_setup(task_id="root_setup", bash_command="echo 'Hello from root_setup'")
-    normal = BashOperator(task_id="normal", bash_command="echo 'I am just a normal task'")
-    BashOperator.as_teardown(task_id="root_teardown", bash_command="echo 'Goodbye from root_teardown'")
+    # You can use the setup and teardown decorators to add setup and teardown tasks at the DAG level
+    @setup
+    @task
+    def root_setup():
+        print("Hello from root_setup")
 
-    with TaskGroup("section_1") as section_1:
+    @teardown
+    @task
+    def root_teardown():
+        print("Goodbye from root_teardown")
 
+    @task
+    def normal():
+        print("I am just a normal task")
+
+    @task_group
+    def section_1():
+        # You can also have setup and teardown tasks at the task group level
         @setup
         @task
         def my_setup():
             print("I set up")
 
-        @task
-        def hello():
-            print("I say hello")
-
         @teardown
         @task
         def my_teardown():
             print("I tear down")
 
+        @task
+        def hello():
+            print("I say hello")
+
         my_setup()
         hello()
         my_teardown()
 
-    with TaskGroup("section_2") as section_2:
-
+    @task_group
+    def section_2():
+        # You can also mark task groups as setup and teardown
+        # and all tasks in them will be setup/teardown tasks
         @setup
         @task_group
         def my_setup_taskgroup():
@@ -78,4 +90,6 @@ with DAG(
         my_setup_taskgroup()
         hello()
 
-    normal >> section_1 >> section_2
+    root_setup()
+    normal() >> section_1() >> section_2()
+    root_teardown()
diff --git a/tests/www/views/test_views_acl.py b/tests/www/views/test_views_acl.py
index d087c311e3..48a89b40d2 100644
--- a/tests/www/views/test_views_acl.py
+++ b/tests/www/views/test_views_acl.py
@@ -251,6 +251,7 @@ def test_dag_autocomplete_success(client_all_dags):
     )
     assert resp.json == [
         {"name": "airflow", "type": "owner"},
+        {"name": "example_setup_teardown_taskflow", "type": "dag"},
         {"name": "test_mapped_taskflow", "type": "dag"},
         {"name": "tutorial_taskflow_api", "type": "dag"},
         {"name": "tutorial_taskflow_api_virtualenv", "type": "dag"},