Commit a6a44506 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Implement the "wait_for" and "wait_while" methods

Refs: #4624
parent 67dfe4a2
......@@ -497,13 +497,13 @@ class Client(Logged):
class Waiter(object):
def _wait(
self, item_id, current_status, get_status,
delay=1, max_wait=100, wait_cb=None):
"""Wait for item while its status is current_status
self, item_id, wait_status, get_status,
delay=1, max_wait=100, wait_cb=None, wait_for_status=False):
"""Wait while the item is still in wait_status or to reach it
:param server_id: integer (str or int)
:param current_status: (str)
:param wait_status: (str)
:param get_status: (method(self, item_id)) if called, returns
(status, progress %) If no way to tell progress, return None
......@@ -513,6 +513,8 @@ class Waiter(object):
:param wait_cb: (method(total steps)) returns a generator for
reporting progress or timeouts i.e., for a progress bar
:param wait_for_status: (bool) wait FOR (True) or wait WHILE (False)
:returns: (str) the new mode if successful, (bool) False if timed out
"""
status, progress = get_status(self, item_id)
......@@ -521,16 +523,17 @@ class Waiter(object):
wait_gen = wait_cb(max_wait // delay)
wait_gen.next()
if status != current_status:
if wait_cb:
try:
wait_gen.next()
except Exception:
pass
if wait_for_status ^ (status != wait_status):
# if wait_cb:
# try:
# wait_gen.next()
# except Exception:
# pass
return status
old_wait = total_wait = 0
while status == current_status and total_wait <= max_wait:
while (wait_for_status ^ (status == wait_status)) and (
total_wait <= max_wait):
if wait_cb:
try:
for i in range(total_wait - old_wait):
......@@ -549,4 +552,18 @@ class Waiter(object):
wait_gen.next()
except:
pass
return status if status != current_status else False
return status if (wait_for_status ^ (status != wait_status)) else False
def wait_for(
self, item_id, target_status, get_status,
delay=1, max_wait=100, wait_cb=None):
self._wait(
item_id, target_status, get_status, delay, max_wait, wait_cb,
wait_for_status=True)
def wait_while(
self, item_id, target_status, get_status,
delay=1, max_wait=100, wait_cb=None):
self._wait(
item_id, target_status, get_status, delay, max_wait, wait_cb,
wait_for_status=False)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment