Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify SmtpNotifier to accept template with defaults #36226

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions airflow/providers/smtp/notifications/templated_smtp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from __future__ import annotations

from pathlib import Path
from typing import Iterable

from airflow.configuration import conf
from airflow.providers.smtp.notifications.smtp import SmtpNotifier
from airflow.settings import SMTP_DEFAULT_TEMPLATED_HTML_CONTENT_PATH, SMTP_DEFAULT_TEMPLATED_SUBJECT

# Read once and cache - is this desirable? If users edit local settings, their changes will not take effect
# On the other hand, if we put this in the constructor, then we need to read from disk on each instantiation
SMTP_DEFAULT_TEMPLATED_HTML_CONTENT = Path(SMTP_DEFAULT_TEMPLATED_HTML_CONTENT_PATH).read_text()


class TemplatedSmtpNotifier(SmtpNotifier):
potiuk marked this conversation as resolved.
Show resolved Hide resolved
"""Templated version of the SMTP Notifier.

Accepts minimal arguments and uses a generic subject line / template that can be overridden
by replacing the variable SMTP_DEFAULT_TEMPLATED_SUBJECT or
SMTP_DEFAULT_TEMPLATED_HTML_CONTENT_PATH in airflow local settings.
"""

def __init__(
self,
to: str | Iterable[str],
**kwargs,
):
from_email = kwargs.pop("from_email", conf.get("smtp", "smtp_mail_from"))
subject = kwargs.pop("subject", SMTP_DEFAULT_TEMPLATED_SUBJECT).replace("\n", "").strip()
html_content = SMTP_DEFAULT_TEMPLATED_HTML_CONTENT
super().__init__(from_email=from_email, to=to, subject=subject, html_content=html_content, **kwargs)


send_templated_smtp_notification = TemplatedSmtpNotifier
16 changes: 16 additions & 0 deletions airflow/providers/smtp/notifications/templates/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
54 changes: 54 additions & 0 deletions airflow/providers/smtp/notifications/templates/email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width">
</head>
<body>
<table role="presentation">
{% if ti is defined %}
<tr>
<td>Try:</td>
<td>{{ ti.try_number }} of {{ ti.max_tries + 1 }}</td>
</tr>
<tr>
<td>Task State:</td>
<td>{{ ti.state }}</td>
</tr>
<tr>
<td>Host:</td>
<td>{{ ti.hostname }}</td>
</tr>
<tr>
<td>Log Link:</td>
<td><a href="{{ ti.log_url }}" style="text-decoration:underline;">{{ ti.log_url }}</a></td>
</tr>
<tr>
<td>Mark Success Link:</td>
<td><a href="{{ ti.mark_success_url }}" style="text-decoration:underline;">{{ ti.mark_success_url }}</a></td>
</tr>
{% elif slas is defined %}
<tr>
<td>Dag:</td>
<td>{{ dag.dag_id }}</td>
</tr>
<tr>
<td>Task List:</td>
<td>{{ task_list }}</td>
</tr>
<tr>
<td>Blocking Task List:</td>
<td>{{ blocking_task_list }}</td>
</tr>
<tr>
<td>SLAs:</td>
<td>{{ slas }}</td>
</tr>
<tr>
<td>Blocking TI's</td>
<td>{{ blocking_tis }}</td>
</tr>
{% endif %}
</table>
</body>
</html>
12 changes: 12 additions & 0 deletions airflow/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,18 @@ def initialize():

DAEMON_UMASK: str = conf.get("core", "daemon_umask", fallback="0o077")

SMTP_DEFAULT_TEMPLATED_SUBJECT = """
{% if ti is defined %}
DAG {{ ti.dag_id }} - Task {{ ti.task_id }} - Run ID {{ run_id }} in State {{ ti.state }}
{% elif slas is defined %}
SLA Missed for DAG {{ dag.dag_id }} - Task {{ slas[0].task_id }}
{% endif %}
"""

SMTP_DEFAULT_TEMPLATED_HTML_CONTENT_PATH = os.path.join(
os.path.dirname(__file__), "providers", "smtp", "notifications", "templates", "email.html"
)


# AIP-44: internal_api (experimental)
# This feature is not complete yet, so we disable it by default.
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,8 @@ def parse_config_files(self, *args, **kwargs) -> None:
self.package_data["airflow"].append(provider_relative_path)
# Add python_kubernetes_script.jinja2 to package data
self.package_data["airflow"].append("providers/cncf/kubernetes/python_kubernetes_script.jinja2")
# Add default email template to package data
self.package_data["airflow"].append("providers/smtp/notifications/templates/email.html")
else:
self.install_requires.extend(
[
Expand Down