Skip to content

Commit

Permalink
Bug 1301785: update python/redo to 1.6; r=gps
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: BdSQshy2f0p

--HG--
extra : rebase_source : 2dc5a1a95cbf3a01e4b62251638cdf98a88bf193
  • Loading branch information
djmitche committed Oct 31, 2016
1 parent 10d3f8e commit ceadc4b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 24 deletions.
2 changes: 1 addition & 1 deletion python/redo/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 1.0
Name: redo
Version: 1.4
Version: 1.6
Summary: Utilities to retry Python callables.
Home-page: https://github.com/bhearsum/redo
Author: Ben Hearsum
Expand Down
2 changes: 1 addition & 1 deletion python/redo/redo.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 1.0
Name: redo
Version: 1.4
Version: 1.6
Summary: Utilities to retry Python callables.
Home-page: https://github.com/bhearsum/redo
Author: Ben Hearsum
Expand Down
3 changes: 2 additions & 1 deletion python/redo/redo.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
README
setup.cfg
setup.py
redo/__init__.py
redo/cmd.py
redo.egg-info/PKG-INFO
redo.egg-info/SOURCES.txt
redo.egg-info/dependency_links.txt
redo.egg-info/entry_points.txt
redo.egg-info/pbr.json
redo.egg-info/top_level.txt
60 changes: 41 additions & 19 deletions python/redo/redo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,36 @@ def retrier(attempts=5, sleeptime=10, max_sleeptime=300, sleepscale=1.5, jitter=
... break
... n += 1
... else:
... print "max tries hit"
... print("max tries hit")
max tries hit
"""
jitter = jitter or 0 # py35 barfs on the next line if jitter is None
if jitter > sleeptime:
# To prevent negative sleep times
raise Exception('jitter ({}) must be less than sleep time ({})'.format(jitter, sleeptime))

sleeptime_real = sleeptime
for _ in range(attempts):
log.debug("attempt %i/%i", _ + 1, attempts)
yield

yield sleeptime_real

if jitter:
sleeptime += random.randint(-jitter, jitter)
sleeptime = max(sleeptime, 0)

if _ == attempts - 1:
# Don't need to sleep the last time
break
log.debug("sleeping for %.2fs (attempt %i/%i)", sleeptime, _ + 1, attempts)
time.sleep(sleeptime)
sleeptime_real = sleeptime + random.randint(-jitter, jitter)
# our jitter should scale along with the sleeptime
jitter = int(jitter * sleepscale)
else:
sleeptime_real = sleeptime

sleeptime *= sleepscale
if sleeptime > max_sleeptime:
sleeptime = max_sleeptime

if sleeptime_real > max_sleeptime:
sleeptime_real = max_sleeptime

# Don't need to sleep the last time
if _ < attempts - 1:
log.debug("sleeping for %.2fs (attempt %i/%i)", sleeptime_real, _ + 1, attempts)
time.sleep(sleeptime_real)


def retry(action, attempts=5, sleeptime=60, max_sleeptime=5 * 60,
Expand Down Expand Up @@ -100,7 +112,7 @@ def retry(action, attempts=5, sleeptime=60, max_sleeptime=5 * 60,
consider using functools.partial or a lambda
function.
args (tuple): positional arguments to call `action` with
hwargs (dict): keyword arguments to call `action` with
kwargs (dict): keyword arguments to call `action` with
Returns:
Whatever action(*args, **kwargs) returns
Expand All @@ -114,7 +126,7 @@ def retry(action, attempts=5, sleeptime=60, max_sleeptime=5 * 60,
>>> def foo():
... global count
... count += 1
... print count
... print(count)
... if count < 3:
... raise ValueError("count is too small!")
... return "success!"
Expand All @@ -126,6 +138,16 @@ def retry(action, attempts=5, sleeptime=60, max_sleeptime=5 * 60,
"""
assert callable(action)
assert not cleanup or callable(cleanup)

action_name = getattr(action, '__name__', action)
if args or kwargs:
log_attempt_format = ("retry: calling %s with args: %s,"
" kwargs: %s, attempt #%%d"
% (action_name, args, kwargs))
else:
log_attempt_format = ("retry: calling %s, attempt #%%d"
% action_name)

if max_sleeptime < sleeptime:
log.debug("max_sleeptime %d less than sleeptime %d" % (
max_sleeptime, sleeptime))
Expand All @@ -135,15 +157,15 @@ def retry(action, attempts=5, sleeptime=60, max_sleeptime=5 * 60,
max_sleeptime=max_sleeptime, sleepscale=sleepscale,
jitter=jitter):
try:
log.info("retry: Calling %s with args: %s, kwargs: %s, "
"attempt #%d" % (action, str(args), str(kwargs), n))
logfn = log.info if n != 1 else log.debug
logfn(log_attempt_format, n)
return action(*args, **kwargs)
except retry_exceptions:
log.debug("retry: Caught exception: ", exc_info=True)
if cleanup:
cleanup()
if n == attempts:
log.info("retry: Giving up on %s" % action)
log.info("retry: Giving up on %s" % action_name)
raise
continue
finally:
Expand All @@ -168,7 +190,7 @@ def retriable(*retry_args, **retry_kwargs):
... def foo():
... global count
... count += 1
... print count
... print(count)
... if count < 3:
... raise ValueError("count too small")
... return "success!"
Expand Down Expand Up @@ -204,7 +226,7 @@ def retrying(func, *retry_args, **retry_kwargs):
>>> def foo():
... global count
... count += 1
... print count
... print(count)
... if count < 3:
... raise ValueError("count too small")
... return "success!"
Expand Down
3 changes: 3 additions & 0 deletions python/redo/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[wheel]
universal = 1

[egg_info]
tag_build =
tag_date = 0
Expand Down
8 changes: 6 additions & 2 deletions python/redo/setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from setuptools import setup
try:
from setuptools import setup
except ImportError:
from distutils.core import setup


setup(
name="redo",
version="1.4",
version="1.6",
description="Utilities to retry Python callables.",
author="Ben Hearsum",
author_email="[email protected]",
Expand Down

0 comments on commit ceadc4b

Please sign in to comment.