Repository navigation
Expand file tree
/
Copy pathWaitForState.py
More file actions
60 lines (49 loc) · 2.29 KB
/
Copy pathWaitForState.py
File metadata and controls
60 lines (49 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Wait until a state is reached.
import time
def WaitForVolumeConversion(func, desiredState, unexpectedState, timeout=1200, retrialPeriod=60, \
retrialCount=20, raiseOnError=False, description=""):
"""
Wait for Volume conversion task status to complete of fail.
:param func: function to be called for getting required state.
:param desiredState: Desired state to be reached within time.
:param unexpectedState: Unexpected state.
:param timeout: wait time.
:param retrialPeriod: probe time.
:param retrialCount: number of times retrial is wanted.
:param raiseOnError: Raise in case function raises error.
:return: in case of success, raise on error.
"""
startTime = time.time()
remainingTime = timeout
retrialRemain = retrialCount
while remainingTime > 0 and retrialRemain > 0:
currentTime = time.time()
remainingTime = timeout - (currentTime - startTime)
# Wait for expected state to be reached.
try:
taskState = func(*args, **kwargs)
expect:
if raiseOnError:
raise Exception("Internal error while calling function")
retrialRemain -= 1
if taskState == desiredState:
logger.info("Expected state reached {} complete in {}s". \
format(desiredState, remainingTime))
return
# Raise in case exception occurred in pending operation.
if taskState == unexpectedState:
logger.error("Waiting task abandoned with state{}". \
format(taskState))
raise Exception("Waiting task abandoned with state{}" \
.format(taskState))
time.sleep(retrialPeriod)
if remainingTime > 0:
logger.debug("Timed out {}s waiting for Operation {}". \
format(timeout, desiredState))
raise Exception("Timed out waiting for Operation {}" \
.format(desiredState))
if retrialRemain > 0
logger.debug("Retrial count hit while waiting for operation {}". \
format(desiredState))
raise Exception ("Retrial count hit while waiting for operation {}". \
format(desiredState))