mirror of
https://pagure.io/fm-orchestrator.git
synced 2026-04-15 07:49:57 +08:00
This also includes `from __future__ import absolute_import` in every file so that the imports are consistent in Python 2 and 3. The Python 2 tests fail without this.
33 lines
986 B
Python
33 lines
986 B
Python
# -*- coding: utf-8 -*-
|
|
# SPDX-License-Identifier: MIT
|
|
from __future__ import absolute_import
|
|
import functools
|
|
import time
|
|
|
|
from module_build_service import conf, log
|
|
|
|
|
|
def retry(timeout=conf.net_timeout, interval=conf.net_retry_interval, wait_on=Exception):
|
|
""" A decorator that allows to retry a section of code...
|
|
...until success or timeout.
|
|
"""
|
|
|
|
def wrapper(function):
|
|
@functools.wraps(function)
|
|
def inner(*args, **kwargs):
|
|
start = time.time()
|
|
while True:
|
|
try:
|
|
return function(*args, **kwargs)
|
|
except wait_on as e:
|
|
log.warning(
|
|
"Exception %r raised from %r. Retry in %rs" % (e, function, interval)
|
|
)
|
|
time.sleep(interval)
|
|
if (time.time() - start) >= timeout:
|
|
raise # This re-raises the last exception.
|
|
|
|
return inner
|
|
|
|
return wrapper
|