8000 More retries (#273) · codecov/codecov-python@07583f3 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 07583f3

Browse files
authored
More retries (#273)
* Try another retry * return * Retry all the things * Bail from the original retries * do real retries * fixes * syntax * black * Remove unnecessary var declaration
1 parent 48a5613 commit 07583f3

File tree

1 file changed

+64
-60
lines changed

1 file changed

+64
-60
lines changed

codecov/__init__.py

Lines changed: 64 additions & 60 deletions
< 57AE /tr>
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ def generate_toc(root):
287287
return str(res).strip() or ""
288288

289289

290+
def retry_upload(url, request_method, retries=3, break_codes=(200,), **kwargs):
291+
for _ in range(retries):
292+
res = request_method(url, **kwargs)
293+
if res.status_code in break_codes:
294+
return res
295+
return res
296+
297+
290298
def main(*argv, **kwargs):
291299
root = os.getcwd()
292300

@@ -1093,71 +1101,67 @@ def main(*argv, **kwargs):
10931101
reports_gzip = gzip_worker.compress(reports) + gzip_worker.flush()
10941102
write(" Compressed contents to {0} bytes".format(len(reports_gzip)))
10951103

1096-
s3 = None
1097-
trys = 0
1098-
while trys < 3:
1099-
trys += 1
1100-
if "s3" not in codecov.disable:
1101-
try:
1102-
write(" Pinging Codecov...")
1103-
res = requests.post(
1104-
"%s/upload/v4?%s" % (codecov.url, urlargs),
1104+
success = False
1105+
if "s3" not in codecov.disable:
1106+
try:
1107+
write(" Pinging Codecov...")
1108+
res = retry_upload(
1109+
"%s/upload/v4?%s" % (codecov.url, urlargs),
1110+
requests.post,
1111+
break_codes=(200, 400, 406),
1112+
verify=codecov.cacert,
1113+
headers={
1114+
"Accept": "text/plain",
1115+
"X-Reduced-Redundancy": "false",
1116+
"X-Content-Type": "application/x-gzip",
1117+
},
1118+
)
1119+
if res.status_code in (400, 406):
1120+
raise Exception(res.text)
1121+
1122+
elif res.status_code < 500:
1123+
assert res.status_code == 200
1124+
res = res.text.strip().split()
1125+
result, upload_url = res[0], res[1]
1126+
1127+
write(" Uploading to S3...")
1128+
s3 = retry_upload(
1129+
upload_url,
1130+
requests.put,
11051131
verify=codecov.cacert,
1132+
data=reports_gzip,
11061133
headers={
1107-
"Accept": "text/plain",
1108-
"X-Reduced-Redundancy": "false",
1109-
"X-Content-Type": "application/x-gzip",
1134+
"Content-Type": "application/x-gzip",
1135+
"Content-Encoding": "gzip",
11101136
},
11111137
)
1112-
if res.status_code in (400, 406):
1113-
raise Exception(res.text)
1114-
1115-
elif res.status_code < 500:
1116-
assert res.status_code == 200
1117-
res = res.text.strip().split()
1118-
result, upload_url = res[0], res[1]
1119-
1120-
write(" Uploading to S3...")
1121-
s3 = requests.put(
1122-
upload_url,
1123-
verify=codecov.cacert,
1124-
data=reports_gzip,
1125-
headers={
1126-
"Content-Type": "application/x-gzip",
1127-
"Content-Encoding": "gzip",
1128-
},
1129-
)
1130-
s3.raise_for_status()
1131-
assert s3.status_code == 200
1132-
write(" " + result)
1133-
break
1134-
else:
1135-
# try again
1136-
continue
1137-
1138-
except AssertionError:
1139-
write(" Direct to s3 failed. Using backup v2 endpoint.")
1140-
1141-
write(" Uploading to Codecov...")
1138+
s3.raise_for_status()
1139+
assert s3.status_code == 200
1140+
write(" " + result)
1141+
success = True
1142+
1143+
except AssertionError:
1144+
write(" Direct to s3 failed. Using backup v2 endpoint.")
1145+
11421146
# just incase, try traditional upload
1143-
res = requests.post(
1144-
"%s/upload/v2?%s" % (codecov.url, urlargs),
1145-
verify=codecov.cacert,
1146-
data=reports_gzip,
1147-
headers={
1148-
"Accept": "text/plain",
1149-
"Content-Type": "application/x-gzip",
1150-
"Content-Encoding": "gzip",
1151-
},
1152-
)
1153-
if res.status_code < 500:
1154-
write(" " + res.text)
1155-
res.raise_for_status()
1156-
result = res.text
1157-
return
1158-
1159-
write(" Retrying... in %ds" % (trys * 30))
1160-
sleep(trys * 30)
1147+
if not success:
1148+
write(" Uploading to Codecov...")
1149+
res = retry_upload(
1150+
"%s/upload/v2?%s" % (codecov.url, urlargs),
1151+
requests.post,
1152+
verify=codecov.cacert,
1153+
data=reports_gzip,
1154+
headers={
1155+
"Accept": "text/plain",
1156+
"Content-Type": "application/x-gzip",
1157+
"Content-Encoding": "gzip",
1158+
},
1159+
)
1160+
if res.status_code < 500:
1161+
write(" " + res.text)
1162+
res.raise_for_status()
1163+
result = res.text
1164+
return
11611165

11621166
except Exception as e:
11631167
write("Error: " + str(e))

0 commit comments

Comments
 (0)
0