forked from postgrespro/testgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
65 lines (52 loc) · 1.86 KB
/
Copy pathutils.py
File metadata and controls
65 lines (52 loc) · 1.86 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
61
62
63
64
65
import typing
import time
import logging
T_WAIT_TIME = typing.Union[int, float]
class Utils:
@staticmethod
def PrintAndSleep(wait: T_WAIT_TIME):
assert type(wait) in [int, float]
logging.info("Wait for {} second(s)".format(wait))
time.sleep(wait)
return
@staticmethod
def WaitUntil(
error_message: str = "Did not complete",
timeout: T_WAIT_TIME = 30,
interval: T_WAIT_TIME = 1,
notification_interval: T_WAIT_TIME = 5,
):
"""
Loop until the timeout is reached. If the timeout is reached, raise an
exception with the given error message.
Source of idea: pgbouncer
"""
assert type(timeout) in [int, float]
assert type(interval) in [int, float]
assert type(notification_interval) in [int, float]
assert timeout >= 0
assert interval >= 0
assert notification_interval >= 0
start_ts = time.monotonic()
end_ts = start_ts + timeout
last_printed_progress = start_ts
last_iteration_ts = start_ts
yield
attempt = 1
while end_ts > time.monotonic():
if (timeout > 5 and time.monotonic() - last_printed_progress) > notification_interval:
last_printed_progress = time.monotonic()
m = "{} in {} seconds and {} attempts - will retry".format(
error_message,
time.monotonic() - start_ts,
attempt,
)
logging.info(m)
interval_remaining = last_iteration_ts + interval - time.monotonic()
if interval_remaining > 0:
time.sleep(interval_remaining)
last_iteration_ts = time.monotonic()
yield
attempt += 1
continue
raise TimeoutError(error_message + " in time")