- more checks about failed recordings attempts
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user