aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/retry.py
blob: e5ae8edae4639db32201e3100a8443a3e37848e8 (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
30
31
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:
            _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