qcomgpsd: move retry logic to a decorator (#30633)
* qcomgpsd: move retry logic to a decorator * make that same * fix * fix thatmqb-accel-override-fix
parent
35f819c823
commit
51fa7b227a
2 changed files with 40 additions and 35 deletions
@ -0,0 +1,30 @@ |
||||
import time |
||||
import functools |
||||
|
||||
from openpilot.common.swaglog import cloudlog |
||||
|
||||
|
||||
def retry(attempts=3, delay=1.0, ignore_failure=False): |
||||
def decorator(func): |
||||
@functools.wraps(func) |
||||
def wrapper(*args, **kwargs): |
||||
for _ in range(attempts): |
||||
try: |
||||
return func(*args, **kwargs) |
||||
except Exception: |
||||
cloudlog.exception(f"{func.__name__} failed, trying again") |
||||
time.sleep(delay) |
||||
|
||||
if ignore_failure: |
||||
cloudlog.error(f"{func.__name__} failed after retry") |
||||
else: |
||||
raise Exception(f"{func.__name__} failed after retry") |
||||
return wrapper |
||||
return decorator |
||||
|
||||
|
||||
if __name__ == "__main__": |
||||
@retry(attempts=10) |
||||
def abc(): |
||||
raise ValueError("abc failed :(") |
||||
abc() |
Loading…
Reference in new issue