kaxil commented on a change in pull request #16182:
URL: https://github.com/apache/airflow/pull/16182#discussion_r649564351



##########
File path: airflow/jobs/scheduler_job.py
##########
@@ -1417,6 +1420,42 @@ def _clean_tis_without_dagrun(self, session):
                     raise
             guard.commit()
 
+    @provide_session
+    def _missing_dag_file_cleanup(self, session: Session = None):
+        """Fails task instances and DagRuns of DAGs that no longer exist in 
the dag folder/DB"""
+        states_to_check = State.unfinished - frozenset([State.NONE, 
State.SHUTDOWN])
+        tis = session.query(TI).filter(TI.state.in_(states_to_check)).all()
+        missing_dags = {}
+        dag_runs = set()
+
+        for ti in tis:
+            if ti.dag_id in missing_dags:
+                ti.set_state(State.FAILED, session=session)
+                continue
+            # Dag no longer in dagbag?
+            if not self.dagbag.has_dag(ti.dag_id, session=session):
+                ti.set_state(State.FAILED, session=session)
+                dag_runs.add(ti.dag_run)
+                missing_dags[ti.dag_id] = [ti]
+                continue
+            # Dag file no longer exists?
+            if not os.path.exists(ti.dag_model.fileloc):
+                ti.set_state(State.FAILED, session=session)
+                dag_runs.add(ti.dag_run)
+                missing_dags[ti.dag_id] = [ti]
+        if missing_dags:
+            self.log.warning(
+                "The following Dags are missing, therefore the DAG's "
+                "task instances have been failed: \t\n%s",

Review comment:
       ```suggestion
                   "task instances have been set to failed: \t\n%s",
   ```




-- 
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


Reply via email to