From c69d3826e1651a8bd3a51f54f0a742022277df75 Mon Sep 17 00:00:00 2001 From: Jorrit Schaap <schaap@astron.nl> Date: Fri, 22 Mar 2024 11:06:40 +0100 Subject: [PATCH] try/except --- .../lib/windmill_standstill.py | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/SAS/TMSS/backend/services/windmill_standstill/lib/windmill_standstill.py b/SAS/TMSS/backend/services/windmill_standstill/lib/windmill_standstill.py index 548e86f1651..f442015ecd9 100644 --- a/SAS/TMSS/backend/services/windmill_standstill/lib/windmill_standstill.py +++ b/SAS/TMSS/backend/services/windmill_standstill/lib/windmill_standstill.py @@ -71,16 +71,23 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler): self.polling_thread.join() def refresh_access_token(self): - logger.info('refreshing access token...') - response = requests.post(self.token_url, - data={"grant_type": "client_credentials", - "scope": self.scope, - "client_id": self.client_id, - "client_secret": self.client_secret}) - json_response = response.json() - self.access_token = json_response["access_token"] - self.access_token_expiration_date = datetime.utcnow() + timedelta(seconds=json_response['expires_in']-10) - logger.info('new token valid till %s' % self.access_token_expiration_date.isoformat()) + try: + logger.info('refreshing access token...') + response = requests.post(self.token_url, + data={"grant_type": "client_credentials", + "scope": self.scope, + "client_id": self.client_id, + "client_secret": self.client_secret}) + except Exception as e: + logger.error("could not refresh access token: %s", str(e)) + + try: + json_response = response.json() + self.access_token = json_response["access_token"] + self.access_token_expiration_date = datetime.utcnow() + timedelta(seconds=json_response['expires_in']-10) + logger.info('new token valid till %s' % self.access_token_expiration_date.isoformat()) + except Exception as e: + logger.error("could not refresh access token: http_status_code=%s error: %s", response.status_code, str(e)) def get_api_path_as_json(self, path): if datetime.utcnow() >= self.access_token_expiration_date: -- GitLab