potiuk commented on code in PR #69877:
URL: https://github.com/apache/airflow/pull/69877#discussion_r3614014577
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -1998,20 +2002,39 @@ def _send_error_email_notification(
error: BaseException | str | None,
log: Logger,
) -> None:
- """Send email notification for task errors using SmtpNotifier."""
- try:
- from airflow.providers.smtp.notifications.smtp import SmtpNotifier
- except ImportError:
- log.error(
- "Failed to send task failure or retry email notification: "
- "`apache-airflow-providers-smtp` is not installed. "
- "Install this provider to enable email notifications."
- )
- return
+ """
+ Send email notification for task errors through the configured email
backend.
+
+ A non-default ``[email] email_backend`` (an SES, SendGrid or org-internal
callable with the
+ ``airflow.utils.email.send_email`` signature) is wrapped in
+
:class:`~airflow.sdk.execution_time.email_backend._LegacyEmailBackendNotifier`;
otherwise the
+ default :class:`~airflow.providers.smtp.notifications.smtp.SmtpNotifier`
is used.
+ Both the worker task-runner path (:func:`finalize`) and the DAG-processor
callback path
+ (``_execute_email_callbacks``) funnel through this function, so the
resolved backend is used
+ consistently regardless of how the task failed.
+ """
if not task.email:
return
+ email_backend = conf.get("email", "email_backend",
fallback=DEFAULT_EMAIL_BACKEND)
+ notifier_description = "SmtpNotifier"
+
+ if email_backend and email_backend != DEFAULT_EMAIL_BACKEND:
+ notifier_class: type = _LegacyEmailBackendNotifier
Review Comment:
Tried it — doesn't typecheck:
```
error: Unexpected keyword argument "to" for "BaseNotifier" [call-arg]
note: "BaseNotifier" defined in "airflow.sdk.bases.notifier"
```
`BaseNotifier.__init__` only takes `context`; the email kwargs live on the
subclasses.
Added a Protocol instead so the call site is still typed:
```python
class _ErrorEmailNotifier(Protocol):
def __call__(self, to, from_email=..., subject=..., html_content=...) ->
BaseNotifier: ...
```
Both `SmtpNotifier` and `_LegacyEmailBackendNotifier` satisfy it; mypy
passes.
---
Drafted-by: Claude Code (Opus 4.8); reviewed by @potiuk before posting
##########
task-sdk/src/airflow/sdk/execution_time/email_backend.py:
##########
@@ -0,0 +1,75 @@
+# 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 typing import TYPE_CHECKING, Any
+
+from airflow.sdk.bases.notifier import BaseNotifier
+from airflow.sdk.configuration import conf
+from airflow.sdk.exceptions import AirflowConfigException
+
+if TYPE_CHECKING:
+ from collections.abc import Iterable
+
+ from airflow.sdk.definitions.context import Context
+
+DEFAULT_EMAIL_BACKEND = "airflow.utils.email.send_email_smtp"
+
+
+class _LegacyEmailBackendNotifier(BaseNotifier):
+ """
+ Adapter that exposes a legacy ``[email] email_backend`` callable as a
notifier.
+
+ Before failure and retry alerts were routed through ``BaseNotifier``
subclasses, deployments
+ configured them through ``[email] email_backend`` -- a callable with the
+ ``airflow.utils.email.send_email`` signature, such as the Amazon SES or
SendGrid senders.
+ This adapter renders the standard email fields like any notifier, then
loads and calls the
+ configured backend, so existing ``email_backend`` setups keep working
unchanged.
+
+ The backend is resolved from config at notify time rather than imported
statically, keeping
+ the Task SDK free of a hard dependency on ``airflow.utils.email`` (which
lives in
+ ``airflow-core``).
+ """
+
+ template_fields = ("to", "from_email", "subject", "html_content")
+
+ def __init__(
+ self,
+ to: str | Iterable[str],
+ from_email: str | None = None,
+ subject: str | None = None,
+ html_content: str | None = None,
+ **kwargs: Any,
+ ) -> None:
+ super().__init__()
+ self.to = to
+ self.from_email = from_email
+ self.subject = subject
+ self.html_content = html_content
+
+ def notify(self, context: Context) -> None:
+ backend = conf.getimport("email", "email_backend",
fallback=DEFAULT_EMAIL_BACKEND)
Review Comment:
It does today — `[email] email_backend` defaults to
`airflow.utils.email.send_email_smtp` in config.yml, and `conf.get` returns it
fine without the fallback. I checked.
Keeping it deliberately, though. The direction here is to remove email
settings from core, and task-sdk only sees that default because it currently
reads core's config.yml under a temporary TODO — it's meant to get its own. The
fallback is what keeps this working when either of those lands.
---
Drafted-by: Claude Code (Opus 4.8); reviewed by @potiuk before posting
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
Review Comment:
Good catch — added `test_execute_email_callbacks_uses_custom_email_backend`.
The gap was real: every existing test in that class patches
`_send_error_email_notification`, so none of them reached backend selection at
all. The new one lets it run for real and asserts the custom backend is called
and `SmtpNotifier` isn't. Verified it fails with the fix reverted.
---
Drafted-by: Claude Code (Opus 4.8); reviewed by @potiuk before posting
--
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]