This is an automated email from the ASF dual-hosted git repository.
kaxil pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new a605c7a8fc2 Document that `on_task_instance_failed` also fires for
retried attempts (#72424)
a605c7a8fc2 is described below
commit a605c7a8fc2af602e708a44087347b6482569081
Author: Avanish Garg <[email protected]>
AuthorDate: Fri Nov 20 22:48:37 2026 +0530
Document that `on_task_instance_failed` also fires for retried attempts
(#72424)
Fired when a task instance is set to UP_FOR_RETRY after a failure, so
listeners can distinguish a task's automatic retries from its terminal
failures without inspecting task_instance.state themselves.
on_task_instance_failed continues to fire for both cases, unchanged,
for backward compatibility.
* Document that on_task_instance_failed also fires for retried attempts
The hook runs when an attempt will be retried as well as when it fails for
good. task_instance.state is UP_FOR_RETRY in the first case and FAILED in the
second, so a listener can tell them apart without a new hook. Say so in the
hookspec docstring, the listeners guide and the example listener, and remove
the on_task_instance_up_for_retry hook this PR first added.
---------
Co-authored-by: Kaxil Naik <[email protected]>
---
.../docs/administration-and-deployment/listeners.rst | 4 ++++
.../src/airflow/example_dags/plugins/event_listener.py | 12 ++++++++++--
.../src/airflow_shared/listeners/spec/taskinstance.py | 8 +++++++-
3 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/airflow-core/docs/administration-and-deployment/listeners.rst
b/airflow-core/docs/administration-and-deployment/listeners.rst
index bc75f42830d..8a8c27058b1 100644
--- a/airflow-core/docs/administration-and-deployment/listeners.rst
+++ b/airflow-core/docs/administration-and-deployment/listeners.rst
@@ -91,6 +91,10 @@ of a
:class:`~airflow.sdk.execution_time.task_runner.RuntimeTaskInstance` instan
- ``on_task_instance_failed``
+This event also occurs for an attempt that will be retried. In that case
``task_instance.state`` is
+``UP_FOR_RETRY``; it is ``FAILED`` when the task instance will not be retried.
A listener that should only
+react to final failures can check ``task_instance.state``.
+
.. exampleinclude:: /../src/airflow/example_dags/plugins/event_listener.py
:language: python
:start-after: [START howto_listen_ti_failure_task]
diff --git a/airflow-core/src/airflow/example_dags/plugins/event_listener.py
b/airflow-core/src/airflow/example_dags/plugins/event_listener.py
index 91af9f5ccc6..b07f9bcde84 100644
--- a/airflow-core/src/airflow/example_dags/plugins/event_listener.py
+++ b/airflow-core/src/airflow/example_dags/plugins/event_listener.py
@@ -21,11 +21,11 @@ from typing import TYPE_CHECKING
from airflow.listeners import hookimpl
from airflow.models.taskinstance import TaskInstance
+from airflow.utils.state import TaskInstanceState
if TYPE_CHECKING:
from airflow.models.dagrun import DagRun
from airflow.sdk.execution_time.task_runner import RuntimeTaskInstance
- from airflow.utils.state import TaskInstanceState
# [START howto_listen_ti_running_task]
@@ -102,16 +102,24 @@ def on_task_instance_failed(
error: None | str | BaseException,
):
"""
- Called when task state changes to FAILED.
+ Called when a task instance attempt fails, including an attempt that will
be retried.
previous_task_state, task_instance object and error can be used to
retrieve more information about current
task_instance that has failed, its dag_run, task and dag information.
+ ``task_instance.state`` is ``UP_FOR_RETRY`` when the attempt will be
retried and ``FAILED`` when it
+ will not.
+
A RuntimeTaskInstance is provided in most cases, except when the task's
state change is triggered
through the API. In that case, the TaskInstance available on the API
server will be provided instead.
"""
print("Task instance in failure state")
+ if task_instance.state == TaskInstanceState.UP_FOR_RETRY:
+ print("This attempt failed and the task instance will be retried.")
+ else:
+ print("The task instance failed and will not be retried.")
+
if isinstance(task_instance, TaskInstance):
print("Task instance's state was changed through the API.")
diff --git a/shared/listeners/src/airflow_shared/listeners/spec/taskinstance.py
b/shared/listeners/src/airflow_shared/listeners/spec/taskinstance.py
index d3450d6b05a..f9ad4cf5376 100644
--- a/shared/listeners/src/airflow_shared/listeners/spec/taskinstance.py
+++ b/shared/listeners/src/airflow_shared/listeners/spec/taskinstance.py
@@ -53,7 +53,13 @@ def on_task_instance_failed(
task_instance: RuntimeTaskInstance | TaskInstance,
error: None | str | BaseException,
):
- """Execute when task state changes to FAIL. previous_state can be None."""
+ """
+ Execute when a task instance attempt fails. previous_state can be None.
+
+ This also runs for an attempt that will be retried.
``task_instance.state`` is then
+ ``UP_FOR_RETRY``, and ``FAILED`` when the task instance will not be
retried. A listener that
+ should only react to final failures can check ``task_instance.state``.
+ """
@hookspec