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 97c04b6b15e09caa78a68f8196efb6e333106143..ff113ffb86bb6d86ce63c5ea88d9cae2a15694cf 100644 --- a/SAS/TMSS/backend/services/windmill_standstill/lib/windmill_standstill.py +++ b/SAS/TMSS/backend/services/windmill_standstill/lib/windmill_standstill.py @@ -58,6 +58,7 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler): self.client_id = client_id self.client_secret = client_secret self.access_token_expiration_date = datetime.min + self.access_token = None def start_handling(self): self.refresh_access_token() @@ -80,7 +81,7 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler): "client_id": self.client_id, "client_secret": self.client_secret}) except Exception as e: - logger.error("could not refresh access token: %s", str(e)) + logger.exception("could not refresh access token: %s", str(e)) try: json_response = response.json() @@ -88,10 +89,11 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler): 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)) + logger.exception("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: + logger.info('fetching %s...', path) + if datetime.utcnow() >= self.access_token_expiration_date or self.access_token is None: self.refresh_access_token() headers = {"Authorization": "Bearer " + self.access_token} response = requests.get(self.windmill_api_url + path, headers=headers) @@ -126,7 +128,6 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler): path += '?' if path.endswith('/') else '&' path += 'till' + scheduled_start_time_before.isoformat() - logger.info('fetching %s...') response = self.get_api_path_as_json(path) return response['blocks']