@@ -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
0 commit comments