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

Don't kill the worker when it cannot connect to smtp server #1821

Merged
merged 2 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions luigi/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,20 @@ def send_email_smtp(config, sender, subject, message, recipients, image_png):

smtp_login = config.get('core', 'smtp_login', None)
smtp_password = config.get('core', 'smtp_password', None)
smtp = smtplib.SMTP(**kwargs) if not smtp_ssl else smtplib.SMTP_SSL(**kwargs)
smtp.ehlo_or_helo_if_needed()
if smtp.has_extn('starttls') and not smtp_without_tls:
smtp.starttls()
if smtp_login and smtp_password:
smtp.login(smtp_login, smtp_password)

msg_root = generate_email(sender, subject, message, recipients, image_png)
try:
smtp = smtplib.SMTP(**kwargs) if not smtp_ssl else smtplib.SMTP_SSL(**kwargs)
smtp.ehlo_or_helo_if_needed()
if smtp.has_extn('starttls') and not smtp_without_tls:
smtp.starttls()
if smtp_login and smtp_password:
smtp.login(smtp_login, smtp_password)

msg_root = generate_email(sender, subject, message, recipients, image_png)

smtp.sendmail(sender, recipients, msg_root.as_string())
smtp.sendmail(sender, recipients, msg_root.as_string())
except socket.error:
logger.error("Not able to connect to smtp server")


def send_email_ses(config, sender, subject, message, recipients, image_png):
Expand Down
35 changes: 35 additions & 0 deletions test/notifications_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from helpers import unittest
import mock
import sys
import socket

from helpers import with_config
from luigi import notifications
Expand Down Expand Up @@ -258,6 +259,40 @@ def test_sends_smtp_email_without_tls(self):
.assert_called_once_with(self.sender, self.recipients,
self.mocked_email_msg)

@with_config({"core": {"smtp_ssl": "False",
"smtp_host": "my.smtp.local",
"smtp_port": "999",
"smtp_local_hostname": "ptms",
"smtp_timeout": "1200",
"smtp_login": "Robin",
"smtp_password": "dooH",
"smtp_without_tls": "True"}})
def test_sends_smtp_email_exceptions(self):
"""
Call notificaions.send_email_smtp when it cannot connect to smtp server (socket.error)
starttls.
"""
smtp_kws = {"host": "my.smtp.local",
"port": 999,
"local_hostname": "ptms",
"timeout": 1200}

with mock.patch('smtplib.SMTP') as SMTP:
with mock.patch('luigi.notifications.generate_email') as generate_email:
SMTP.side_effect = socket.error()
generate_email.return_value \
.as_string.return_value = self.mocked_email_msg

try:
notifications.send_email_smtp(configuration.get_config(),
*self.notification_args)
except socket.error:
self.fail("send_email_smtp() raised expection unexpectedly")

SMTP.assert_called_once_with(**smtp_kws)
self.assertEqual(notifications.generate_email.called, False)
self.assertEqual(SMTP.sendemail.called, False)


class TestSendgridEmail(unittest.TestCase, NotificationFixture):
"""
Expand Down