def retry(conf, action):
n_retries = int(conf.get('N_RETRIES', '5'))
reset_period = int(conf.get('RESET_PERIOD', '3600'))
error_pause = int(conf.get('ERROR_PAUSE', '30'))
reset = int(time.time())
i = 0
while True:
try:
ret = action()
# These are the exception types that justify a retry -- extend this list as needed
except (httplib.IncompleteRead, socket.error, boto.exception.BotoClientError, ValueErrorRetry), e:
now = int(time.time())
if now > reset + reset_period:
print "******* RETRY RESET"
i = 0
reset = now
i += 1
print "******* RETRY %d/%d: %s" % (i, n_retries, e)
if i < n_retries:
print "******* WAITING %d seconds..." % (error_pause,)
time.sleep(error_pause)
else:
raise ValueError("FAIL after %d retries" % (n_retries,))
else:
return ret
评论列表
文章目录