From 2e85ec653f03741dab953b0845281535ec8abd86 Mon Sep 17 00:00:00 2001 From: Pelle Nilsson Date: Sun, 18 Feb 2018 16:48:08 +0100 Subject: [PATCH] Make SMTP connections thread safe --- isso/ext/notifications.py | 55 +++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/isso/ext/notifications.py b/isso/ext/notifications.py index 38fa173..0f71539 100644 --- a/isso/ext/notifications.py +++ b/isso/ext/notifications.py @@ -31,31 +31,10 @@ else: from _thread import start_new_thread -class SMTP(object): +class SMTPConnection(object): - def __init__(self, isso): - - self.isso = isso - self.conf = isso.conf.section("smtp") - - # test SMTP connectivity - try: - with self: - logger.info("connected to SMTP server") - except (socket.error, smtplib.SMTPException): - logger.exception("unable to connect to SMTP server") - - if uwsgi: - def spooler(args): - try: - self._sendmail(args[b"subject"].decode("utf-8"), - args["body"].decode("utf-8")) - except smtplib.SMTPConnectError: - return uwsgi.SPOOL_RETRY - else: - return uwsgi.SPOOL_OK - - uwsgi.spooler = spooler + def __init__(self, conf): + self.conf = conf def __enter__(self): klass = (smtplib.SMTP_SSL if self.conf.get( @@ -85,6 +64,32 @@ class SMTP(object): def __exit__(self, exc_type, exc_value, traceback): self.client.quit() +class SMTP(object): + + def __init__(self, isso): + + self.isso = isso + self.conf = isso.conf.section("smtp") + + # test SMTP connectivity + try: + with SMTPConnection(self.conf): + logger.info("connected to SMTP server") + except (socket.error, smtplib.SMTPException): + logger.exception("unable to connect to SMTP server") + + if uwsgi: + def spooler(args): + try: + self._sendmail(args[b"subject"].decode("utf-8"), + args["body"].decode("utf-8")) + except smtplib.SMTPConnectError: + return uwsgi.SPOOL_RETRY + else: + return uwsgi.SPOOL_OK + + uwsgi.spooler = spooler + def __iter__(self): yield "comments.new:after-save", self.notify @@ -153,7 +158,7 @@ class SMTP(object): msg['Date'] = formatdate(localtime=True) msg['Subject'] = Header(subject, 'utf-8') - with self as con: + with SMTPConnection(self.conf) as con: con.sendmail(from_addr, to_addr, msg.as_string()) def _retry(self, subject, body, to):