diff options
author | Valerii Ponomarov <vponomar@redhat.com> | 2019-03-07 20:30:44 +0530 |
---|---|---|
committer | vponomar <vponomar@redhat.com> | 2019-03-18 11:34:37 +0000 |
commit | 32b611b2a6498b1de307142e335e09d1e0ec082c (patch) | |
tree | aaf600ab6e6adabab7c3facbf30ae6f056731969 /openshift-storage-libs/openshiftstoragelibs/waiter.py | |
parent | 0fcdb081517c5904969b89b20326d21b361e448e (diff) |
Reorder lib files removing redundant dir layer
Move all the files of 'cns-libs/cnslibs/common' dir
to the 'openshift-storage-libs/openshiftstoragelibs',
because 'common' is the only dir there, which doesn't really makes sense.
And "cns" is old project name, so, replace it with
"openshift-storage-libs".
Also, fix all the imports of these libs.
Change-Id: Ife00a73554e73b21b214b15016b0c8dbbf423446
Diffstat (limited to 'openshift-storage-libs/openshiftstoragelibs/waiter.py')
-rw-r--r-- | openshift-storage-libs/openshiftstoragelibs/waiter.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/openshift-storage-libs/openshiftstoragelibs/waiter.py b/openshift-storage-libs/openshiftstoragelibs/waiter.py new file mode 100644 index 00000000..0d0c8c3a --- /dev/null +++ b/openshift-storage-libs/openshiftstoragelibs/waiter.py @@ -0,0 +1,38 @@ +"""Helper object to encapsulate waiting for timeouts. + +Provide a Waiter class which encapsulates the operation +of doing an action in a loop until a timeout values elapses. +It aims to avoid having to write boilerplate code comparing times. +""" + +import time + + +class Waiter(object): + """A wait-retry loop as iterable. + This object abstracts away the wait logic allowing functions + to write the retry logic in a for-loop. + """ + def __init__(self, timeout=60, interval=1): + self.timeout = timeout + self.interval = interval + self.expired = False + self._attempt = 0 + self._start = None + + def __iter__(self): + return self + + def next(self): + if self._start is None: + self._start = time.time() + if time.time() - self._start > self.timeout: + self.expired = True + raise StopIteration() + if self._attempt != 0: + time.sleep(self.interval) + self._attempt += 1 + return self + + # NOTE(vponomar): py3 uses "__next__" method instead of "next" one. + __next__ = next |