You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					31 lines
				
				737 B
			
		
		
			
		
	
	
					31 lines
				
				737 B
			| 
											2 years ago
										 | 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()
 |