From 9f4ed2f1efc3e1d903fce2874b68aa10ff0fe083 Mon Sep 17 00:00:00 2001 From: julie-sullivan Date: Tue, 6 Jul 2021 11:57:52 +0100 Subject: [PATCH] add query retry to pycellbase --- .../src/main/python/pycellbase/__init__.py | 2 +- .../main/python/pycellbase/cbrestclients.py | 4 +- .../src/main/python/pycellbase/commons.py | 41 +++++++++++++++++++ cellbase-client/src/main/python/setup.py | 2 +- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/cellbase-client/src/main/python/pycellbase/__init__.py b/cellbase-client/src/main/python/pycellbase/__init__.py index 3c9329b277..a0f66580ce 100755 --- a/cellbase-client/src/main/python/pycellbase/__init__.py +++ b/cellbase-client/src/main/python/pycellbase/__init__.py @@ -1 +1 @@ -__version__ = '4.7.1' +__version__ = '5.0.0' diff --git a/cellbase-client/src/main/python/pycellbase/cbrestclients.py b/cellbase-client/src/main/python/pycellbase/cbrestclients.py index cff658d190..91240a1a7c 100755 --- a/cellbase-client/src/main/python/pycellbase/cbrestclients.py +++ b/cellbase-client/src/main/python/pycellbase/cbrestclients.py @@ -1,6 +1,7 @@ import sys +import subprocess -from pycellbase.commons import get, deprecated +from pycellbase.commons import get, deprecated, retry class _ParentRestClient(object): @@ -11,6 +12,7 @@ def __init__(self, session, configuration, subcategory, category): self._subcategory = subcategory self._category = category + @retry(subprocess.TimeoutExpired) def _get(self, resource, query_id=None, options=None): """Queries the REST service and returns the result""" response = get(session=self._session, diff --git a/cellbase-client/src/main/python/pycellbase/commons.py b/cellbase-client/src/main/python/pycellbase/commons.py index 6572cc1bfc..730311d673 100644 --- a/cellbase-client/src/main/python/pycellbase/commons.py +++ b/cellbase-client/src/main/python/pycellbase/commons.py @@ -1,6 +1,8 @@ +import logging import sys import time import warnings +from functools import wraps import requests import threading import itertools @@ -265,3 +267,42 @@ def new_func(*args, **kwargs): warnings.simplefilter('default', DeprecationWarning) # reset filter return func(*args, **kwargs) return new_func + +def retry(exception_type, total_tries=2, wait_period=2, backoff=2): + """ + Definition of a spicy little decorator-wrapper class + This will wrap methods (e.g. samtools reads), catching only specific error types, and replaying the command + This uses exponential backoff-retrier logical timing, and a set number of retries/adjustable wait period + """ + + def retry_decorator(f): + @wraps(f) + def func_with_retries(*args, **kwargs): + _tries, _delay = total_tries + 1, wait_period + while _tries > 1: + + try: + return f(*args, **kwargs) + except exception_type as e: + _tries -= 1 + if _tries == 1: + logging.error( + "Attempted command {} times, without success".format( + total_tries + ), + exc_info=True, + ) + raise + + logging.warning( + "Timeout detected, retrying in {} seconds. Args {}, Kwargs {}".format( + _delay, args, kwargs + ) + ) + time.sleep(_delay) + # for each failure, extend the wait + _delay *= backoff + + return func_with_retries + + return retry_decorator \ No newline at end of file diff --git a/cellbase-client/src/main/python/setup.py b/cellbase-client/src/main/python/setup.py index 8cf3a57023..f35bf2bbb5 100755 --- a/cellbase-client/src/main/python/setup.py +++ b/cellbase-client/src/main/python/setup.py @@ -13,7 +13,7 @@ setup_kwargs = { 'name': 'pycellbase', - 'version': '4.7.1', + 'version': '5.0.0', 'description': 'Python client for CellBase', 'long_description': long_description, 'long_description_content_type': 'text/x-rst',