Skip to content

Commit 5a38dfd

Browse files
committed
Improve default task failure email template for readability
- Replace plain text email with professional HTML template - Add dark-themed design matching Airflow UI - Include structured task details (run, attempt, host, tags) - Add Airflow 3.x compatibility (conditional rendering for URLs) - Provide plain text fallback for email clients without HTML support - Maintain backward compatibility (custom templates still work) Fixes apache#61667
1 parent 1522953 commit 5a38dfd

4 files changed

Lines changed: 181 additions & 11 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body style="margin:0;padding:0;background-color:#171c2f;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif">
4+
<table width="100%" cellpadding="0" cellspacing="0" style="padding:64px 24px 72px 24px">
5+
<tr>
6+
<td align="center">
7+
<table width="600" cellpadding="0" cellspacing="0">
8+
<!-- Logo -->
9+
<tr>
10+
<td align="center" style="padding-bottom:48px">
11+
<img src="https://cwiki.apache.org/confluence/download/attachments/145723561/airflow_transparent.png" width="56" height="56" alt="Airflow" style="display:block;width:56px;height:56px">
12+
</td>
13+
</tr>
14+
<!-- Status -->
15+
<tr>
16+
<td style="padding-bottom:0">
17+
<table cellpadding="0" cellspacing="0">
18+
<tr>
19+
<td style="font-size:13px;color:#ef4444;line-height:1;vertical-align:middle">&#9679;</td>
20+
<td style="padding-left:8px">
21+
<span style="font-size:13px;font-weight:600;text-transform:uppercase;letter-spacing:0.08em;color:#ef4444">Task Failed</span>
22+
</td>
23+
</tr>
24+
</table>
25+
</td>
26+
</tr>
27+
<!-- DAG name -->
28+
<tr>
29+
<td style="padding-bottom:6px;padding-top:8px">
30+
<span style="font-size:28px;font-weight:700;color:#e8ecf4;letter-spacing:-0.02em">{{ti.dag_id}}</span>
31+
</td>
32+
</tr>
33+
<!-- Task name -->
34+
<tr>
35+
<td style="padding-bottom:24px">
36+
<span style="font-size:18px;color:#8896b3">{{ti.task_id}}</span>
37+
</td>
38+
</tr>
39+
<!-- Details card -->
40+
<tr>
41+
<td style="padding-bottom:24px">
42+
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#1f2539;border-radius:8px;border:1px solid #2d3450">
43+
<tr>
44+
<td style="padding:24px">
45+
<table width="100%" cellpadding="0" cellspacing="0" style="font-size:15px;line-height:1.5">
46+
<tr>
47+
<td style="padding:8px 0;color:#8896b3;width:100px;vertical-align:top">Run</td>
48+
<td style="padding:8px 0;color:#e8ecf4">{{ti.run_id}}</td>
49+
</tr>
50+
<tr>
51+
<td colspan="2" style="border-bottom:1px solid #2d3450;font-size:0;line-height:0">&nbsp;</td>
52+
</tr>
53+
<tr>
54+
<td style="padding:8px 0;color:#8896b3;vertical-align:top">Attempt</td>
55+
<td style="padding:8px 0;color:#e8ecf4">{{try_number}} of {{max_tries + 1}}</td>
56+
</tr>
57+
<tr>
58+
<td colspan="2" style="border-bottom:1px solid #2d3450;font-size:0;line-height:0">&nbsp;</td>
59+
</tr>
60+
<tr>
61+
<td style="padding:8px 0;color:#8896b3;vertical-align:top">Host</td>
62+
<td style="padding:8px 0;color:#e8ecf4;font-family:'SF Mono',SFMono-Regular,Consolas,'Liberation Mono',Menlo,monospace;font-size:14px">{{ti.hostname}}</td>
63+
</tr>
64+
{% if dag and dag.tags %}
65+
<tr>
66+
<td colspan="2" style="border-bottom:1px solid #2d3450;font-size:0;line-height:0">&nbsp;</td>
67+
</tr>
68+
<tr>
69+
<td style="padding:8px 0;color:#8896b3;vertical-align:top">Tags</td>
70+
<td style="padding:8px 0">
71+
{% for tag in dag.tags|sort %}<span style="display:inline-block;background-color:#2d3450;color:#a3b3d1;padding:3px 12px;border-radius:20px;font-size:13px;font-weight:500;margin:2px 4px 2px 0">{{tag}}</span>{% endfor %}
72+
</td>
73+
</tr>
74+
{% endif %}
75+
</table>
76+
</td>
77+
</tr>
78+
</table>
79+
</td>
80+
</tr>
81+
<!-- Exception card -->
82+
<tr>
83+
<td style="padding-bottom:32px">
84+
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#2a1215;border-radius:8px;border:1px solid #4a2025">
85+
<tr>
86+
<td style="padding:20px 24px;font-family:'SF Mono',SFMono-Regular,Consolas,'Liberation Mono',Menlo,monospace;font-size:14px;line-height:1.7;color:#fca5a5;word-break:break-word">
87+
{{exception_html}}
88+
</td>
89+
</tr>
90+
</table>
91+
</td>
92+
</tr>
93+
<!-- Actions -->
94+
<tr>
95+
<td>
96+
<table cellpadding="0" cellspacing="0">
97+
<tr>
98+
{% if ti.log_url %}
99+
<td style="padding-right:12px">
100+
<a href="{{ti.log_url}}" style="display:inline-block;padding:8px 28px;background-color:#3b82f6;color:#ffffff;text-decoration:none;border-radius:4px;font-size:15px;font-weight:600">View Log</a>
101+
</td>
102+
{% endif %}
103+
{% if ti.mark_success_url %}
104+
<td>
105+
<a href="{{ti.mark_success_url}}" style="display:inline-block;padding:8px 28px;background-color:transparent;color:#a3b3d1;text-decoration:none;border-radius:4px;font-size:15px;font-weight:500;border:1px solid #2d3450">Mark Success</a>
106+
</td>
107+
{% endif %}
108+
</tr>
109+
</table>
110+
</td>
111+
</tr>
112+
</table>
113+
</td>
114+
</tr>
115+
</table>
116+
</body>
117+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
AIRFLOW TASK FAILURE
2+
====================
3+
4+
● TASK FAILED
5+
6+
DAG: {{ti.dag_id}}
7+
Task: {{ti.task_id}}
8+
9+
Details
10+
-------
11+
Run: {{ti.run_id}}
12+
Attempt: {{try_number}} of {{max_tries + 1}}
13+
Host: {{ti.hostname}}
14+
{% if dag and dag.tags %}Tags: {% for tag in dag.tags|sort %}{{tag}}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %}
15+
16+
Exception
17+
---------
18+
{{exception}}
19+
20+
Actions
21+
-------
22+
{% if ti.log_url %}View Log: {{ti.log_url}}{% endif %}
23+
{% if ti.mark_success_url %}Mark Success: {{ti.mark_success_url}}{% endif %}
24+
25+
---
26+
Apache Airflow
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Airflow alert: {{ti.dag_id}} / {{ti.task_id}} failed

