Skip to content

Commit 3fecb28

Browse files
kaxilChris Fei
authored and
Chris Fei
committed
Fix Bug when passing emails for SLA
1 parent 3f2367a commit 3fecb28

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

airflow/jobs.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
SimpleDagBag,
5959
list_py_file_paths)
6060
from airflow.utils.db import create_session, provide_session
61-
from airflow.utils.email import send_email
61+
from airflow.utils.email import send_email, get_email_address_list
6262
from airflow.utils.log.logging_mixin import LoggingMixin, set_context, StreamLogWriter
6363
from airflow.utils.net import get_hostname
6464
from airflow.utils.state import State
@@ -708,16 +708,13 @@ def manage_slas(self, dag, session=None):
708708
Blocking tasks:
709709
<pre><code>{blocking_task_list}\n{bug}<code></pre>
710710
""".format(bug=asciiart.bug, **locals())
711-
emails = []
712-
for t in dag.tasks:
713-
if t.email:
714-
if isinstance(t.email, basestring):
715-
l = [t.email]
716-
elif isinstance(t.email, (list, tuple)):
717-
l = t.email
718-
for email in l:
719-
if email not in emails:
720-
emails.append(email)
711+
emails = set()
712+
for task in dag.tasks:
713+
if task.email:
714+
if isinstance(task.email, basestring):
715+
emails |= set(get_email_address_list(task.email))
716+
elif isinstance(task.email, (list, tuple)):
717+
emails |= set(task.email)
721718
if emails and len(slas):
722719
try:
723720
send_email(

airflow/utils/email.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ def send_email(to, subject, html_content,
4848
path, attr = configuration.conf.get('email', 'EMAIL_BACKEND').rsplit('.', 1)
4949
module = importlib.import_module(path)
5050
backend = getattr(module, attr)
51+
to = get_email_address_list(to)
52+
to = ", ".join(to)
53+
5154
return backend(to, subject, html_content, files=files,
5255
dryrun=dryrun, cc=cc, bcc=bcc,
5356
mime_subtype=mime_subtype, mime_charset=mime_charset, **kwargs)
@@ -129,9 +132,9 @@ def send_MIME_email(e_from, e_to, mime_msg, dryrun=False):
129132
def get_email_address_list(address_string):
130133
if isinstance(address_string, basestring):
131134
if ',' in address_string:
132-
address_string = address_string.split(',')
135+
address_string = [address.strip() for address in address_string.split(',')]
133136
elif ';' in address_string:
134-
address_string = address_string.split(';')
137+
address_string = [address.strip() for address in address_string.split(';')]
135138
else:
136139
address_string = [address_string]
137140

tests/utils/test_email.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
import unittest
21+
from airflow.utils.email import get_email_address_list
22+
23+
EMAILS = ['test1@example.com', 'test2@example.com']
24+
25+
26+
class EmailTest(unittest.TestCase):
27+
28+
def test_get_email_address_comma_sep_string(self):
29+
emails_string = 'test1@example.com, test2@example.com'
30+
31+
self.assertEquals(
32+
get_email_address_list(emails_string), EMAILS)
33+
34+
def test_get_email_address_colon_sep_string(self):
35+
emails_string = 'test1@example.com; test2@example.com'
36+
37+
self.assertEquals(
38+
get_email_address_list(emails_string), EMAILS)
39+
40+
def test_get_email_address_list(self):
41+
emails_list = ['test1@example.com', 'test2@example.com']
42+
43+
self.assertEquals(
44+
get_email_address_list(emails_list), EMAILS)

0 commit comments

Comments
 (0)