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

   closes: #46224.
   
   When a trigger ends a deferred task directly with `TaskFailedEvent`, 
`email_on_failure` and `email_on_retry` were never honoured. #69821 already 
fixed retries and callbacks for this path, but emails were still skipped. This 
PR adds them.
   
   related: #46224
   
   ## What changed
   
   In `models/trigger.py`, the handler for `BaseTaskEndEvent` now sends an 
`EmailRequest` right after the callback is submitted and before any task state 
is changed.
   
   - The request has the same shape as the one the scheduler builds when it 
fails a task (`scheduler_job_runner.py`). It is sent through 
`DatabaseCallbackSink`, like the callback next to it, so the DAG processor 
sends the email.
   - The email type reuses the existing retry decision: "retry" if the callback 
type is `UP_FOR_RETRY`, otherwise "failure". There is no second retry check.
   - `task_instance_to_runtime` is now computed once and shared by the callback 
and the email.
   - No email is sent, with a log line, if the task definition could not be 
loaded, the task has no `email`, or the matching `email_on_retry` / 
`email_on_failure` flag is False.
   - Errors while building or sending the request are caught and logged, so 
they never block the state change.
   - There is no shared helper for building an `EmailRequest`: the scheduler 
constructs it inline in two places. I followed the same construction here, in a 
small nested helper next to `_submit_callback_if_necessary`, because the 
trigger path needs the skip conditions and error handling in one place.
   
   ## Testing
   
   New tests in `airflow-core/tests/unit/models/test_trigger.py` cover:
   - retry and failure emails
   - each flag-off combination and a task with no `email`
   - success and skipped events sending no email
   - task load failure
   - a send error not blocking the state change
   - the contents of the request
   
   Results, run in Breeze:
   - `-k task_end`: 20 passed.
   - With `trigger.py` reverted, six of the new test cases fail as expected 
(retry email, failure email, no email, task load error, send error, request 
contents). The flag-off and success/skipped cases are negative checks and pass 
either way.
   - Full `test_trigger.py` on an isolated SQLite DB: 112 passed, 1 skipped 
(that test needs the sqlite backend).
   - Not tested: real email delivery, because there is no SMTP server in my 
setup. The tests assert the `EmailRequest` that reaches the callback sink.
   
   ## Not in this PR
   
   Metrics, listeners, asset outlets, `on_skipped_callback` and `fail_fast` are 
also skipped on this path. I left them out to keep this reviewable and asked 
about them on the issue.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Sonnet 5.5 following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   
   ---


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