ashb commented on code in PR #55241:
URL: https://github.com/apache/airflow/pull/55241#discussion_r2330848406


##########
airflow-core/src/airflow/triggers/deadline.py:
##########
@@ -49,26 +49,25 @@ async def run(self) -> AsyncIterator[TriggerEvent]:
         from airflow.models.deadline import DeadlineCallbackState  # to avoid 
cyclic imports
 
         try:
-            callback = import_string(self.callback_path)
             yield TriggerEvent({PAYLOAD_STATUS_KEY: 
DeadlineCallbackState.RUNNING})
+            callback = import_string(self.callback_path)
 
-            # TODO: get airflow context
-            context: dict = {}
-
-            result = await callback(**self.callback_kwargs, context=context)
-            log.info("Deadline callback completed with return value: %s", 
result)
+            # TODO: get full context and run template rendering. Right now, a 
simple context in included in `callback_kwargs`
+            result = await callback(**self.callback_kwargs)
             yield TriggerEvent({PAYLOAD_STATUS_KEY: 
DeadlineCallbackState.SUCCESS, PAYLOAD_BODY_KEY: result})
+
         except Exception as e:
             if isinstance(e, ImportError):
                 message = "Failed to import this deadline callback on the 
triggerer"
             elif isinstance(e, TypeError) and "await" in str(e):
                 message = "Failed to run this deadline callback because it is 
not awaitable"
             else:
                 message = "An error occurred during execution of this deadline 
callback"
-            log.exception("%s: %s; kwargs: %s\n%s", message, 
self.callback_path, self.callback_kwargs, e)
+
             yield TriggerEvent(
                 {
                     PAYLOAD_STATUS_KEY: DeadlineCallbackState.FAILED,
                     PAYLOAD_BODY_KEY: f"{message}: 
{traceback.format_exception(e)}",
                 }
             )
+            log.exception("%s: %s; kwargs: %s\n%s", message, 
self.callback_path, self.callback_kwargs, e)

Review Comment:
   I think this should be before the yield, as if this is anything like how the 
triggers for tasks are handled, once the deadline is in a "terminal" state the 
async task will get cancelled and there is a chance the final log never shows 
up?



##########
airflow-core/tests/unit/models/test_deadline.py:
##########
@@ -160,16 +161,37 @@ def test_repr_without_callback_kwargs(self, deadline_orm, 
dagrun, session):
         )
 
     @pytest.mark.db_test
-    def test_handle_miss_async_callback(self, dagrun, deadline_orm, session):
+    @pytest.mark.parametrize(
+        "kwargs",
+        [
+            pytest.param(TEST_CALLBACK_KWARGS, id="non-empty kwargs"),
+            pytest.param(None, id="null kwargs"),
+        ],
+    )
+    def test_handle_miss_async_callback(self, dagrun, session, kwargs):
+        deadline_orm = Deadline(
+            deadline_time=DEFAULT_DATE,
+            callback=AsyncCallback(TEST_CALLBACK_PATH, kwargs),
+            dagrun_id=dagrun.id,
+        )
+        session.add(deadline_orm)
+        session.flush()
         deadline_orm.handle_miss(session=session)
         session.flush()
 
         assert deadline_orm.trigger_id is not None
-
         trigger = session.query(Trigger).filter(Trigger.id == 
deadline_orm.trigger_id).one()
         assert trigger is not None
+
         assert trigger.kwargs["callback_path"] == TEST_CALLBACK_PATH
-        assert trigger.kwargs["callback_kwargs"] == TEST_CALLBACK_KWARGS
+
+        kwargs = trigger.kwargs["callback_kwargs"]
+        context = kwargs.pop("context")
+        assert kwargs == kwargs

Review Comment:
   I don't think you meant to assign this to `kwargs` here, because L190 is not 
testing anything anymore, because of course `kwargs == kwargs` :)  



##########
airflow-core/src/airflow/models/deadline.py:
##########
@@ -202,10 +202,20 @@ def handle_miss(self, session: Session):
         """Handle a missed deadline by running the callback in the appropriate 
host and updating the `callback_state`."""
         from airflow.sdk.definitions.deadline import AsyncCallback, 
SyncCallback
 
+        def get_simple_context():
+            from airflow.api_fastapi.core_api.datamodels.dag_run import 
DAGRunResponse
+
+            # TODO: Use the TaskAPI from within Triggerer to fetch full 
context instead of sending this context
+            #  from the scheduler
+            return {
+                "dag_run": 
DAGRunResponse.model_validate(self.dagrun).model_dump(mode="json"),
+                "deadline": {"id": self.id, "deadline_time": 
self.deadline_time},
+            }
+
         if isinstance(self.callback, AsyncCallback):
             callback_trigger = DeadlineCallbackTrigger(
                 callback_path=self.callback.path,
-                callback_kwargs=self.callback.kwargs,
+                callback_kwargs=(self.callback.kwargs or {}) | {"context": 
get_simple_context()},

Review Comment:
   Nit: let's take anything called context from the user if they specify it? 
   ```suggestion
                   callback_kwargs={"context": get_simple_context()} | 
(self.callback.kwargs or {}),
   ```



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