pmclanahan/django-celery-email

conn.open needs try/catch to retry

oppianmatt opened this issue · 0 comments

When sending the emails need to also try/catch the conn.open part so if the connection open fails it can retry all the messages.

...
    conn = get_connection(backend=settings.CELERY_EMAIL_BACKEND, **combined_kwargs)
    conn.open()

    messages_sent = 0

    for message in messages:
        try:
            sent = conn.send_messages([dict_to_email(message)])
            if sent is not None:
                messages_sent += sent
            logger.debug("Successfully sent email message to %r.", message['to'])
        except Exception as e:
            # Not expecting any specific kind of exception here because it
            # could be any number of things, depending on the backend
            logger.warning("Failed to send email message to %r, retrying. (%r)",
                           message['to'], e)
            send_emails.retry([[message], combined_kwargs], exc=e, throw=False)

    conn.close()

And if there is a SoftTimeLimitExceeded error, it should try cleanup and exit