-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwaiting.py
More file actions
174 lines (140 loc) · 5.42 KB
/
waiting.py
File metadata and controls
174 lines (140 loc) · 5.42 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""Test utilities for waiting on Redis state conditions."""
import asyncio
import time
from typing import cast
from docket import Docket
from docket.execution import ExecutionState
async def wait_for_progress_data(
docket: Docket,
key: str,
*,
min_current: int = 1,
min_total: int = 1,
timeout: float = 2.0,
interval: float = 0.01,
) -> tuple[int, int]:
"""Wait for progress data to appear in Redis with specified values.
Args:
docket: Docket instance
key: Task key
min_current: Minimum current value to wait for
min_total: Minimum total value to wait for
timeout: Maximum time to wait in seconds
interval: Sleep interval between checks in seconds
Returns:
Tuple of (current, total) values once condition is met
Raises:
TimeoutError: If condition not met within timeout
"""
start_time = time.monotonic()
progress_key = f"{docket.name}:progress:{key}"
while time.monotonic() - start_time < timeout:
async with docket.redis() as redis:
data = await redis.hgetall(progress_key) # type: ignore[misc]
if data: # pragma: no branch
current = int(cast(bytes, data.get(b"current", b"0"))) # type: ignore[misc]
total = int(cast(bytes, data.get(b"total", b"100"))) # type: ignore[misc]
if current >= min_current and total >= min_total: # pragma: no branch
return (current, total)
await asyncio.sleep(interval)
raise TimeoutError( # pragma: no cover
f"Progress data did not reach min_current={min_current}, min_total={min_total} "
f"within {timeout}s for task {key}"
)
async def wait_for_execution_state(
docket: Docket,
key: str,
state: ExecutionState,
*,
timeout: float = 2.0,
interval: float = 0.01,
) -> None:
"""Wait for execution to reach specified state.
Args:
docket: Docket instance
key: Task key
state: Target execution state
timeout: Maximum time to wait in seconds
interval: Sleep interval between checks in seconds
Raises:
TimeoutError: If state not reached within timeout
"""
start_time = time.monotonic()
execution_key = f"{docket.name}:runs:{key}"
while time.monotonic() - start_time < timeout:
async with docket.redis() as redis:
data = await redis.hgetall(execution_key) # type: ignore[misc]
if data: # pragma: no branch
state_value = data.get(b"state") # type: ignore[misc]
if state_value: # pragma: no branch
current_state = ExecutionState(cast(bytes, state_value).decode())
if current_state == state: # pragma: no branch
return
await asyncio.sleep(interval)
raise TimeoutError( # pragma: no cover
f"Execution did not reach state {state.value} within {timeout}s for task {key}"
)
async def wait_for_worker_assignment(
docket: Docket,
key: str,
*,
timeout: float = 2.0,
interval: float = 0.01,
) -> str:
"""Wait for a worker to be assigned to the execution.
Args:
docket: Docket instance
key: Task key
timeout: Maximum time to wait in seconds
interval: Sleep interval between checks in seconds
Returns:
Worker name once assigned
Raises:
TimeoutError: If no worker assigned within timeout
"""
start_time = time.monotonic()
execution_key = f"{docket.name}:runs:{key}"
while time.monotonic() - start_time < timeout:
async with docket.redis() as redis:
data = await redis.hgetall(execution_key) # type: ignore[misc]
if data: # pragma: no branch
worker = data.get(b"worker") # type: ignore[misc]
if worker: # pragma: no branch
return cast(bytes, worker).decode()
await asyncio.sleep(interval)
raise TimeoutError( # pragma: no cover
f"No worker was assigned to task {key} within {timeout}s"
)
async def wait_for_watch_subscribed(
docket: Docket,
key: str,
*,
timeout: float = 3.0,
interval: float = 0.01,
) -> None:
"""Wait for watch command to subscribe to state channel.
Uses Redis PUBSUB NUMSUB to detect when watch has subscribed.
This ensures watch won't miss state events published after subscription.
Args:
docket: Docket instance
key: Task key
timeout: Maximum time to wait in seconds
interval: Sleep interval between checks in seconds
Raises:
TimeoutError: If watch doesn't subscribe within timeout
"""
start_time = time.monotonic()
state_channel = f"{docket.name}:state:{key}"
while time.monotonic() - start_time < timeout:
async with docket.redis() as redis:
result = await redis.pubsub_numsub(state_channel) # type: ignore[misc]
# Returns list of tuples: [(channel_bytes, count), ...]
for channel, count in result: # type: ignore[misc]
if isinstance(channel, bytes): # pragma: no branch
channel = channel.decode()
if channel == state_channel and count > 0: # pragma: no branch
return
await asyncio.sleep(interval)
raise TimeoutError( # pragma: no cover
f"Watch command did not subscribe to {state_channel} within {timeout}s"
)