omnata-plugin-runtime 0.4.5__tar.gz → 0.4.6a101__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: omnata-plugin-runtime
3
- Version: 0.4.5
3
+ Version: 0.4.6a101
4
4
  Summary: Classes and common runtime components for building and running Omnata Plugins
5
5
  Author: James Weakley
6
6
  Author-email: james.weakley@omnata.com
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "omnata-plugin-runtime"
3
- version = "0.4.5"
3
+ version = "0.4.6-a101"
4
4
  description = "Classes and common runtime components for building and running Omnata Plugins"
5
5
  authors = ["James Weakley <james.weakley@omnata.com>"]
6
6
  readme = "README.md"
@@ -8,7 +8,7 @@ import re
8
8
  import threading
9
9
  from email.utils import parsedate_to_datetime
10
10
  from logging import getLogger
11
- from typing import List, Literal, Optional, Dict, Tuple
11
+ from typing import Any, List, Literal, Optional, Dict, Tuple
12
12
  import requests
13
13
  from pydantic import Field, root_validator
14
14
  from pydantic.json import pydantic_encoder
@@ -349,6 +349,32 @@ class RetryLaterException(Exception):
349
349
  self.message = message
350
350
  super().__init__(self.message)
351
351
 
352
+ class RetryWithLogging(Retry):
353
+ """
354
+ Adding extra logs before making a retry request
355
+ """
356
+ def __call__(self, *args: Any, thread_cancellation_token:threading.Event, **kwargs: Any) -> Any:
357
+ self.thread_cancellation_token = thread_cancellation_token
358
+ return super().__call__(*args, **kwargs)
359
+
360
+ def sleep_for_retry(self, response=None):
361
+ retry_after = self.get_retry_after(response)
362
+ if retry_after:
363
+ logger.info(f"Retrying after {retry_after} seconds due to Retry-After header")
364
+ if self.thread_cancellation_token.wait(retry_after):
365
+ raise InterruptedWhileWaitingException(message="The sync was interrupted while waiting for rate limiting to expire")
366
+ return True
367
+ return False
368
+
369
+ def _sleep_backoff(self):
370
+ backoff = self.get_backoff_time()
371
+ if backoff <= 0:
372
+ return
373
+ logger.info(f"Retrying after {backoff} seconds due to backoff time")
374
+ if self.thread_cancellation_token.wait(backoff):
375
+ raise InterruptedWhileWaitingException(message="The sync was interrupted while waiting for rate limiting to expire")
376
+
377
+
352
378
  class RateLimitedSession(requests.Session):
353
379
  """
354
380
  Creates a requests session that will automatically handle rate limiting.
@@ -366,7 +392,7 @@ class RateLimitedSession(requests.Session):
366
392
  self.thread_cancellation_token = thread_cancellation_token
367
393
  self.statuses_to_include = statuses_to_include
368
394
 
369
- retry_strategy = Retry(
395
+ retry_strategy = RetryWithLogging(
370
396
  total=max_retries,
371
397
  backoff_factor=backoff_factor,
372
398
  status_forcelist=statuses_to_include,
@@ -398,7 +424,7 @@ class RateLimitedSession(requests.Session):
398
424
  def request(self, method, url, **kwargs):
399
425
  while True:
400
426
  response = super().request(method, url, **kwargs)
401
-
427
+ # TODO: this is probably all redundant as the Retry object should handle this at a lower level (urllib3)
402
428
  if response.status_code in self.statuses_to_include:
403
429
  if 'Retry-After' in response.headers:
404
430
  retry_after = response.headers['Retry-After']