ephraimbuddy commented on code in PR #73430:
URL: https://github.com/apache/airflow/pull/73430#discussion_r4740379459


##########
airflow-core/src/airflow/models/dagrun.py:
##########
@@ -1506,14 +1507,18 @@ def recalculate(self) -> _UnfinishedStates:
 
             if dag.deadline:
                 # The dagrun has succeeded.  If there were any Deadlines for 
it which were not breached, they are no longer needed.
-                deadline_alerts = [
-                    DeadlineAlertModel.get_by_id(alert_id, session=session) 
for alert_id in dag.deadline
-                ]
+                # Entries are deadline_alert ids for DAGs loaded from the 
database, but encoded deadline
+                # alerts after the in-memory round trip in dag.test(), where 
there are no rows to look up.
+                deadline_refs = []
+                for entry in dag.deadline:
+                    if isinstance(entry, str):
+                        deadline_refs.append(
+                            DeadlineAlertModel.get_by_id(entry, 
session=session).reference_class
+                        )
+                    else:
+                        
deadline_refs.append(type(decode_deadline_alert(entry).reference))
 
-                if any(
-                    deadline_alert.reference_class in 
SerializedReferenceModels.TYPES.DAGRUN
-                    for deadline_alert in deadline_alerts
-                ):
+                if any(ref in SerializedReferenceModels.TYPES.DAGRUN for ref 
in deadline_refs):
                     Deadline.prune_deadlines(session=session, 
conditions={DagRun.id: self.id})

Review Comment:
   This lookup isn't needed: every Deadline tied to a run comes from a 
`TYPES.DAGRUN` alert, so we can prune directly.
   
   ```suggestion
               if dag.deadline:
                   # The dagrun has succeeded.  If there were any Deadlines for 
it which were not breached, they are no longer needed.
                   Deadline.prune_deadlines(session=session, 
conditions={DagRun.id: self.id})
   ```



##########
airflow-core/tests/unit/models/test_dagrun.py:
##########
@@ -2031,6 +2033,37 @@ def test_dagrun_success_prunes_dagrun_deadlines(
         mock_prune.assert_called_once_with(session=session, 
conditions={DagRun.id: dag_run.id})
         assert dag_run.state == DagRunState.SUCCESS
 
+    @mock.patch.object(Deadline, "prune_deadlines")
+    @mock.patch.object(DeadlineAlertModel, "get_by_id")
+    def test_dagrun_success_prunes_serialized_deadlines(
+        self, mock_get_by_id, mock_prune, session, deadline_test_dag
+    ):
+        """dag.test() round-trips the DAG in memory, so dag.deadline holds 
encoded
+        deadline alerts instead of deadline_alert ids, and there are no rows 
to fetch."""
+        alert = DeadlineAlert(
+            reference=DeadlineReference.DAGRUN_LOGICAL_DATE,
+            interval=datetime.timedelta(hours=1),
+            callback=AsyncCallback(empty_callback_for_deadline),
+        )
+
+        scheduler_dag = deadline_test_dag()
+        scheduler_dag.deadline = [

Review Comment:
   `dag.test()` doesn't produce this wrapped shape, so can we test through 
`dag.test()` on a Dag with a deadline instead?



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

Reply via email to