ferruzzi commented on code in PR #58248:
URL: https://github.com/apache/airflow/pull/58248#discussion_r2563995663
##########
airflow-core/src/airflow/models/serialized_dag.py:
##########
@@ -371,6 +374,111 @@ def _sort_serialized_dag_dict(cls, serialized_dag: Any):
return [cls._sort_serialized_dag_dict(i) for i in serialized_dag]
return serialized_dag
+ @classmethod
+ def _process_deadline_alerts(
+ cls,
+ serialized_dag_id: str,
+ dag_data: dict[str, Any],
+ session: Session,
+ ) -> bool:
+ """
+ Process DeadlineAlerts for a Dag during serialization.
+
+ Creates or finds deadline_alert records in the database and replaces
+ the deadline field in dag_data with UUID references.
+
+ :param serialized_dag_id: The serialized_dag id
+ :param dag_data: The serialized Dag data dictionary (will be modified
in place)
+ :param session: Database session
+ """
+ dag_deadline_data = dag_data.get("dag", {}).get("deadline")
+ if not dag_deadline_data:
+ return False
+
+ log.debug("Processing DeadlineAlerts for Dag: %s", serialized_dag_id)
+
+ deadline_alerts = dag_deadline_data if isinstance(dag_deadline_data,
list) else [dag_deadline_data]
+ deadline_alert_ids = []
+ new_alerts = []
+
+ for deadline_alert in deadline_alerts:
+ deadline_data = deadline_alert.get(Encoding.VAR, deadline_alert)
+
+ reference = deadline_data[DeadlineAlertFields.REFERENCE]
+ interval = deadline_data[DeadlineAlertFields.INTERVAL]
+ callback = deadline_data[DeadlineAlertFields.CALLBACK]
+
+ # This looks odd, but I had issues comparing the serialized data
directly
+ # while doing manual testing. To avoid them, we fetch by dag_id
and interval,
+ # then use python's dict comparison instead of trying to match
strings in SQL.
+ candidates = (
+ session.execute(
+ select(DeadlineAlertModel).filter(
+ DeadlineAlertModel.serialized_dag_id ==
serialized_dag_id,
+ DeadlineAlertModel.interval == interval,
+ )
+ )
+ .scalars()
+ .all()
+ )
+
+ existing_alert = None
+ for alert in candidates:
+ if alert.reference == reference and alert.callback_def ==
callback:
+ existing_alert = alert
+ break
+
+ if existing_alert:
+ log.debug("Found existing DeadlineAlert: %s",
existing_alert.id)
+ deadline_alert_ids.append(str(existing_alert.id))
+ else:
+ log.warning("No existing alert found, creating... ")
Review Comment:
Sure, I'll make the change.
--
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]