Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query retry to PyCellBase v5 #562

Open
wants to merge 1 commit into
base: release-5.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cellbase-client/src/main/python/pycellbase/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '4.7.1'
__version__ = '5.0.0'
4 changes: 3 additions & 1 deletion cellbase-client/src/main/python/pycellbase/cbrestclients.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import subprocess

from pycellbase.commons import get, deprecated
from pycellbase.commons import get, deprecated, retry


class _ParentRestClient(object):
Expand All @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions cellbase-client/src/main/python/pycellbase/commons.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import sys
import time
import warnings
from functools import wraps
import requests
import threading
import itertools
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion cellbase-client/src/main/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down