OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 # pylint: disable=unused-wildcard-import |
6 Function/method decorators that provide timeout and retry logic. | 6 # pylint: disable=wildcard-import |
7 """ | |
8 | 7 |
9 import functools | 8 from devil.android.decorators import * |
10 import os | |
11 import sys | |
12 import threading | |
13 | |
14 from pylib import cmd_helper | |
15 from pylib import constants | |
16 from pylib.device import device_errors | |
17 from pylib.utils import reraiser_thread | |
18 from pylib.utils import timeout_retry | |
19 | |
20 DEFAULT_TIMEOUT_ATTR = '_default_timeout' | |
21 DEFAULT_RETRIES_ATTR = '_default_retries' | |
22 | |
23 | |
24 def _TimeoutRetryWrapper(f, timeout_func, retries_func, pass_values=False): | |
25 """ Wraps a funcion with timeout and retry handling logic. | |
26 | |
27 Args: | |
28 f: The function to wrap. | |
29 timeout_func: A callable that returns the timeout value. | |
30 retries_func: A callable that returns the retries value. | |
31 pass_values: If True, passes the values returned by |timeout_func| and | |
32 |retries_func| to the wrapped function as 'timeout' and | |
33 'retries' kwargs, respectively. | |
34 Returns: | |
35 The wrapped function. | |
36 """ | |
37 @functools.wraps(f) | |
38 def TimeoutRetryWrapper(*args, **kwargs): | |
39 timeout = timeout_func(*args, **kwargs) | |
40 retries = retries_func(*args, **kwargs) | |
41 if pass_values: | |
42 kwargs['timeout'] = timeout | |
43 kwargs['retries'] = retries | |
44 def impl(): | |
45 return f(*args, **kwargs) | |
46 try: | |
47 if isinstance(threading.current_thread(), | |
48 timeout_retry.TimeoutRetryThread): | |
49 return impl() | |
50 else: | |
51 return timeout_retry.Run(impl, timeout, retries) | |
52 except reraiser_thread.TimeoutError as e: | |
53 raise device_errors.CommandTimeoutError(str(e)), None, ( | |
54 sys.exc_info()[2]) | |
55 except cmd_helper.TimeoutError as e: | |
56 raise device_errors.CommandTimeoutError(str(e)), None, ( | |
57 sys.exc_info()[2]) | |
58 return TimeoutRetryWrapper | |
59 | |
60 | |
61 def WithTimeoutAndRetries(f): | |
62 """A decorator that handles timeouts and retries. | |
63 | |
64 'timeout' and 'retries' kwargs must be passed to the function. | |
65 | |
66 Args: | |
67 f: The function to decorate. | |
68 Returns: | |
69 The decorated function. | |
70 """ | |
71 get_timeout = lambda *a, **kw: kw['timeout'] | |
72 get_retries = lambda *a, **kw: kw['retries'] | |
73 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | |
74 | |
75 | |
76 def WithExplicitTimeoutAndRetries(timeout, retries): | |
77 """Returns a decorator that handles timeouts and retries. | |
78 | |
79 The provided |timeout| and |retries| values are always used. | |
80 | |
81 Args: | |
82 timeout: The number of seconds to wait for the decorated function to | |
83 return. Always used. | |
84 retries: The number of times the decorated function should be retried on | |
85 failure. Always used. | |
86 Returns: | |
87 The actual decorator. | |
88 """ | |
89 def decorator(f): | |
90 get_timeout = lambda *a, **kw: timeout | |
91 get_retries = lambda *a, **kw: retries | |
92 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | |
93 return decorator | |
94 | |
95 | |
96 def WithTimeoutAndRetriesDefaults(default_timeout, default_retries): | |
97 """Returns a decorator that handles timeouts and retries. | |
98 | |
99 The provided |default_timeout| and |default_retries| values are used only | |
100 if timeout and retries values are not provided. | |
101 | |
102 Args: | |
103 default_timeout: The number of seconds to wait for the decorated function | |
104 to return. Only used if a 'timeout' kwarg is not passed | |
105 to the decorated function. | |
106 default_retries: The number of times the decorated function should be | |
107 retried on failure. Only used if a 'retries' kwarg is not | |
108 passed to the decorated function. | |
109 Returns: | |
110 The actual decorator. | |
111 """ | |
112 def decorator(f): | |
113 get_timeout = lambda *a, **kw: kw.get('timeout', default_timeout) | |
114 get_retries = lambda *a, **kw: kw.get('retries', default_retries) | |
115 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) | |
116 return decorator | |
117 | |
118 | |
119 def WithTimeoutAndRetriesFromInstance( | |
120 default_timeout_name=DEFAULT_TIMEOUT_ATTR, | |
121 default_retries_name=DEFAULT_RETRIES_ATTR): | |
122 """Returns a decorator that handles timeouts and retries. | |
123 | |
124 The provided |default_timeout_name| and |default_retries_name| are used to | |
125 get the default timeout value and the default retries value from the object | |
126 instance if timeout and retries values are not provided. | |
127 | |
128 Note that this should only be used to decorate methods, not functions. | |
129 | |
130 Args: | |
131 default_timeout_name: The name of the default timeout attribute of the | |
132 instance. | |
133 default_retries_name: The name of the default retries attribute of the | |
134 instance. | |
135 Returns: | |
136 The actual decorator. | |
137 """ | |
138 def decorator(f): | |
139 def get_timeout(inst, *_args, **kwargs): | |
140 return kwargs.get('timeout', getattr(inst, default_timeout_name)) | |
141 def get_retries(inst, *_args, **kwargs): | |
142 return kwargs.get('retries', getattr(inst, default_retries_name)) | |
143 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) | |
144 return decorator | |
145 | |
OLD | NEW |