kaxil commented on a change in pull request #6633: [AIRFLOW-2279] Clear tasks 
across DAGs if marked by ExternalTaskMarker
URL: https://github.com/apache/airflow/pull/6633#discussion_r364043507
 
 

 ##########
 File path: airflow/models/dag.py
 ##########
 @@ -955,6 +993,60 @@ def clear(
         if only_running:
             tis = tis.filter(TI.state == State.RUNNING)
 
+        if include_subdags:
+            from airflow.sensors.external_task_sensor import ExternalTaskMarker
+
+            # Recursively find external tasks indicated by ExternalTaskMarker
+            instances = tis.all()
+            for ti in instances:
+                if ti.operator == ExternalTaskMarker.__name__:
+                    task = self.get_task(ti.task_id)
+
+                    if recursion_depth == 0:
+                        # Maximum recursion depth allowed is the 
recursion_depth of the first
+                        # ExternalTaskMarker in the tasks to be cleared.
+                        max_recursion_depth = task.recursion_depth
+
+                    if recursion_depth + 1 > max_recursion_depth:
+                        # Prevent cycles or accidents.
+                        raise AirflowException("Maximum recursion depth {} 
reached for {} {}. "
+                                               "Attempted to clear too many 
tasks "
+                                               "or there may be a cyclic 
dependency."
+                                               .format(max_recursion_depth,
+                                                       
ExternalTaskMarker.__name__, task.task_id))
+
+                    instance = TaskInstance(task=task, 
execution_date=ti.execution_date)
 
 Review comment:
   Can we not directly do `ti.render_templates` here?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to