aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/retry.py
blob: d14170bfec19709b44324b8bffd307c5f67ca7a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import time
import functools


# Partly copy-pasted from contrib/python/retry
def retry_func(f, exceptions=Exception, tries=-1, delay=1, max_delay=None, backoff=1):
    _tries, _delay = tries, delay
    while _tries:
        try:
            return f()
        except exceptions as e:
            _tries -= 1
            if not _tries:
                raise

            time.sleep(_delay)
            _delay *= backoff

            if max_delay is not None:
                _delay = min(_delay, max_delay)


def retry(**retry_kwargs):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            return retry_func(lambda: func(*args, **kwargs), **retry_kwargs)
        return wrapper
    return decorator