Saggi Mizrahi has uploaded a new change for review.
Change subject: Add callback to the plethora of retry halting possibilities
......................................................................
Add callback to the plethora of retry halting possibilities
Change-Id: Idb5a2158f008b41133352dcfb4926ad21dcceea1
Signed-off-by: Saggi Mizrahi <smizrahi(a)redhat.com>
---
M tests/miscTests.py
M vdsm/storage/misc.py
2 files changed, 35 insertions(+), 1 deletion(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/93/8093/1
diff --git a/tests/miscTests.py b/tests/miscTests.py
index bec6539..c8bcf09 100644
--- a/tests/miscTests.py
+++ b/tests/miscTests.py
@@ -621,6 +621,35 @@
self.assertEquals(misc._alignData(1, 1), (1, 1, 1))
+class RetryTests(TestCaseBase):
+ def testStopCallback(self):
+ counter = [0]
+ limit = 4
+ def stopCallback():
+ counter[0] += 1
+ if counter[0] == limit:
+ return True
+
+ return False
+
+ def foo():
+ print counter[0]
+ if counter[0] == (limit - 1):
+ return
+
+ raise RuntimeError("If at first you don't succeed, try, try
again."
+ "Then quit. There's no point in being a
damn"
+ "fool about it.")
+ # W. C. Fields
+
+ self.assertRaises(RuntimeError, misc.retry, foo, tries=(limit - 1), sleep=0,
+ stopCallback=stopCallback)
+
+ counter[0] = 0
+ misc.retry(foo, RuntimeError, tries=limit, sleep=0,
+ stopCallback=stopCallback)
+
+
class ValidateDDBytes(TestCaseBase):
def testValidInputTrue(self):
"""
diff --git a/vdsm/storage/misc.py b/vdsm/storage/misc.py
index 161726b..ef0c74e 100644
--- a/vdsm/storage/misc.py
+++ b/vdsm/storage/misc.py
@@ -728,7 +728,7 @@
def retry(func, expectedException=Exception, tries=None,
- timeout=None, sleep=1):
+ timeout=None, sleep=1, stopCallback=None):
"""
Retry a function. Wraps the retry logic so you don't have to
implement it each time you need it.
@@ -741,6 +741,8 @@
the method. It will just not run it if it ended after the
timeout.
:param sleep: Time to sleep between calls in seconds.
+ :param stopCallback: A function that takes no parameters and invokes a
+ bail-out when it returns with a positive value.
"""
if tries in [0, None]:
tries = -1
@@ -761,6 +763,9 @@
if (timeout > 0) and ((time.time() - startTime) > timeout):
raise
+ if stopCallback is not None and stopCallback():
+ raise
+
time.sleep(sleep)
--
To view, visit
http://gerrit.ovirt.org/8093
To unsubscribe, visit
http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Idb5a2158f008b41133352dcfb4926ad21dcceea1
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Saggi Mizrahi <smizrahi(a)redhat.com>