diff --git a/lwr/lwr_client/client.py b/lwr/lwr_client/client.py index c4241c8642ce8a30d912522eca94ff4f97b1f248..e56a5db897844419d31b27e04201b7d53256f5ad 100644 --- a/lwr/lwr_client/client.py +++ b/lwr/lwr_client/client.py @@ -1,49 +1,18 @@ import os import shutil -import json +import time from json import dumps -from time import sleep from .destination import submit_params from .setup_handler import build as build_setup_handler from .job_directory import RemoteJobDirectory +from .decorators import parseJson +from .decorators import retry import logging log = logging.getLogger(__name__) CACHE_WAIT_SECONDS = 3 -MAX_RETRY_COUNT = 5 -RETRY_SLEEP_TIME = 0.1 - - -class parseJson(object): - - def __call__(self, func): - def replacement(*args, **kwargs): - response = func(*args, **kwargs) - return json.loads(response) - return replacement - - -class retry(object): - - def __call__(self, func): - - def replacement(*args, **kwargs): - max_count = MAX_RETRY_COUNT - count = 0 - while True: - count += 1 - try: - return func(*args, **kwargs) - except: - if count >= max_count: - raise - else: - sleep(RETRY_SLEEP_TIME) - continue - - return replacement class OutputNotFoundException(Exception): @@ -298,9 +267,10 @@ class JobClient(BaseJobClient): return complete_response else: print complete_response - sleep(1) + time.sleep(1) i += 1 + @retry() @parseJson() def raw_check_complete(self): """ @@ -317,7 +287,6 @@ class JobClient(BaseJobClient): response = self.raw_check_complete() return response["complete"] == "true" - @retry() def get_status(self): check_complete_response = self.raw_check_complete() # Older LWR instances won't set status so use 'complete', at some diff --git a/lwr/lwr_client/decorators.py b/lwr/lwr_client/decorators.py new file mode 100644 index 0000000000000000000000000000000000000000..94035f5ba1b355077cd5e4752fc31779bd031657 --- /dev/null +++ b/lwr/lwr_client/decorators.py @@ -0,0 +1,35 @@ +import time +import json + +MAX_RETRY_COUNT = 5 +RETRY_SLEEP_TIME = 0.1 + + +class parseJson(object): + + def __call__(self, func): + def replacement(*args, **kwargs): + response = func(*args, **kwargs) + return json.loads(response) + return replacement + + +class retry(object): + + def __call__(self, func): + + def replacement(*args, **kwargs): + max_count = MAX_RETRY_COUNT + count = 0 + while True: + count += 1 + try: + return func(*args, **kwargs) + except: + if count >= max_count: + raise + else: + time.sleep(RETRY_SLEEP_TIME) + continue + + return replacement diff --git a/test/client_test.py b/test/client_test.py index 336c9b5606083f0d3bd1bcd669635b3b092fb8df..2a25ad2757d60b8ca5d4b8274125f0cff9ee6d29 100644 --- a/test/client_test.py +++ b/test/client_test.py @@ -7,7 +7,7 @@ from six import text_type, binary_type from lwr.lwr_client.client import JobClient from lwr.lwr_client.manager import HttpLwrInterface from lwr.lwr_client.transport import Urllib2Transport -from lwr.lwr_client.client import retry, MAX_RETRY_COUNT +from lwr.lwr_client.decorators import retry, MAX_RETRY_COUNT def test_with_retry():