Skip to content

Commit

Permalink
bugfix: worker dies on smtp connection errors (#1821)
Browse files Browse the repository at this point in the history
Catch socket.error exception when trying to connect smtp server in error email notification.
  • Loading branch information
interskh authored and Tarrasch committed Aug 23, 2016
1 parent ae50c79 commit c0c4d5c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
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

0 comments on commit c0c4d5c

Please sign in to comment.