avanish-garg opened a new pull request, #72424:
URL: https://github.com/apache/airflow/pull/72424

   related: #72426
   
   `on_task_instance_failed` fires 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 already `UP_FOR_RETRY` when the hook runs, so a listener can tell 
the two apart with `task_instance.state` and 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:
   
   - the hookspec docstring for `on_task_instance_failed`
   - a paragraph under `on_task_instance_failed` in the listeners guide
   - the example listener, which now checks `task_instance.state`
   
   The `on_task_instance_up_for_retry` hook 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
   
   - New `on_task_instance_up_for_retry(previous_state, task_instance, error)` 
hookspec in the shared
     listener spec (`airflow_shared/listeners/spec/taskinstance.py`), picked up 
automatically by both
     the core and task-sdk listener managers (no separate registration needed, 
unlike specs that live
     in their own module).
   - Fired from both existing `on_task_instance_failed` call sites, right after 
that call, only when
     the 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`'s `finalize()` (task-execution path — 
this one already has a
       distinct `on_retry_callback` vs `on_failure_callback` split at the 
DAG-author callback level,
       so this hook follows the same precedent already established for 
user-facing callbacks)
   - `on_task_instance_failed` is left firing unchanged in both branches — this 
is purely additive,
     so no existing listener's behavior changes.
   - Docs: new bullet + example in 
`administration-and-deployment/listeners.rst`'s "Compatibility"
     table and TaskInstance events section, plus a runnable example in
     `example_dags/plugins/event_listener.py`.
   - Tests covering both call sites: that the new hook fires for the 
retry-eligible branch, that it
     does *not* fire for the terminal-failure branch, and that 
`on_task_instance_failed` still fires
     in both cases as before.
   
   ## Design note
   
   I considered instead scoping `on_task_instance_failed` to the 
terminal-failure branch only (as
   suggested 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.


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