| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """ | 5 """ |
| 6 Function/method decorators that provide timeout and retry logic. | 6 Function/method decorators that provide timeout and retry logic. |
| 7 """ | 7 """ |
| 8 | 8 |
| 9 import functools | 9 import functools |
| 10 import itertools |
| 10 import sys | 11 import sys |
| 11 import threading | 12 import threading |
| 12 | 13 |
| 13 from devil.android import device_errors | 14 from devil.android import device_errors |
| 14 from devil.utils import cmd_helper | 15 from devil.utils import cmd_helper |
| 15 from devil.utils import reraiser_thread | 16 from devil.utils import reraiser_thread |
| 16 from devil.utils import timeout_retry | 17 from devil.utils import timeout_retry |
| 17 | 18 |
| 18 DEFAULT_TIMEOUT_ATTR = '_default_timeout' | 19 DEFAULT_TIMEOUT_ATTR = '_default_timeout' |
| 19 DEFAULT_RETRIES_ATTR = '_default_retries' | 20 DEFAULT_RETRIES_ATTR = '_default_retries' |
| 20 | 21 |
| 21 | 22 |
| 22 def _TimeoutRetryWrapper(f, timeout_func, retries_func, pass_values=False): | 23 def _TimeoutRetryWrapper(f, timeout_func, retries_func, pass_values=False): |
| 23 """ Wraps a funcion with timeout and retry handling logic. | 24 """ Wraps a funcion with timeout and retry handling logic. |
| 24 | 25 |
| 25 Args: | 26 Args: |
| 26 f: The function to wrap. | 27 f: The function to wrap. |
| 27 timeout_func: A callable that returns the timeout value. | 28 timeout_func: A callable that returns the timeout value. |
| 28 retries_func: A callable that returns the retries value. | 29 retries_func: A callable that returns the retries value. |
| 29 pass_values: If True, passes the values returned by |timeout_func| and | 30 pass_values: If True, passes the values returned by |timeout_func| and |
| 30 |retries_func| to the wrapped function as 'timeout' and | 31 |retries_func| to the wrapped function as 'timeout' and |
| 31 'retries' kwargs, respectively. | 32 'retries' kwargs, respectively. |
| 32 Returns: | 33 Returns: |
| 33 The wrapped function. | 34 The wrapped function. |
| 34 """ | 35 """ |
| 35 @functools.wraps(f) | 36 @functools.wraps(f) |
| 36 def TimeoutRetryWrapper(*args, **kwargs): | 37 def timeout_retry_wrapper(*args, **kwargs): |
| 37 timeout = timeout_func(*args, **kwargs) | 38 timeout = timeout_func(*args, **kwargs) |
| 38 retries = retries_func(*args, **kwargs) | 39 retries = retries_func(*args, **kwargs) |
| 39 if pass_values: | 40 if pass_values: |
| 40 kwargs['timeout'] = timeout | 41 kwargs['timeout'] = timeout |
| 41 kwargs['retries'] = retries | 42 kwargs['retries'] = retries |
| 43 @functools.wraps(f) |
| 42 def impl(): | 44 def impl(): |
| 43 return f(*args, **kwargs) | 45 return f(*args, **kwargs) |
| 44 try: | 46 try: |
| 45 if isinstance(threading.current_thread(), | 47 if isinstance(threading.current_thread(), |
| 46 timeout_retry.TimeoutRetryThread): | 48 timeout_retry.TimeoutRetryThread): |
| 47 return impl() | 49 return impl() |
| 48 else: | 50 else: |
| 49 return timeout_retry.Run(impl, timeout, retries) | 51 desc = '%s(%s)' % (f.__name__, ', '.join(itertools.chain( |
| 52 (str(a) for a in args), |
| 53 ('%s=%s' % (k, str(v)) for k, v in kwargs.iteritems())))) |
| 54 return timeout_retry.Run(impl, timeout, retries, desc=desc) |
| 50 except reraiser_thread.TimeoutError as e: | 55 except reraiser_thread.TimeoutError as e: |
| 51 raise device_errors.CommandTimeoutError(str(e)), None, ( | 56 raise device_errors.CommandTimeoutError(str(e)), None, ( |
| 52 sys.exc_info()[2]) | 57 sys.exc_info()[2]) |
| 53 except cmd_helper.TimeoutError as e: | 58 except cmd_helper.TimeoutError as e: |
| 54 raise device_errors.CommandTimeoutError(str(e)), None, ( | 59 raise device_errors.CommandTimeoutError(str(e)), None, ( |
| 55 sys.exc_info()[2]) | 60 sys.exc_info()[2]) |
| 56 return TimeoutRetryWrapper | 61 return timeout_retry_wrapper |
| 57 | 62 |
| 58 | 63 |
| 59 def WithTimeoutAndRetries(f): | 64 def WithTimeoutAndRetries(f): |
| 60 """A decorator that handles timeouts and retries. | 65 """A decorator that handles timeouts and retries. |
| 61 | 66 |
| 62 'timeout' and 'retries' kwargs must be passed to the function. | 67 'timeout' and 'retries' kwargs must be passed to the function. |
| 63 | 68 |
| 64 Args: | 69 Args: |
| 65 f: The function to decorate. | 70 f: The function to decorate. |
| 66 Returns: | 71 Returns: |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 def get_timeout(inst, *_args, **kwargs): | 144 def get_timeout(inst, *_args, **kwargs): |
| 140 ret = getattr(inst, default_timeout_name) | 145 ret = getattr(inst, default_timeout_name) |
| 141 if min_default_timeout is not None: | 146 if min_default_timeout is not None: |
| 142 ret = max(min_default_timeout, ret) | 147 ret = max(min_default_timeout, ret) |
| 143 return kwargs.get('timeout', ret) | 148 return kwargs.get('timeout', ret) |
| 144 def get_retries(inst, *_args, **kwargs): | 149 def get_retries(inst, *_args, **kwargs): |
| 145 return kwargs.get('retries', getattr(inst, default_retries_name)) | 150 return kwargs.get('retries', getattr(inst, default_retries_name)) |
| 146 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) | 151 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) |
| 147 return decorator | 152 return decorator |
| 148 | 153 |
| OLD | NEW |