[BE][GHF] Add retries_decorator (#101227)

I've noticed that 3-4 functions in trymerge are trying to implement similar tail recursion for flaky network retries.

Unify them using single wrapper in `gitutils.py`

<!--
copilot:poem
-->
### <samp>🤖 Generated by Copilot at 8d40631</samp>

> _`retries_decorator`_
> _adds resilience to GitHub scripts_
> _autumn of errors_
Pull Request resolved: https://github.com/pytorch/pytorch/pull/101227
Approved by: https://github.com/kit1980
This commit is contained in:
Nikita Shulga
2023-05-12 20:29:06 +00:00
committed by PyTorch MergeBot
parent 2fcc2002fa
commit 568bac7961
3 changed files with 59 additions and 45 deletions

View File

@ -5,8 +5,21 @@ import re
import tempfile
from collections import defaultdict
from datetime import datetime
from typing import Any, cast, Dict, Iterator, List, Optional, Tuple, Union
from functools import wraps
from typing import (
Any,
Callable,
cast,
Dict,
Iterator,
List,
Optional,
Tuple,
TypeVar,
Union,
)
T = TypeVar("T")
RE_GITHUB_URL_MATCH = re.compile("^https://.*@?github.com/(.+)/(.+)$")
@ -380,3 +393,24 @@ def are_ghstack_branches_in_sync(repo: GitRepo, head_ref: str) -> bool:
repo.diff(f"{repo.remote}/{base_ref}", f"{repo.remote}/{head_ref}")
)
return orig_diff_sha == head_diff_sha
def retries_decorator(
rc: Any = None, num_retries: int = 3
) -> Callable[[Callable[..., T]], Callable[..., T]]:
def decorator(f: Callable[..., T]) -> Callable[..., T]:
@wraps(f)
def wrapper(*args: List[Any], **kwargs: Dict[str, Any]) -> T:
for idx in range(num_retries):
try:
return f(*args, **kwargs)
except Exception as e:
print(
f'Attempt {idx} of {num_retries} to call {f.__name__} failed with "{e}"'
)
pass
return cast(T, rc)
return wrapper
return decorator