Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit 1729c5a

Browse files
Merge pull request #22 from ShorensteinCenter/devel
[Bugfix, N/A] Added exponential backoff to async api requests for other error types
2 parents 7d0c889 + 9fdf7e2 commit 1729c5a

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ These instructions will get you a copy of the project up and running on your loc
3636
* `SES_REGION_NAME` - AWS Simple Email Service region.
3737
* `SES_DEFAULT_EMAIL_SOURCE` - The default email address to send from. This email needs to be verified by SES and active outside the SES sandbox.
3838
* `SES_CONFIGURATION_SET` - SES Configuration set for tracking opens/clicks/etc. Optional.
39-
* `NO_PROXY` - We use proxies to distribute our MailChimp requests across IP addresses. Set this variable to `True` in order to disable proxying, or modify the `enable_proxy` method in `app/lists.py` according to your proxy setup.
39+
* `NO_PROXY` - We use proxies to distribute our MailChimp requests across IP addresses. Set this variable to `True` in order to disable proxying, or modify the `enable_proxy` method in `app/lists.py` according to your proxy configuration.
40+
* `NO_EMAIL` - Suppresses sending of emails. Note that SES is still required, emails will just be logged rather than sent. Optional.
4041
* `ADMIN_EMAIL` - Email address to send error emails to. Optional.
4142

4243
##### Upgrade the database

app/emails.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""This module contains functions associated with sending email."""
2+
import os
3+
import logging
24
import boto3
35
from flask import render_template
46
from app import app
@@ -32,6 +34,13 @@ def send_email(subject, recipients, template_name, template_context, # pylint: d
3234

3335
with app.app_context():
3436
html = render_template(template_name, **template_context)
37+
if os.environ.get('NO_EMAIL'):
38+
logger = logging.getLogger(__name__)
39+
logger.warning('NO_EMAIL environment variable set. '
40+
'Suppressing an email with the following params: '
41+
'Sender: %s. Recipients: %s. Subject: %s.',
42+
sender, recipients, subject)
43+
return
3544
ses.send_email(
3645
Source=sender,
3746
Destination={'ToAddresses': recipients},

app/lists.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
from pandas.io.json import json_normalize
1313
import numpy as np
1414
from aiohttp import ClientSession, BasicAuth
15-
from aiohttp.client_exceptions import (
16-
ClientHttpProxyError, ServerDisconnectedError)
1715
import iso8601
1816
from celery.utils.log import get_task_logger
1917

@@ -247,9 +245,7 @@ async def make_async_request(self, url, params, session, retry=0):
247245
error_details)
248246

249247
# Catch proxy problems as well as potential asyncio timeouts/disconnects
250-
except (asyncio.TimeoutError, # pylint: disable=invalid-name
251-
ClientHttpProxyError,
252-
ServerDisconnectedError) as e:
248+
except Exception as e: # pylint: disable=invalid-name
253249

254250
exception_type = type(e).__name__
255251

@@ -261,10 +257,15 @@ async def make_async_request(self, url, params, session, retry=0):
261257
self.logger.warning('Server disconnected! URL: %s. API key: '
262258
'%s.', url, self.api_key)
263259

264-
else:
260+
elif exception_type == 'TimeoutError':
265261
self.logger.warning('Asyncio request timed out! URL: %s. '
266262
'API key: %s.', url, self.api_key)
267263

264+
else:
265+
self.logger.warning('An unforseen error type occurred. '
266+
'Error type: %s. URL: %s. API Key: %s.',
267+
exception_type, url, self.api_key)
268+
268269
# Retry if we haven't already retried a few times
269270
if retry < self.MAX_RETRIES:
270271

0 commit comments

Comments
 (0)