Skip to content

Commit 4af291b

Browse files
committed
change retry logic to use report status
1 parent 13a402f commit 4af291b

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

examples/client/generate_version_detail_report.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import logging
1717
import sys
1818
import time
19+
from pprint import pprint
1920

2021
logging.basicConfig(
2122
level=logging.DEBUG,
@@ -52,34 +53,39 @@ class FailedReportDownload(Exception):
5253
default=",".join(all_reports),
5354
help=f"Comma separated list (no spaces) of the reports to generate - {list(version_name_map.keys())}. Default is all reports.")
5455
parser.add_argument('--format', default='CSV', choices=["CSV"], help="Report format - only CSV available for now")
55-
parser.add_argument('-t', '--tries', default=4, type=int, help="How many times to retry downloading the report, i.e. wait for the report to be generated")
56-
parser.add_argument('-s', '--sleep_time', default=5, type=int, help="The amount of time to sleep in-between (re-)tries to download the report")
56+
parser.add_argument('-t', '--tries', default=30, type=int, help="How many times to retry downloading the report, i.e. wait for the report to be generated")
57+
parser.add_argument('-s', '--sleep_time', default=10, type=int, help="The amount of time to sleep in-between (re-)tries to download the report")
5758
parser.add_argument('--no-verify', dest='verify', action='store_false', help="disable TLS certificate verification")
5859

5960
args = parser.parse_args()
6061

6162
def download_report(bd_client, location, filename, retries=args.tries):
6263
report_id = location.split("/")[-1]
63-
download_url = f"{bd_client.base_url}api/reports/{report_id}"
64+
base_url = bd_client.base_url if bd_client.base_url.endswith("/") else bd_client.base_url + "/"
65+
download_url = f"{base_url}api/reports/{report_id}"
66+
67+
logging.info(f"Retrieving report list for {location}")
6468

6569
if retries:
66-
print("Retrieving generated report from {}".format(location))
67-
response = bd.session.get(download_url, headers={'Content-Type': 'application/zip', 'Accept':'application/zip'})
68-
if response.status_code == 200:
69-
with open(filename, "wb") as f:
70-
f.write(response.content)
71-
print(f"Successfully downloaded zip file to {filename} for report {report_id}")
72-
else:
73-
print(f"Failed to retrieve report {report_id}")
74-
print("Probably not ready yet, waiting 5 seconds then retrying...")
75-
time.sleep(args.sleep_time)
70+
response = bd_client.session.get(location)
71+
report_status = response.json().get('status', 'Not Ready')
72+
if response.status_code == 200 and report_status == 'COMPLETED':
73+
response = bd.session.get(download_url, headers={'Content-Type': 'application/zip', 'Accept':'application/zip'})
74+
if response.status_code == 200:
75+
with open(filename, "wb") as f:
76+
f.write(response.content)
77+
logging.info(f"Successfully downloaded zip file to {filename} for report {report_id}")
78+
else:
79+
logging.error(f"Failed to download report")
80+
else:
7681
retries -= 1
77-
download_report(location, filename, retries)
82+
logging.debug(f"Failed to retrieve report {report_id}, report status: {report_status}")
83+
logging.debug(f"Will retry {retries} more times. Sleeping for {args.sleep_time} second(s)")
84+
time.sleep(args.sleep_time)
85+
download_report(bd_client, location, filename, retries)
7886
else:
7987
raise FailedReportDownload(f"Failed to retrieve report {report_id} after multiple retries")
8088

81-
# hub = HubInstance()
82-
8389
with open(args.token_file, 'r') as tf:
8490
access_token = tf.readline().strip()
8591

0 commit comments

Comments
 (0)