A waiter class for some check to reach a consistent state. :type min_successes: int :param min_successes: The minimum number of successful check calls to treat the check as stable. Default of 1 success. :type max_attempts: int :param min_successes: The maximum number of ti
| 1085 | |
| 1086 | |
| 1087 | class ConsistencyWaiter: |
| 1088 | """ |
| 1089 | A waiter class for some check to reach a consistent state. |
| 1090 | |
| 1091 | :type min_successes: int |
| 1092 | :param min_successes: The minimum number of successful check calls to |
| 1093 | treat the check as stable. Default of 1 success. |
| 1094 | |
| 1095 | :type max_attempts: int |
| 1096 | :param min_successes: The maximum number of times to attempt calling |
| 1097 | the check. Default of 20 attempts. |
| 1098 | |
| 1099 | :type delay: int |
| 1100 | :param delay: The number of seconds to delay the next API call after a |
| 1101 | failed check call. Default of 5 seconds. |
| 1102 | """ |
| 1103 | |
| 1104 | def __init__( |
| 1105 | self, |
| 1106 | min_successes=1, |
| 1107 | max_attempts=20, |
| 1108 | delay=5, |
| 1109 | delay_initial_poll=False, |
| 1110 | ): |
| 1111 | self.min_successes = min_successes |
| 1112 | self.max_attempts = max_attempts |
| 1113 | self.delay = delay |
| 1114 | self.delay_initial_poll = delay_initial_poll |
| 1115 | |
| 1116 | def wait(self, check, *args, **kwargs): |
| 1117 | """ |
| 1118 | Wait until the check succeeds the configured number of times |
| 1119 | |
| 1120 | :type check: callable |
| 1121 | :param check: A callable that returns True or False to indicate |
| 1122 | if the check succeeded or failed. |
| 1123 | |
| 1124 | :type args: list |
| 1125 | :param args: Any ordered arguments to be passed to the check. |
| 1126 | |
| 1127 | :type kwargs: dict |
| 1128 | :param kwargs: Any keyword arguments to be passed to the check. |
| 1129 | """ |
| 1130 | attempts = 0 |
| 1131 | successes = 0 |
| 1132 | if self.delay_initial_poll: |
| 1133 | time.sleep(self.delay) |
| 1134 | while attempts < self.max_attempts: |
| 1135 | attempts += 1 |
| 1136 | if check(*args, **kwargs): |
| 1137 | successes += 1 |
| 1138 | if successes >= self.min_successes: |
| 1139 | return |
| 1140 | else: |
| 1141 | time.sleep(self.delay) |
| 1142 | fail_msg = self._fail_message(attempts, successes) |
| 1143 | raise ConsistencyWaiterException(fail_msg) |
| 1144 |
no outgoing calls