Skip to content

Commit 28ee839

Browse files
committed
added support for 503 and general API failures
1 parent efe0a4e commit 28ee839

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.wfp
22
*-result.json
3+
*-res.json
34
*.pem
45
.vscode/
56
gitee_com_*
@@ -15,3 +16,9 @@ venv/
1516
.idea
1617
src/scanoss/data/build_date.txt
1718
bad*.txt
19+
*.csv
20+
*.json
21+
22+
*.tar
23+
*.tgz
24+
*.gz

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Upcoming changes...
1111

12+
## [1.3.3] - 2023-01-06
13+
### Added
14+
- Added support for handling 503 service unavailable responses
15+
- Added latest SPDX license definitions (2.2.7)
16+
1217
## [1.3.2] - 2022-12-28
1318
### Added
1419
- Added `x-request-id` to all scanning requests
@@ -174,3 +179,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
174179
[1.3.0]: https://github.com/scanoss/scanoss.py/compare/v1.2.3...v1.3.0
175180
[1.3.1]: https://github.com/scanoss/scanoss.py/compare/v1.3.0...v1.3.1
176181
[1.3.2]: https://github.com/scanoss/scanoss.py/compare/v1.3.1...v1.3.2
182+
[1.3.3]: https://github.com/scanoss/scanoss.py/compare/v1.3.2...v1.3.3

CONTRIBUTING.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ Want to submit a pull request? Great! But please follow some basic rules:
2121
- If you are changing a source file please make sure that you only include in the changeset the lines changed by you (beware of your editor reformatting the file)
2222
- If you are adding functionality, please write a unit test.
2323

24-
When reviewing your pull request, we will follow a checklist similar to this one: https://gist.github.com/audreyr/4feef90445b9680475f2
25-
2624
### Licensing
2725

2826
The SCANOSS Platform is released under the GPL-2.0 license. If you wish to contribute, you must accept that you are aware of the license under which the project is released, and that your contribution will be released under the same license. Sometimes the GPL-2.0 license is incompatible with other licenses chosen by other projects. Therefore, you must accept that your contribution can also be released under the MIT license, which is the license we choose for those situations. Unless you expressly request otherwise, we may use your name, email address, username or URL for your attribution notice text. The submission of your contribution implies that you agree with these licensing terms.

src/scanoss/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
THE SOFTWARE.
2323
"""
2424

25-
__version__ = '1.3.2'
25+
__version__ = '1.3.3'

src/scanoss/scanner.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ def scan_folder(self, scan_dir: str) -> bool:
360360
scan_started = False
361361
for root, dirs, files in os.walk(scan_dir):
362362
self.print_trace(f'U Root: {root}, Dirs: {dirs}, Files {files}')
363+
if self.threaded_scan and self.threaded_scan.stop_scanning():
364+
self.print_stderr('Warning: Aborting fingerprinting as the scanning service is not available.')
365+
break
363366
dirs[:] = self.__filter_dirs(dirs) # Strip out unwanted directories
364367
filtered_files = self.__filter_files(files) # Strip out unwanted files
365368
self.print_debug(f'F Root: {root}, Dirs: {dirs}, Files {filtered_files}')

src/scanoss/scanossapi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ def scan(self, wfp: str, context: str = None, scan_id: int = None):
165165
else:
166166
self.print_stderr(f'Warning: No response received from {self.url}. Retrying...')
167167
time.sleep(5)
168+
elif r.status_code == 503: # Service limits have most likely been reached
169+
self.print_stderr(f'ERROR: SCANOSS API rejected the scan request ({request_id}) due to '
170+
f'service limits being exceeded')
171+
self.print_stderr(f'ERROR: Details: {r.text.strip()}')
172+
raise Exception(f"ERROR: {r.status_code} - The SCANOSS API request ({request_id}) rejected "
173+
f"for {self.url} due to service limits being exceeded.")
168174
elif r.status_code >= 400:
169175
if retry > 5: # No response 5 or more times, fail
170176
self.save_bad_req_wfp(scan_files, request_id, scan_id)

src/scanoss/threadedscanning.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ def __init__(self, scanapi :ScanossApi, debug: bool = False, trace: bool = False
6666
self._bar_count = 0
6767
self._errors = False
6868
self._lock = threading.Lock()
69-
self._stop_event = threading.Event()
69+
self._stop_event = threading.Event() # Control when scanning threads should terminate
70+
self._stop_scanning = threading.Event() # Control if the parent process should abort scanning
7071
self._threads = []
7172
if nb_threads > MAX_ALLOWED_THREADS:
7273
self.print_msg(f'Warning: Requested threads too large: {nb_threads}. Reducing to {MAX_ALLOWED_THREADS}')
@@ -133,6 +134,12 @@ def queue_add(self, wfp: str) -> None:
133134
def get_queue_size(self) -> int:
134135
return self.inputs.qsize()
135136

137+
def stop_scanning(self) -> bool:
138+
"""
139+
Check if we should keep scanning or not
140+
"""
141+
return self._stop_scanning.is_set()
142+
136143
@property
137144
def responses(self) -> List[Dict]:
138145
"""
@@ -186,28 +193,34 @@ def worker_post(self) -> None:
186193
"""
187194
current_thread = threading.get_ident()
188195
self.print_trace(f'Starting worker {current_thread}...')
196+
api_error = False
189197
while not self._stop_event.is_set():
190198
wfp = None
191199
if not self.inputs.empty(): # Only try to get a message if there is one on the queue
192200
try:
193201
wfp = self.inputs.get(timeout=5)
194-
self.print_trace(f'Processing input request ({current_thread})...')
195-
count = self.__count_files_in_wfp(wfp)
196-
if wfp is None or wfp == '':
197-
self.print_stderr(f'Warning: Empty WFP in request input: {wfp}')
198-
resp = self.scanapi.scan(wfp, scan_id=current_thread)
199-
if resp:
200-
self.output.put(resp) # Store the output response to later collection
201-
self.update_bar(count)
202-
self.inputs.task_done()
203-
self.print_trace(f'Request complete ({current_thread}).')
202+
if api_error: # API error encountered, so stop processing anymore requests
203+
self.inputs.task_done() # remove request from the queue
204+
else:
205+
self.print_trace(f'Processing input request ({current_thread})...')
206+
count = self.__count_files_in_wfp(wfp)
207+
if wfp is None or wfp == '':
208+
self.print_stderr(f'Warning: Empty WFP in request input: {wfp}')
209+
resp = self.scanapi.scan(wfp, scan_id=current_thread)
210+
if resp:
211+
self.output.put(resp) # Store the output response to later collection
212+
self.update_bar(count)
213+
self.inputs.task_done()
214+
self.print_trace(f'Request complete ({current_thread}).')
204215
except queue.Empty as e:
205216
self.print_stderr(f'No message available to process ({current_thread}). Checking again...')
206217
except Exception as e:
207-
ThreadedScanning.print_stderr(f'ERROR: Problem encountered running scan: {e}')
218+
self.print_stderr(f'ERROR: Problem encountered running scan: {e}. Aborting current thread.')
208219
self._errors = True
209220
if wfp:
210221
self.inputs.task_done() # If there was a WFP being processed, remove it from the queue
222+
api_error = True # Stop processing anymore work requests
223+
self._stop_scanning.set() # Tell the parent process to abort scanning
211224
else:
212225
time.sleep(1) # Sleep while waiting for the queue depth to build up
213226
self.print_trace(f'Thread complete ({current_thread}).')

0 commit comments

Comments
 (0)