Skip to content

Commit

Permalink
client: only load version when needed (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Sep 1, 2022
1 parent a40d115 commit 62ece62
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions mpcontribs-client/mpcontribs/client/__init__.py
Expand Up @@ -582,32 +582,33 @@ def __init__(
if self.url not in VALID_URLS:
raise ValueError(f"{self.url} not a valid URL (one of {VALID_URLS})")

retries, max_retries = 0, 3
is_mock_test = 'pytest' in sys.modules and self.protocol == "http"

if is_mock_test:
now = datetime.datetime.now()
self.version = Version(
major=now.year, minor=now.month, patch=now.day,
prerelease=(str(now.hour), str(now.minute))
)
else:
while retries < max_retries:
try:
r = requests.get(f"{self.url}/healthcheck", timeout=2)
if r.status_code == 200:
self.version = r.json().get("version")
break
else:
if "version" not in self.__dict__:
retries, max_retries = 0, 3
is_mock_test = 'pytest' in sys.modules and self.protocol == "http"

if is_mock_test:
now = datetime.datetime.now()
self.version = Version(
major=now.year, minor=now.month, patch=now.day,
prerelease=(str(now.hour), str(now.minute))
)
else:
while retries < max_retries:
try:
r = requests.get(f"{self.url}/healthcheck", timeout=2)
if r.status_code == 200:
self.version = r.json().get("version")
break
else:
retries += 1
logger.warning(
f"Healthcheck for {self.url} failed ({r.status_code})! Wait 30s."
)
time.sleep(30)
except RequestException as ex:
retries += 1
logger.warning(
f"Healthcheck for {self.url} failed with {r.status_code}! Waiting 30s."
)
logger.warning(f"Could not connect to {self.url} ({ex})! Wait 30s.")
time.sleep(30)
except RequestException as ex:
retries += 1
logger.warning(f"Could not connect to {self.url} ({ex})! Waiting 30s ...")
time.sleep(30)

if "session" not in self.__dict__:
self.session = get_session()
Expand Down

0 comments on commit 62ece62

Please sign in to comment.