Commit e3f93e7a authored by Morten Brekkevold's avatar Morten Brekkevold
Browse files

Add error handling functions

Because:
- We keep seeing random timeout errors in production.
- It's better to retry operations that time out a few times before
  giving up completely - this runs in the background sending cron mail,
  so we should try to take care of our own problemns ;-)
parent 13a320d8
"""Error handling mechanisms for kindnavsync"""
import time
import functools
def retry_on_timeout(retries=3, delay=0):
"""Decorator to retry an operation a set number of times if a TimeoutError is
raised. An optional delay between retries is also available.
Usage example:
@retry_on_timeout(retries=3, delay=1)
def error_prone_function(query)
do_slow_operation(query)
:param retries: The number of times to try a successful run of the function
before re-raising the timeout.
:param delay: An optional delay between retries.
"""
def inner_function(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
for attempt in range(retries):
try:
return func(*args, **kwargs)
except TimeoutError:
if attempt == retries-1:
raise
elif delay:
time.sleep(delay)
return wrapper
return inner_function
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment