diff --git a/src/agent_gsm.py b/src/agent_gsm.py index be6f01a..d6da42f 100644 --- a/src/agent_gsm.py +++ b/src/agent_gsm.py @@ -127,7 +127,7 @@ def run_analyze(file_test: str, file_reference: str, number: str) -> bool: result = False - if file_test: + if file_test.exists(): # Wait 5 seconds to give a chance to flush recorded file time.sleep(5.0) @@ -205,10 +205,10 @@ def run_analyze(file_test: str, file_reference: str, number: str) -> bool: upload_result = BACKEND.upload_audio(r['id'], file_test) if upload_result: - utils.log('Recorded audio is uploaded ok.') + utils.log(' Recorded audio is uploaded ok.') result = True else: - utils.log_error('Recorded audio is not uploaded.') + utils.log_error(' Recorded audio is not uploaded.') CACHE.add_recorded_audio(file_test, probe_id=upload_id) else: CACHE.add_recorded_audio(file_test, probe_id=upload_id) @@ -216,7 +216,7 @@ def run_analyze(file_test: str, file_reference: str, number: str) -> bool: except Exception as e: utils.log_error(e) else: - utils.log_error('Seems the file is not recorded. Usually it happens because adb logcat is not stable sometimes. Return signal to restart') + utils.log_error('Seems the file is not recorded. Skipping analysis and upload.') # Increase finished calls counter CALL_COUNTER.value = CALL_COUNTER.value + 1 @@ -543,11 +543,13 @@ if __name__ == '__main__': if BACKEND.load_audio(BACKEND.phone.audio_id, CONFIG.ReferenceAudio): CACHE.add_reference_audio(BACKEND.phone.audio_id, CONFIG.ReferenceAudio) else: - utils.log_error('Audio is not available online.') - if not CACHE.get_reference_audio(BACKEND.phone.audio_id, CONFIG.REFERENCE_AUDIO): - utils.log_error('Reference audio is not cached, sorry. Exiting.') + utils.log('Audio is not available online...') + if not CACHE.get_reference_audio(BACKEND.phone.audio_id, CONFIG.ReferenceAudio): + utils.log_error(' Reference audio is not cached, sorry. Exiting.') sys.exit(EXIT_ERROR) - + else: + utils.log(f' Found in cache.') + # Preparing reference audio utils.log('Running answering loop...') perform_answerer() diff --git a/src/utils_qualtest.py b/src/utils_qualtest.py index c8488c4..8680814 100644 --- a/src/utils_qualtest.py +++ b/src/utils_qualtest.py @@ -45,7 +45,22 @@ def ParseAttributes(t: str) -> dict: result[tokens[0].strip()] = tokens[1].strip() return result - + +# Time of operation start +TRACE_START_TIME = None + +# 10 seconds for I/O operation +TRACE_TOTAL_TIMEOUT = 30 + + +# This function serves as a "hook" that executes for each Python statement +# down the road. There may be some performance penalty, but as downloading +# a webpage is mostly I/O bound, it's not going to be significant. + +def trace_function(frame, event, arg): + if time.time() - TRACE_START_TIME > TRACE_TOTAL_TIMEOUT: + raise Exception('Timed out!') # Use whatever exception you consider appropriate. + class QualtestBackend: address: str @@ -98,12 +113,16 @@ class QualtestBackend: return result - def upload_audio(self, probe_id, path_recorded): + def upload_audio(self, probe_id, path_recorded: Path): result = False # Log about upload attempt utils.log_verbose(f"Uploading to {self.address} audio {path_recorded}") - + if not path_recorded.exists(): + utils.log_error(' File doesn\'t exists, skip.') + return False + + # Find URL for uploading url = utils.join_host_and_path(self.address, "/upload_audio/") try: @@ -112,8 +131,16 @@ class QualtestBackend: 'audio_kind': (None, '1'), 'audio_name': (None, os.path.basename(path_recorded))} - # values = {'probe_id': probe_id} - response = requests.post(url, files=files, timeout=utils.NETWORK_TIMEOUT) + try: + # Limit POST time by TRACE_TOTAL_TIMEOUT seconds + TRACE_START_TIME = time.time() + sys.settrace(trace_function) + response = requests.post(url, files=files, timeout=utils.NETWORK_TIMEOUT) + except: + raise + finally: + sys.settrace(None) + if response.status_code != 200: utils.log_error(f"Upload audio to {self.address} finished with error {response.status_code}", None) else: @@ -213,7 +240,10 @@ class QualtestBackend: def load_audio(self, audio_id: int, output_path: Path): + global TRACE_START_TIME + utils.log(f'Loading audio with ID: {audio_id} ...') + TRACE_START_TIME = time.time() try: # Build query for both V1 & V2 API params = urllib.parse.urlencode({"audio_id": audio_id}) @@ -221,8 +251,15 @@ class QualtestBackend: # Find URL url = utils.join_host_and_path(self.address, "/play_audio/?") + params - # Get response from server - response = requests.get(url, timeout=(utils.NETWORK_TIMEOUT, 5)) + sys.settrace(trace_function) + try: + # Get response from server + response = requests.get(url, timeout=(utils.NETWORK_TIMEOUT, 5)) + except: + raise + finally: + sys.settrace(None) + if response.status_code != 200: utils.log_error(f' Failed to get audio. Error code: {response.status_code}, msg: {response.content}') return False @@ -233,7 +270,7 @@ class QualtestBackend: utils.log(' Done.') return True except Exception as err: - utils.log_error(f' Exception when fetching audio: {str(err)}') + utils.log_error(f' Exception when fetching audio: {str(err)}') return False