You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "uranusjr (via GitHub)" <gi...@apache.org> on 2025/03/20 09:09:16 UTC

[PR] Implement on_execute_callback in task sdk [airflow]

uranusjr opened a new pull request, #47989:
URL: https://github.com/apache/airflow/pull/47989

   The callable is run *outside* of the timeout block. Failures calling any callback functions are simply logged without failing the entire task. This matches Airflow 2's behavior.
   
   Submitting this first to get feedback. Other callbacks will be implemented in later PRs if I'm on the right track.
   
   First part of #45424.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Implement on_execute_callback in task sdk [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #47989:
URL: https://github.com/apache/airflow/pull/47989#discussion_r2005207878


##########
task-sdk/tests/task_sdk/execution_time/test_task_runner.py:
##########
@@ -1928,6 +1928,83 @@ def execute(self, context):
         assert listener.error == error
 
 
+@pytest.mark.usefixtures("mock_supervisor_comms")
+class TestTaskRunnerCallsCallbacks:
+    def test_task_runner_calls_execute_callback(self):
+        results = []
+
+        def custom_callback(context):
+            results.append("callback")
+
+        class CustomOperator(BaseOperator):
+            def execute(self, context):
+                results.append("execute")
+
+        task = CustomOperator(
+            task_id="test_task_runner_calls_execute_callback",
+            on_execute_callback=custom_callback,
+        )
+        dag = get_inline_dag(dag_id="test_task_runner_calls_execute_callback", task=task)
+        ti = TaskInstance(
+            id=uuid7(),
+            task_id=task.task_id,
+            dag_id=dag.dag_id,
+            run_id="test_task_runner_calls_execute_callback",
+            try_number=1,
+        )
+
+        runtime_ti = RuntimeTaskInstance.model_construct(
+            **ti.model_dump(exclude_unset=True),
+            task=task,
+            start_date=timezone.utcnow(),
+        )

Review Comment:
   You could use `create_runtime_ti` fixture
   
   ```suggestion
           runtime_ti = create_runtime_ti(dag_id="test_task_runner_calls_execute_callback", task=task)
   ```
   
   
   https://github.com/apache/airflow/blob/f4af596beeed2a29fc4e878479f81b90ea8b140c/task-sdk/tests/conftest.py#L330



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Implement on_execute_callback in task sdk [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #47989:
URL: https://github.com/apache/airflow/pull/47989#discussion_r2005213418


##########
task-sdk/tests/task_sdk/execution_time/test_task_runner.py:
##########
@@ -1928,6 +1928,83 @@ def execute(self, context):
         assert listener.error == error
 
 
+@pytest.mark.usefixtures("mock_supervisor_comms")
+class TestTaskRunnerCallsCallbacks:
+    def test_task_runner_calls_execute_callback(self):
+        results = []
+
+        def custom_callback(context):
+            results.append("callback")
+
+        class CustomOperator(BaseOperator):
+            def execute(self, context):
+                results.append("execute")
+
+        task = CustomOperator(
+            task_id="test_task_runner_calls_execute_callback",
+            on_execute_callback=custom_callback,
+        )
+        dag = get_inline_dag(dag_id="test_task_runner_calls_execute_callback", task=task)
+        ti = TaskInstance(
+            id=uuid7(),
+            task_id=task.task_id,
+            dag_id=dag.dag_id,
+            run_id="test_task_runner_calls_execute_callback",
+            try_number=1,
+        )
+
+        runtime_ti = RuntimeTaskInstance.model_construct(
+            **ti.model_dump(exclude_unset=True),
+            task=task,
+            start_date=timezone.utcnow(),
+        )
+        log = mock.MagicMock()
+        state, _, _ = run(runtime_ti, log)
+
+        assert state == TerminalTIState.SUCCESS
+        assert results == ["callback", "execute"]
+
+    def test_task_runner_not_fail_on_failed_execute_callback(self):
+        results = []
+
+        def custom_callback_1(context):
+            results.append("callback 1")
+
+        def custom_callback_2(context):
+            raise Exception("sorry!")
+
+        class CustomOperator(BaseOperator):
+            def execute(self, context):
+                results.append("execute")
+
+        task = CustomOperator(
+            task_id="test_task_runner_calls_execute_callback",
+            on_execute_callback=[custom_callback_1, custom_callback_2],
+        )
+        dag = get_inline_dag(dag_id="test_task_runner_calls_execute_callback", task=task)
+        ti = TaskInstance(
+            id=uuid7(),
+            task_id=task.task_id,
+            dag_id=dag.dag_id,
+            run_id="test_task_runner_calls_execute_callback",
+            try_number=1,
+        )
+
+        runtime_ti = RuntimeTaskInstance.model_construct(
+            **ti.model_dump(exclude_unset=True),
+            task=task,
+            start_date=timezone.utcnow(),
+        )

Review Comment:
   ```suggestion
           runtime_ti = create_runtime_ti(
               dag_id="test_task_runner_calls_execute_callback"
               task=task,
               start_date=timezone.utcnow(),
           )
   ```



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Implement on_execute_callback in task sdk [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr merged PR #47989:
URL: https://github.com/apache/airflow/pull/47989


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org