Skip to content

Commit f4d07b0

Browse files
authored
Add retry to llms script (#589)
* Add retry to script * Use linear backoff
1 parent 2be4a89 commit f4d07b0

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

scripts/generate_llms.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import re
88
import requests
9+
import time
910

1011
# Set the base directory to the root of docs
1112
base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
@@ -115,7 +116,7 @@ def fetch_local_snippet(snippet_ref, snippet_directory):
115116

116117
return snippet_content.strip()
117118

118-
def fetch_remote_snippet(snippet_ref, yaml_data):
119+
def fetch_remote_snippet(snippet_ref, yaml_data, max_retries=3, backoff_factor=2):
119120
# Match URL with optional line range (start:end)
120121
match = re.match(r'^(https?://[^:]+)(?::(\d+))?(?::(\d+))?$', snippet_ref)
121122

@@ -129,30 +130,36 @@ def fetch_remote_snippet(snippet_ref, yaml_data):
129130

130131
# Resolve any template placeholders using the yaml_data
131132
url = resolve_placeholders(url, yaml_data)
132-
#print(f"{url}")
133-
#print(f"{line_start}")
134-
#print(f"{line_end}")
135-
136133

137134
# Skip URLs containing unresolved template placeholders
138135
if "{{" in url:
139136
print(f"Skipping snippet with unresolved template: {url}")
140137
return f"Unresolved template: {url}"
141138

142-
try:
143-
response = requests.get(url)
144-
response.raise_for_status()
145-
snippet_content = response.text
146-
147-
# Extract specific lines if requested
148-
if line_start is not None and line_end is not None:
149-
lines = snippet_content.split('\n')
150-
snippet_content = '\n'.join(lines[line_start-1:line_end])
151-
152-
return snippet_content.strip()
153-
except requests.RequestException as e:
154-
print(f"Failed to fetch snippet from {url}: {e}")
155-
return f"Error fetching snippet from {url}"
139+
for attempt in range(1, max_retries + 1):
140+
try:
141+
response = requests.get(url)
142+
if response.status_code == 429:
143+
wait = backoff_factor * attempt
144+
print(f"429 Too Many Requests. Retrying in {wait} seconds...")
145+
time.sleep(wait)
146+
continue
147+
response.raise_for_status()
148+
snippet_content = response.text
149+
150+
# Extract specific lines if requested
151+
if line_start is not None and line_end is not None:
152+
lines = snippet_content.split('\n')
153+
snippet_content = '\n'.join(lines[line_start - 1:line_end])
154+
155+
return snippet_content.strip()
156+
157+
except requests.RequestException as e:
158+
print(f"Attempt {attempt} failed: {e}")
159+
if attempt == max_retries:
160+
return f"Error fetching snippet from {url}"
161+
162+
return f"Failed to fetch snippet after {max_retries} attempts"
156163

157164
def resolve_placeholders(text, data):
158165
# Replace placeholders like {{dependencies.repositories.asset_transfer_api.version}}

0 commit comments

Comments
 (0)