Skip to content

Commit 1cd411c

Browse files
committed
update simulator resilience
1 parent 12328ec commit 1cd411c

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

simulator/authentication.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ def login(self) -> str:
129129
if CLIENT_SECRET:
130130
data["client_secret"] = CLIENT_SECRET
131131

132-
max_retries = 5
133-
retry_delay = 2
132+
retry_delay = 3
133+
attempt = 0
134134

135-
for attempt in range(max_retries):
135+
while True:
136+
attempt += 1
136137
try:
137138
response = self.session.post(token_url, data=data, timeout=10)
138139

@@ -142,13 +143,12 @@ def login(self) -> str:
142143
return token_data["access_token"]
143144

144145
if response.status_code == 503 or response.status_code == 502:
145-
if attempt < max_retries - 1:
146-
logger.warning(
147-
f"Keycloak not ready (HTTP {response.status_code}). "
148-
f"Retrying in {retry_delay} seconds... (attempt {attempt + 1}/{max_retries})",
149-
)
150-
time.sleep(retry_delay)
151-
continue
146+
logger.warning(
147+
f"Keycloak not ready (HTTP {response.status_code}). "
148+
f"Retrying in {retry_delay} seconds... (attempt {attempt})",
149+
)
150+
time.sleep(retry_delay)
151+
continue
152152

153153
try:
154154
error_data = response.json()
@@ -161,19 +161,16 @@ def login(self) -> str:
161161
f"HTTP {response.status_code}: {response.text[:100]}"
162162
)
163163

164-
raise Exception(
164+
logger.warning(
165165
f"Authentication failed: {error_msg}. "
166-
f"Make sure the client '{CLIENT_ID}' has 'Direct Access Grants' enabled in Keycloak "
167-
f"and that USERNAME/PASSWORD are correct.",
166+
f"Retrying in {retry_delay} seconds... (attempt {attempt})",
168167
)
168+
time.sleep(retry_delay)
169+
continue
169170
except requests.exceptions.RequestException as e:
170-
if attempt < max_retries - 1:
171-
logger.warning(
172-
f"Connection error during authentication: {e}. "
173-
f"Retrying in {retry_delay} seconds... (attempt {attempt + 1}/{max_retries})",
174-
)
175-
time.sleep(retry_delay)
176-
continue
177-
raise
178-
179-
raise Exception("Authentication failed after multiple retries")
171+
logger.warning(
172+
f"Connection error during authentication: {e}. "
173+
f"Retrying in {retry_delay} seconds... (attempt {attempt})",
174+
)
175+
time.sleep(retry_delay)
176+
continue

simulator/graphql_client.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ def __init__(self):
1313
else:
1414
self.authenticator = InteractiveAuthenticator(self.session)
1515

16+
def _is_authentication_error(self, response: requests.Response) -> bool:
17+
if response.status_code == 401:
18+
return True
19+
if response.status_code == 400:
20+
try:
21+
error_data = response.json()
22+
if "errors" in error_data:
23+
for error in error_data["errors"]:
24+
message = error.get("message", "").lower()
25+
if "unauthenticated" in message or "not authenticated" in message:
26+
return True
27+
except Exception:
28+
pass
29+
return False
30+
1631
def query(self, query: str, variables: dict = None) -> dict:
1732
if not self.token:
1833
self.token = self.authenticator.login()
@@ -31,9 +46,9 @@ def query(self, query: str, variables: dict = None) -> dict:
3146
headers=headers,
3247
)
3348

34-
if response.status_code == 401:
49+
if self._is_authentication_error(response):
3550
logger.warning(
36-
"Token expired (401). Initiating re-authentication...",
51+
"Authentication error detected. Re-authenticating...",
3752
)
3853
self.token = self.authenticator.login()
3954
headers["Authorization"] = f"Bearer {self.token}"

0 commit comments

Comments
 (0)