From b2449c4d8961e1a312451bcb3903df59d308df5c Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 16 Nov 2020 10:01:56 -0500 Subject: [PATCH] Trying to catch the too many messages error, and just stop rather than failing on all subsequent samples, so that it just keeps trying. --- communicator/api/admin.py | 19 ++++++++++++++++++- communicator/services/notification_service.py | 8 +------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/communicator/api/admin.py b/communicator/api/admin.py index d00cbc8..03355a2 100644 --- a/communicator/api/admin.py +++ b/communicator/api/admin.py @@ -1,3 +1,5 @@ +import smtplib + from communicator import db, app, executor from communicator.models import Sample from communicator.models.invitation import Invitation @@ -87,12 +89,27 @@ def _notify_by_email(file_name=None, retry=False): count += 1 sample.email_notified = True db.session.add(Notification(type=EMAIL_TYPE, sample=sample, successful=True)) + except smtplib.SMTPServerDisconnected as de: + app.logger.error("Database connection terminated, stopping for now.", exc_info=True) + break + except smtplib.SMTPResponseException as re: + if re.smtp_code == 451: + app.logger.error("Too many messages error from SMTP Service, stopping for now.", exc_info=True) + break + else: + app.logger.error(f'An exception happened in EmailService sending to {sample.email} ', exc_info=True) + app.logger.error(str(e)) + db.session.add(Notification(type=EMAIL_TYPE, sample=sample, successful=False, + error_message=str(e))) except Exception as e: + app.logger.error(f'An exception happened in EmailService sending to {sample.email} ', exc_info=True) + app.logger.error(str(e)) db.session.add(Notification(type=EMAIL_TYPE, sample=sample, successful=False, error_message=str(e))) db.session.commit() sleep(0.5) - if count > 190: + if count > 190: # At 2 a second, it should take 80 seconds or around a minute and 1/2 to send out a set. + app.logger.info("Reached the max 190 messages, stopping for now.") break diff --git a/communicator/services/notification_service.py b/communicator/services/notification_service.py index 17a6463..11e426c 100644 --- a/communicator/services/notification_service.py +++ b/communicator/services/notification_service.py @@ -182,13 +182,7 @@ class NotificationService(object): return all_recipients = recipients + bcc - - try: - self.email_server.sendmail(sender, all_recipients, msgRoot.as_bytes()) - except Exception as e: - app.logger.error('An exception happened in EmailService', exc_info=True) - app.logger.error(str(e)) - raise CommError(5000, f"failed to send email to {', '.join(recipients)}", e) + self.email_server.sendmail(sender, all_recipients, msgRoot.as_bytes()) def is_reasonable_hour_for_text_messages(self): """Where 'reasaonable' is between 8am and 10pm. """