uranusjr commented on code in PR #32169:
URL: https://github.com/apache/airflow/pull/32169#discussion_r1247415859


##########
docs/apache-airflow/core-concepts/dags.rst:
##########
@@ -484,6 +484,125 @@ You can also combine this with the 
:ref:`concepts:depends-on-past` functionality
     .. image:: /img/branch_with_trigger.png
 
 
+.. _concepts:setup-and-teardown:
+
+Setup and Teardown
+~~~~~~~~~~~~~~~~~~
+
+In data workflows it's common to create resources (such as a compute 
resource), use it to do some work, and then tear it down. Airflow provides 
setup and teardown tasks to support this need.
+
+Key features of setup and teardown tasks:
+
+  * If you clear a task, its setups and teardowns will be cleared.
+  * By default, teardown tasks are ignored for the purpose of evaluating dag 
run state.
+  * A teardown task will run if it's setup was successful, even if its work 
tasks failed.
+  * Teardown tasks are ignored when setting dependencies against task groups.
+  * A setup task must always have a teardown and vice versa. You may use 
EmptyOperator as a setup or teardown.
+
+Basic usage
+"""""""""""
+
+Suppose you have a dag that creates a cluster, runs a query, and deletes the 
cluster. Without using setup and teardown tasks you might set these 
relationships:
+
+.. code-block:: python
+
+  create_cluster >> run_query >> delete_cluster
+
+We can use the ``as_teardown`` let airflow know that ``create_cluster`` is a 
setup task and ``delete_cluster`` is its teardown:
+
+.. code-block:: python
+
+  create_cluster >> run_query >> 
delete_cluster.as_teardown(setups=create_cluster)
+
+Observations:
+
+  * If you clear ``run_query`` to run it again, then both ``create_cluster`` 
and ``delete_cluster`` will be cleared.
+  * If ``run_query`` fails, then ``delete_cluster`` will still run.
+  * The success of the dag run will depend on the success of ``run_query``.
+
+Setup "scope"
+"""""""""""""
+
+We require that a setup always have a teardown in order to have a well-defined 
scope. If you wish to only add a teardown task or only a setup task, you may 
use EmptyOperator as your "empty setup" or "empty teardown".
+
+The "scope" of a setup will be determined by where the teardown is.  Tasks 
between a setup and its teardown are in the "scope" of the setup / teardown 
pair. Example:
+
+.. code-block:: python
+
+    s1 >> w1 >> w2 >> t1.as_teardown(setups=s1) >> w3
+    w2 >> w4

Review Comment:
   This indentation is not consistent. Do we want to indent by two or four in 
this document?



-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to