Repository navigation
Conversation
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.
|
For reviewers: this touches |
|
What's the case |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions. |
…ce-failed-retry-note # Conflicts: # airflow-core/src/airflow/models/taskinstance.py
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.
|
Thanks, and sorry for the late answer @kaxil You're right and I've cut this PR down to a docstring change for |
|
Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions. |
related: #72426
on_task_instance_failedfires for an attempt that will be retried as well as for a final failure. In both the scheduler path (fetch_handle_failure_context) and the task runner (finalize) the task instance is alreadyUP_FOR_RETRYwhen the hook runs, so a listener can tell the two apart withtask_instance.stateand doesn't need a new hook (as discussed above).The documentation didn't say so: the hookspec docstring read "Execute when task state changes to FAIL". This PR only changes docs:
on_task_instance_failedon_task_instance_failedin the listeners guidetask_instance.stateThe
on_task_instance_up_for_retryhook this PR first proposed is removed.Not run: a Sphinx docs build and the unit tests (no Airflow environment here); ruff check and format are clean on the changed Python files.
Change
on_task_instance_up_for_retry(previous_state, task_instance, error)hookspec in the sharedlistener spec (
airflow_shared/listeners/spec/taskinstance.py), picked up automatically by boththe core and task-sdk listener managers (no separate registration needed, unlike specs that live
in their own module).
on_task_instance_failedcall sites, right after that call, only whenthe task instance's resulting state is
UP_FOR_RETRY:models/taskinstance.py::TaskInstance.fetch_handle_failure_context(API-server-triggered path)execution_time/task_runner.py'sfinalize()(task-execution path — this one already has adistinct
on_retry_callbackvson_failure_callbacksplit at the DAG-author callback level,so this hook follows the same precedent already established for user-facing callbacks)
on_task_instance_failedis left firing unchanged in both branches — this is purely additive,so no existing listener's behavior changes.
administration-and-deployment/listeners.rst's "Compatibility"table and TaskInstance events section, plus a runnable example in
example_dags/plugins/event_listener.py.does not fire for the terminal-failure branch, and that
on_task_instance_failedstill firesin both cases as before.
Design note
I considered instead scoping
on_task_instance_failedto the terminal-failure branch only (assuggested in the issue), but that would be a breaking behavior change for any listener already
relying on it firing for retries too. Adding the new hook without touching the existing one's
firing behavior is a strictly additive, backward-compatible change, so I went with that instead.
Happy to discuss if a different tradeoff is preferred.