Skip to content

Commit 03ff93e

Browse files
author
Max Wang
committed
improvments to retry
1 parent 72096e8 commit 03ff93e

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

examples/advanced/file_upload.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,32 @@ def generate_test_pdf(size_mb: int = 10) -> Path:
153153
return test_file
154154

155155

156-
def backoff(op, *, delays=(0, 2, 5, 10)):
156+
def backoff(op, *, delays=(0, 2, 5, 10, 20, 20)):
157157
last = None
158+
total_delay = 0
159+
attempts = 0
158160
for d in delays:
159161
if d:
160162
time.sleep(d)
163+
total_delay += d
164+
attempts += 1
161165
try:
162-
return op()
166+
result = op()
167+
if attempts > 1:
168+
retry_count = attempts - 1
169+
print(
170+
f" ↺ Backoff succeeded after {retry_count} retry(s); waited {total_delay}s total."
171+
)
172+
return result
163173
except Exception as ex: # noqa: BLE001
164174
last = ex
165175
continue
166176
if last:
177+
if attempts:
178+
retry_count = max(attempts - 1, 0)
179+
print(
180+
f" ⚠ Backoff exhausted after {retry_count} retry(s); waited {total_delay}s total."
181+
)
167182
raise last
168183

169184

examples/advanced/walkthrough.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from enum import IntEnum
2424
from azure.identity import InteractiveBrowserCredential
2525
from PowerPlatform.Dataverse.client import DataverseClient
26+
from PowerPlatform.Dataverse.core.errors import MetadataError
2627
import requests
2728

2829

@@ -38,17 +39,32 @@ class Priority(IntEnum):
3839
HIGH = 3
3940

4041

41-
def backoff(op, *, delays=(0, 2, 5, 10)):
42+
def backoff(op, *, delays=(0, 2, 5, 10, 20, 20)):
4243
last = None
44+
total_delay = 0
45+
attempts = 0
4346
for d in delays:
4447
if d:
4548
time.sleep(d)
49+
total_delay += d
50+
attempts += 1
4651
try:
47-
return op()
52+
result = op()
53+
if attempts > 1:
54+
retry_count = attempts - 1
55+
print(
56+
f" ↺ Backoff succeeded after {retry_count} retry(s); waited {total_delay}s total."
57+
)
58+
return result
4859
except Exception as ex: # noqa: BLE001
4960
last = ex
5061
continue
5162
if last:
63+
if attempts:
64+
retry_count = max(attempts - 1, 0)
65+
print(
66+
f" ⚠ Backoff exhausted after {retry_count} retry(s); waited {total_delay}s total."
67+
)
5268
raise last
5369

5470

@@ -183,7 +199,8 @@ def main():
183199
# Multiple read with filter
184200
log_call(f"client.get('{table_name}', filter='new_quantity gt 5')")
185201
all_records = []
186-
for page in client.get(table_name, filter="new_quantity gt 5"):
202+
records_iterator = backoff(lambda: client.get(table_name, filter="new_quantity gt 5"))
203+
for page in records_iterator:
187204
all_records.extend(page)
188205
print(f"✓ Found {len(all_records)} records with new_quantity > 5")
189206
for rec in all_records:
@@ -232,7 +249,8 @@ def main():
232249
# Query with paging
233250
log_call(f"client.get('{table_name}', page_size=5)")
234251
print("Fetching records with page_size=5...")
235-
for page_num, page in enumerate(client.get(table_name, orderby=["new_Quantity"], page_size=5), start=1):
252+
paging_iterator = backoff(lambda: client.get(table_name, orderby=["new_Quantity"], page_size=5))
253+
for page_num, page in enumerate(paging_iterator, start=1):
236254
record_ids = [r.get("new_walkthroughdemoid")[:8] + "..." for r in page]
237255
print(f" Page {page_num}: {len(page)} records - IDs: {record_ids}")
238256

@@ -321,7 +339,7 @@ def main():
321339
print(f"✓ Deleted table: {table_name}")
322340
except Exception as ex: # noqa: BLE001
323341
code = getattr(getattr(ex, "response", None), "status_code", None)
324-
if isinstance(ex, requests.exceptions.HTTPError) and code == 404:
342+
if (isinstance(ex, (requests.exceptions.HTTPError, MetadataError)) and code == 404):
325343
print(f"✓ Table removed: {table_name}")
326344
else:
327345
raise

0 commit comments

Comments
 (0)