Retry a function until it doesn't raise a `CalledProcessError`, uses exponential backoff until `max_duration` is reached.
(fn: Callable[[], T], max_duration: int = 60)
| 148 | |
| 149 | |
| 150 | def run_with_retries(fn: Callable[[], T], max_duration: int = 60) -> T: |
| 151 | """Retry a function until it doesn't raise a `CalledProcessError`, uses |
| 152 | exponential backoff until `max_duration` is reached.""" |
| 153 | for retry in range(math.ceil(math.log2(max_duration))): |
| 154 | try: |
| 155 | return fn() |
| 156 | except subprocess.CalledProcessError as e: |
| 157 | sleep_time = 2**retry |
| 158 | print(f"Failed: {e}, retrying in {sleep_time}s") |
| 159 | time.sleep(sleep_time) |
| 160 | return fn() |