runbot/runbot_merge/utils.py
Xavier Morel 7598d45283 [IMP] runbot_merge: use exponential backoff on head check
It's a waste to lose the entire staging if it's only a short blip /
delay thing, so retry multiple times. Add utility function to make
backoff functions easier (though the UI is not great ATM).

Also log the "left" parent of a merge commit (which should be the
"base") when creating it, for additional post-mortem information.
2019-11-07 10:03:54 +01:00

33 lines
972 B
Python

# -*- coding: utf-8 -*-
import itertools
import time
def shorten(text_ish, length):
""" If necessary, cuts-off the text or bytes input and appends ellipsis to
signal the cutoff, such that the result is below the provided length
(according to whatever "len" means on the text-ish so bytes or codepoints
or code units).
"""
if len(text_ish or ()) <= length:
return text_ish
cont = '...'
if isinstance(text_ish, bytes):
cont = cont.encode('ascii') # whatever
# add enough room for the ellipsis
return text_ish[:length-3] + cont
BACKOFF_DELAYS = (0.1, 0.2, 0.4, 0.8, 1.6)
def backoff(func=None, *, delays=BACKOFF_DELAYS, exc=Exception):
if func is None:
return lambda func: backoff(func, delays=delays, exc=exc)
for delay in itertools.chain(delays, [None]):
try:
return func()
except exc:
if delay is None:
raise
time.sleep(delay)