OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """A utility to run functions with timeouts and retries.""" | 5 """A utility to run functions with timeouts and retries.""" |
6 # pylint: disable=W0702 | 6 # pylint: disable=W0702 |
7 | 7 |
8 import logging | 8 import logging |
9 import threading | 9 import threading |
10 import time | 10 import time |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 The number of seconds remaining before the thread times out, or None | 42 The number of seconds remaining before the thread times out, or None |
43 if the thread never times out. | 43 if the thread never times out. |
44 | 44 |
45 Raises: | 45 Raises: |
46 reraiser_thread.TimeoutError if the remaining time is less than the | 46 reraiser_thread.TimeoutError if the remaining time is less than the |
47 required time. | 47 required time. |
48 """ | 48 """ |
49 remaining = self._watcher.GetRemaining() | 49 remaining = self._watcher.GetRemaining() |
50 if remaining is not None and remaining < required: | 50 if remaining is not None and remaining < required: |
51 if msg is None: | 51 if msg is None: |
52 msg = 'Timeout expired' | 52 msg = 'Timeout of %.1f secs expired' % self._watcher.GetTimeout() |
53 if remaining > 0: | 53 if remaining > 0: |
54 msg += (', wait of %.1f secs required but only %.1f secs left' | 54 msg += (', wait of %.1f secs required but only %.1f secs left' |
55 % (required, remaining)) | 55 % (required, remaining)) |
perezju
2017/08/16 08:35:29
As I just wrote on the tracking issue; maybe it's
charliea (OOO until 10-5)
2017/08/18 18:52:18
Done.
| |
56 raise reraiser_thread.TimeoutError(msg) | 56 raise reraiser_thread.TimeoutError(msg) |
57 return remaining | 57 return remaining |
58 | 58 |
59 | 59 |
60 def CurrentTimeoutThreadGroup(): | 60 def CurrentTimeoutThreadGroup(): |
61 """Returns the thread group that owns or is blocked on the active thread. | 61 """Returns the thread group that owns or is blocked on the active thread. |
62 | 62 |
63 Returns: | 63 Returns: |
64 Returns None if no TimeoutRetryThreadGroup is tracking the current thread. | 64 Returns None if no TimeoutRetryThreadGroup is tracking the current thread. |
65 """ | 65 """ |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 msg = ['condition', repr(condition_name), 'met' if result else 'not met'] | 103 msg = ['condition', repr(condition_name), 'met' if result else 'not met'] |
104 if timeout_thread_group: | 104 if timeout_thread_group: |
105 # pylint: disable=no-member | 105 # pylint: disable=no-member |
106 msg.append('(%.1fs)' % timeout_thread_group.GetElapsedTime()) | 106 msg.append('(%.1fs)' % timeout_thread_group.GetElapsedTime()) |
107 logger.info(' '.join(msg)) | 107 logger.info(' '.join(msg)) |
108 if result: | 108 if result: |
109 return result | 109 return result |
110 if timeout_thread_group: | 110 if timeout_thread_group: |
111 # pylint: disable=no-member | 111 # pylint: disable=no-member |
112 timeout_thread_group.GetRemainingTime(wait_period, | 112 timeout_thread_group.GetRemainingTime(wait_period, |
113 msg='Timed out waiting for %r' % condition_name) | 113 msg='Timed out waiting for %r' % condition_name) |
perezju
2017/08/16 08:35:29
I think you also need to update the msg here to ha
charliea (OOO until 10-5)
2017/08/18 18:52:17
Given that I think that the caller is more likely
| |
114 time.sleep(wait_period) | 114 time.sleep(wait_period) |
115 return None | 115 return None |
116 | 116 |
117 | 117 |
118 def AlwaysRetry(_exception): | 118 def AlwaysRetry(_exception): |
119 return True | 119 return True |
120 | 120 |
121 | 121 |
122 def Run(func, timeout, retries, args=None, kwargs=None, desc=None, | 122 def Run(func, timeout, retries, args=None, kwargs=None, desc=None, |
123 error_log_func=logging.critical, retry_if_func=AlwaysRetry): | 123 error_log_func=logging.critical, retry_if_func=AlwaysRetry): |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 if num_try > retries or not retry_if_func(e): | 166 if num_try > retries or not retry_if_func(e): |
167 raise | 167 raise |
168 # Do not catch KeyboardInterrupt. | 168 # Do not catch KeyboardInterrupt. |
169 except Exception as e: # pylint: disable=broad-except | 169 except Exception as e: # pylint: disable=broad-except |
170 if num_try > retries or not retry_if_func(e): | 170 if num_try > retries or not retry_if_func(e): |
171 raise | 171 raise |
172 error_log_func( | 172 error_log_func( |
173 '(%s) Exception on %s, attempt %d of %d: %r', | 173 '(%s) Exception on %s, attempt %d of %d: %r', |
174 thread_name, desc, num_try, retries + 1, e) | 174 thread_name, desc, num_try, retries + 1, e) |
175 num_try += 1 | 175 num_try += 1 |
OLD | NEW |