1
0
Fork 0

Add retrying

This commit is contained in:
Casper V. Kristensen 2022-05-08 14:51:26 +02:00
parent 65ee59722a
commit 4082a6946c
2 changed files with 25 additions and 0 deletions

View file

@ -113,6 +113,7 @@ def generate_post(releases: Releases) -> str:
return post_str return post_str
@util.retry(attempts=3, delay=120)
def generate(post=False, pm_recipients=None) -> None: def generate(post=False, pm_recipients=None) -> None:
logger.info("-------------------------------------------------------------------------------------------------") logger.info("-------------------------------------------------------------------------------------------------")
start_time = time.time() start_time = time.time()

View file

@ -1,6 +1,11 @@
import difflib import difflib
import logging
import time
from functools import wraps
from typing import Sequence, List from typing import Sequence, List
logger = logging.getLogger(__name__)
def humanize(n: int, precision=2, prefix="bin", suffix="B") -> str: def humanize(n: int, precision=2, prefix="bin", suffix="B") -> str:
""" """
@ -61,3 +66,22 @@ def markdown_escape(text: str) -> str:
"|": "|", "|": "|",
} }
return text.translate(str.maketrans(table)) return text.translate(str.maketrans(table))
def retry(attempts=3, delay=0):
"""
Retry wrapped function `attempts` times.
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
for i in range(1, attempts+1):
try:
return func(*args, **kwargs)
except Exception as e:
logger.exception(f"{func.__name__} attempt {i}/{attempts}", exc_info=e)
if i >= attempts:
raise
time.sleep(delay)
return wrapper
return decorator