Skip to content

Commit 522451e

Browse files
authored
Retry callbacks on 429 'too many requests' code (#2155)
* Handle 429 'too many requests' differently with callbacks by retrying later * Added comments on code intent. * Run formatting on test file
1 parent f664568 commit 522451e

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

app/celery/service_callback_tasks.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,23 @@ def _send_data_to_service_callback_api(self, data, service_callback_url, token,
6565
data=json.dumps(data),
6666
headers={
6767
"Content-Type": "application/json",
68-
"Authorization": "Bearer {}".format(token),
68+
"Authorization": f"Bearer {token}",
6969
},
7070
timeout=5,
7171
)
7272
current_app.logger.info(
73-
"{} sending {} to {}, response {}".format(
74-
function_name,
75-
notification_id,
76-
service_callback_url,
77-
response.status_code,
78-
)
73+
f"{function_name} sending {notification_id} to {service_callback_url}, response {response.status_code}"
7974
)
8075
response.raise_for_status()
8176
except RequestException as e:
8277
current_app.logger.warning(
83-
"{} request failed for notification_id: {} and url: {}. exc: {}".format(
84-
function_name, notification_id, service_callback_url, e
85-
)
78+
f"{function_name} request failed for notification_id: {notification_id} and url: {service_callback_url}. exc: {e}"
8679
)
87-
if not isinstance(e, HTTPError) or e.response.status_code >= 500:
80+
# Retry if the response status code is server-side or 429 (too many requests).
81+
if not isinstance(e, HTTPError) or e.response.status_code >= 500 or e.response.status_code == 429:
8882
try:
8983
self.retry(queue=QueueNames.CALLBACKS_RETRY)
9084
except self.MaxRetriesExceededError:
9185
current_app.logger.warning(
92-
"Retry: {} has retried the max num of times for callback url {} and notification_id: {}".format(
93-
function_name, service_callback_url, notification_id
94-
)
86+
"Retry: {function_name} has retried the max num of times for callback url {service_callback_url} and notification_id: {notification_id}"
9587
)

tests/app/celery/test_service_callback_tasks.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ def test_send_complaint_to_service_posts_https_request_to_service_with_signed_da
9090

9191

9292
@pytest.mark.parametrize("notification_type", ["email", "letter", "sms"])
93-
def test__send_data_to_service_callback_api_retries_if_request_returns_500_with_signed_data(
94-
notify_db_session, mocker, notification_type
93+
@pytest.mark.parametrize("status_code", [429, 500, 503])
94+
def test__send_data_to_service_callback_api_retries_if_request_returns_error_code_with_signed_data(
95+
notify_db_session, mocker, notification_type, status_code
9596
):
9697
callback_api, template = _set_up_test_data(notification_type, "delivery_status")
9798
datestr = datetime(2017, 6, 20)
@@ -107,7 +108,7 @@ def test__send_data_to_service_callback_api_retries_if_request_returns_500_with_
107108
signed_data = _set_up_data_for_status_update(callback_api, notification)
108109
mocked = mocker.patch("app.celery.service_callback_tasks.send_delivery_status_to_service.retry")
109110
with requests_mock.Mocker() as request_mock:
110-
request_mock.post(callback_api.url, json={}, status_code=500)
111+
request_mock.post(callback_api.url, json={}, status_code=status_code)
111112
send_delivery_status_to_service(notification.id, signed_status_update=signed_data)
112113

113114
assert mocked.call_count == 1

0 commit comments

Comments
 (0)