Skip to content

Commit 975bc6b

Browse files
authored
feat: retry request if api returns a 429 when precaching nodes (#90)
We've had a couple of our (re)generation runs fail when precaching the Drupal nodes due to 429s - we might as well include a basic "retry once" attempt to help avoid this. While it's possible this could happen for any API request it should be very unlikely except for this one which is why I've not changed them. In the long-run if this becomes more regular (or requested by the Drupal team) we'll probably move to a more dedicated HTTP client implementation to handle this across the board
1 parent 7728d37 commit 975bc6b

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

scripts/precache_nodes.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import json
99
import os
10+
import time
1011
import typing
1112
from itertools import batched
1213

@@ -16,7 +17,7 @@
1617
from user_agent import user_agent
1718

1819

19-
def fetch_drupal_nodes(nids: list[str]) -> list[drupal.Node]:
20+
def fetch_drupal_nodes(nids: list[str], retry: bool = True) -> list[drupal.Node]:
2021
"""
2122
Fetches a node from drupal.org by its id
2223
"""
@@ -27,6 +28,13 @@ def fetch_drupal_nodes(nids: list[str]) -> list[drupal.Node]:
2728

2829
resp = requests.get(url, headers={'user-agent': user_agent})
2930

31+
if retry and resp.status_code == 429:
32+
seconds = int(resp.headers.get('Retry-After', 0))
33+
print(f' |* (waiting {seconds} seconds before retrying)')
34+
time.sleep(seconds)
35+
36+
return fetch_drupal_nodes(nids, retry=False)
37+
3038
if resp.status_code == 200:
3139
items = typing.cast(drupal.ApiResponse[drupal.Node], resp.json())['list']
3240

0 commit comments

Comments
 (0)