Skip to content

Commit 273e549

Browse files
author
Glenn Snyder
committed
fixing start_time to look at updatedAt as opposed to createdAt
1 parent 4e1aa42 commit 273e549

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

examples/wait_for_scan_results.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
from blackduck.HubRestApi import HubInstance, object_id
2020

2121
class ScanMonitor(object):
22+
SUCCESS = 0
23+
FAILURE = 1
24+
TIMED_OUT = 2
25+
2226
def __init__(self, hub, scan_location_name, max_checks=10, check_delay=5, snippet_scan=False, start_time = None):
2327
self.hub = hub
2428
self.scan_location_name = scan_location_name
@@ -48,24 +52,28 @@ def wait_for_scan_completion(self):
4852
while remaining_checks > 0:
4953
scans = self.hub.execute_get(scans_url).json().get('items', [])
5054

51-
newer_scans = list(filter(lambda s: arrow.get(s['createdAt']) > self.start_time, scans))
55+
newer_scans = list(filter(lambda s: arrow.get(s['updatedAt']) > self.start_time, scans))
5256
logging.debug(f"Found {len(newer_scans)} newer scans")
5357

5458
expected_scans_seen = len(newer_scans) == number_expected_newer_scans
5559
logging.debug(f"expected_scans_seen: {expected_scans_seen}")
5660

57-
if expected_scans_seen and all([s['status'] == 'COMPLETE' for s in newer_scans]):
61+
if expected_scans_seen and all([s['status'] in ['COMPLETE', 'FAILURE'] for s in newer_scans]):
5862
logging.info("Scans have finished processing")
59-
break
63+
if all([s['status'] == 'COMPLETE' for s in newer_scans]):
64+
return ScanMonitor.SUCCESS
65+
else:
66+
return ScanMonitor.FAILURE
6067
else:
6168
remaining_checks -= 1
6269
logging.debug(f"Sleeping for {self.check_delay} seconds before checking again. {remaining_checks} remaining")
6370
time.sleep(self.check_delay)
6471

65-
# TODO: Check for success/failure and return appropriate exit status?
72+
return ScanMonitor.TIMED_OUT
73+
6674

6775
if __name__ == "__main__":
68-
parser = argparse.ArgumentParser("Wait for scan processing to complete for a given code (scan) location/name")
76+
parser = argparse.ArgumentParser("Wait for scan processing to complete for a given code (scan) location/name and provide an exit status - 0 successful, 1 failed, and 2 timed-out")
6977
parser.add_argument("scan_location_name", help="The scan location name")
7078
parser.add_argument('-m', '--max_checks', type=int, default=10, help="Set the maximum number of checks before quitting")
7179
parser.add_argument('-t', '--time_between_checks', type=int, default=5, help="Set the number of seconds to wait in-between checks")

0 commit comments

Comments
 (0)