Run submission in a separate thread, and more explicit state

Also more explicit timeouts and an overall better handling of errors.
This commit is contained in:
2018-08-14 16:36:43 +02:00
parent 5d86b87f40
commit b64577df2e
8 changed files with 191 additions and 66 deletions

View File

@@ -7,27 +7,11 @@ import urllib.parse
import requests
from . import timeouts, exceptions
log = logging.getLogger(__name__)
class CommunicationError(requests.exceptions.BaseHTTPError):
"""Raised when we get an invalid status code form the MyData server."""
def __init__(self, message: str, response: requests.Response):
self.message = message
self.status_code = response.status_code
self.body = response.text
if response.headers.get('Content-Type', '') == 'application/json':
self.json = response.json()
else:
self.json = None
def __str__(self):
return f'{self.message}; ' \
f'status_code={self.status_code}; json={self.json}; body={self.body}'
class SubmissionResult:
"""Metadata of the submitted benchmark.
@@ -46,7 +30,6 @@ class SubmissionResult:
class BenchmarkClient:
default_timeout = 30 # seconds
def __init__(self, mydata_server: str) -> None:
from requests.adapters import HTTPAdapter
@@ -126,7 +109,7 @@ class BenchmarkClient:
log.debug('validating token at %s', self.url_verify_token)
resp = self.session.get(self.url_verify_token,
headers={'Authorization': f'Bearer {self.auth_token}'},
timeout=self.default_timeout)
timeout=timeouts.verify)
token_ok = resp.status_code in {200, 204}
if not token_ok:
log.info('Client token is no longer valid, will obtain another one.')
@@ -158,7 +141,7 @@ class BenchmarkClient:
if not webbrowser.open_new_tab(url):
raise SystemError(f'Unable to open a browser to visit {url}')
self.auth_token = self.auth_http_server.wait_for_token()
self.auth_token = self.auth_http_server.wait_for_token(timeout=timeouts.wait_for_token)
self._stop_http_server()
if self.auth_token:
@@ -179,10 +162,10 @@ class BenchmarkClient:
resp = self.session.post(self.url_submit,
json=payload,
headers={'Authorization': f'Bearer {self.auth_token}'},
timeout=self.default_timeout)
timeout=timeouts.submit)
if resp.status_code != 201:
log.error('Bad status code %d received: %s', resp.status_code, resp.text)
raise CommunicationError(f'Bad status code received', resp)
raise exceptions.CommunicationError(f'Bad status code received', resp)
result = resp.json()
return SubmissionResult(