emails: Improve handling of timeouts when sending.

We use the EMAIL_TIMEOUT django setting to timeout after 15s of trying
to send an email. This will nicely lead to retries in the email_senders
queue, due to the retry_send_email_failures decorator.

smtlib documentation suggests that socket.timeout can be raised as the
result of timing out, so in attempts I'm getting
smtplib.SMTPServerDisconnected. Either way, seems appropriate to add
socket.timeout to the exception that we catch.
This commit is contained in:
Mateusz Mandera 2020-07-03 13:46:48 +02:00 committed by Tim Abbott
parent 294510c68a
commit d51afcf485
2 changed files with 5 additions and 2 deletions

View File

@ -149,9 +149,10 @@ def retry_send_email_failures(
def wrapper(worker: ConcreteQueueWorker, data: Dict[str, Any]) -> None:
try:
func(worker, data)
except (smtplib.SMTPServerDisconnected, socket.gaierror, EmailNotDeliveredException):
except (smtplib.SMTPServerDisconnected, socket.gaierror, socket.timeout,
EmailNotDeliveredException) as e:
def on_failure(event: Dict[str, Any]) -> None:
logging.exception("Event %r failed", event)
logging.exception("Event %r failed due to exception %s", event, e.__class__.__name__)
retry_event(worker.queue_name, data, on_failure)

View File

@ -1106,6 +1106,8 @@ elif not EMAIL_HOST:
else:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_TIMEOUT = 15
EMAIL_HOST_PASSWORD = get_secret('email_password')
EMAIL_GATEWAY_PASSWORD = get_secret('email_gateway_password')
AUTH_LDAP_BIND_PASSWORD = get_secret('auth_ldap_bind_password', '')