amoghrajesh commented on code in PR #57354:
URL: https://github.com/apache/airflow/pull/57354#discussion_r2469126057
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -1224,67 +1220,29 @@ def _get_email_subject_content(
'Mark success: <a href="{{ti.mark_success_url}}">Link</a><br>'
)
- default_html_content_err = (
- "Try {{try_number}} out of {{max_tries + 1}}<br>"
- "Exception:<br>Failed attempt to attach error logs<br>"
- 'Log: <a href="{{ti.log_url}}">Link</a><br>'
- "Host: {{ti.hostname}}<br>"
- 'Mark success: <a href="{{ti.mark_success_url}}">Link</a><br>'
- )
-
- additional_context: dict[str, Any] = {
- "exception": exception,
+ # Add exception_html to context for template rendering
+ exception_html = str(error).replace("\n", "<br>")
+ additional_context = {
+ "exception": error,
"exception_html": exception_html,
- "try_number": task_instance.try_number,
- "max_tries": task_instance.max_tries,
+ "try_number": ti.try_number,
+ "max_tries": ti.max_tries,
}
+ email_context = {**context, **additional_context}
+ to_emails = task.email
+ if not to_emails:
+ return
- # Use the Dag's get_template_env() to set force_sandboxed. Don't add
- # the flag to the function on task object -- that function can be
- # overridden, and adding a flag breaks backward compatibility.
- dag = task_instance.task.get_dag()
- if dag:
- jinja_env = dag.get_template_env(force_sandboxed=True)
- else:
- jinja_env = SandboxedEnvironment(cache_size=0)
- jinja_context = task_instance.get_template_context()
- if not jinja_context:
- jinja_context = Context()
- # Add additional fields to the context for email template rendering
- jinja_context.update(additional_context) # type: ignore[typeddict-item]
-
- def render(key: str, content: str) -> str:
- if conf.has_option("email", key):
- path = conf.get_mandatory_value("email", key)
- try:
- with open(path) as f:
- content = f.read()
- except FileNotFoundError:
- log.warning("Could not find email template file. Using
defaults...", file=path)
- except OSError:
- log.exception("Error while using email template. Using
defaults...", file=path)
- return render_template_to_string(jinja_env.from_string(content),
jinja_context)
-
- subject = render("subject_template", default_subject)
- html_content = render("html_content_template", default_html_content)
- html_content_err = render("html_content_template",
default_html_content_err)
-
- return subject, html_content, html_content_err
-
-
-def _send_task_error_email(
- to: Iterable[str],
- ti: RuntimeTaskInstance,
- exception: BaseException | str | None,
- log: Logger,
-) -> None:
- from airflow.utils.email import send_email
-
- subject, content, err = _get_email_subject_content(task_instance=ti,
exception=exception, log=log)
try:
- send_email(to, subject, content)
+ notifier = SmtpNotifier(
+ to=to_emails,
+ subject=default_subject,
+ html_content=default_html_content,
Review Comment:
@eladkal I had checked that out but I decided not to go for it because the
default template doesn't contain anything related to the exception like this
for example:
```shell script
Exception:
integer division or modulo by zero
```
It rather throws this:
<img width="1724" height="883" alt="image"
src="https://github.com/user-attachments/assets/a048864c-fa43-4dc9-a66d-165f0c436f87"
/>
vs
<img width="3448" height="1986" alt="image"
src="https://github.com/user-attachments/assets/13419c18-895a-46dd-ba0c-bf264bd37cb1"
/>
However, I like the subject more now, but I didnt change it to avoid
regression from previous version
--
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]