‎task-sdk/src/airflow/sdk/execution_time/task_runner.py‎

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,25 +1573,51 @@ def _send_error_email_notification(
15731573
if subject_template_file and Path(subject_template_file).exists():
15741574
subject = Path(subject_template_file).read_text()
15751575
else:
1576-
# Fallback to default
1577-
subject = "Airflow alert: {{ti}}"
1576+
# Try to load from built-in template files
1577+
default_subject_template = (
1578+
Path(__file__).parent.parent.parent.parent
1579+
/ "airflow-core"
1580+
/ "src"
1581+
/ "airflow"
1582+
/ "templates"
1583+
/ "email"
1584+
/ "task_failure_subject.txt"
1585+
)
1586+
if default_subject_template.exists():
1587+
subject = default_subject_template.read_text()
1588+
else:
1589+
# Final fallback to inline default for backward compatibility
1590+
subject = "Airflow alert: {{ti}}"
15781591

15791592
html_content_template_file = conf.get("email", "html_content_template", fallback=None)
15801593

15811594
# Read the template file if configured
15821595
if html_content_template_file and Path(html_content_template_file).exists():
15831596
html_content = Path(html_content_template_file).read_text()
15841597
else:
1585-
# Fallback to default
1586-
# For reporting purposes, we report based on 1-indexed,
1587-
# not 0-indexed lists (i.e. Try 1 instead of Try 0 for the first attempt).
1588-
html_content = (
1589-
"Try {{try_number}} out of {{max_tries + 1}}<br>"
1590-
"Exception:<br>{{exception_html}}<br>"
1591-
'Log: <a href="{{ti.log_url}}">Link</a><br>'
1592-
"Host: {{ti.hostname}}<br>"
1593-
'Mark success: <a href="{{ti.mark_success_url}}">Link</a><br>'
1598+
# Try to load from built-in template files
1599+
default_html_template = (
1600+
Path(__file__).parent.parent.parent.parent
1601+
/ "airflow-core"
1602+
/ "src"
1603+
/ "airflow"
1604+
/ "templates"
1605+
/ "email"
1606+
/ "task_failure_body.html"
15941607
)
1608+
if default_html_template.exists():
1609+
html_content = default_html_template.read_text()
1610+
else:
1611+
# Final fallback to inline default for backward compatibility
1612+
# For reporting purposes, we report based on 1-indexed,
1613+
# not 0-indexed lists (i.e. Try 1 instead of Try 0 for the first attempt).
1614+
html_content = (
1615+
"Try {{try_number}} out of {{max_tries + 1}}<br>"
1616+
"Exception:<br>{{exception_html}}<br>"
1617+
'Log: <a href="{{ti.log_url}}">Link</a><br>'
1618+
"Host: {{ti.hostname}}<br>"
1619+
'Mark success: <a href="{{ti.mark_success_url}}">Link</a><br>'
1620+
)
15951621

15961622
# Add exception_html to context for template rendering
15971623
import html

0 commit comments

Comments
 (0)