Skip to content

Commit b1f87ed

Browse files
kesmit13claude
andcommitted
feature: Loop on active workspace until endpoint is ready
When creating a new workspace with wait_on_active=True, the system now waits for the workspace state to become Active, and then verifies that the endpoint is actually ready by attempting to establish a connection. This ensures that the workspace is truly ready for use before returning to the caller. Added _wait_on_endpoint method in WorkspaceManager that polls the workspace endpoint with configurable interval and timeout, using a context manager to properly handle database connections. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 5f9cada commit b1f87ed

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

singlestoredb/management/manager.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,58 @@ def _wait_on_state(
310310
out = getattr(self, f'get_{self.obj_type}')(out.id)
311311

312312
return out
313+
314+
def _wait_on_endpoint(
315+
self,
316+
out: Any,
317+
interval: int = 10,
318+
timeout: int = 300,
319+
) -> Any:
320+
"""
321+
Wait for the endpoint to be ready by attempting to connect.
322+
323+
Parameters
324+
----------
325+
out : Any
326+
Workspace object with an endpoint attribute
327+
interval : int, optional
328+
Interval between each connection attempt (default: 10 seconds)
329+
timeout : int, optional
330+
Maximum time to wait before raising an exception (default: 300 seconds)
331+
332+
Raises
333+
------
334+
ManagementError
335+
If timeout is reached or endpoint is not available
336+
337+
Returns
338+
-------
339+
Same object type as `out`
340+
341+
"""
342+
if not hasattr(out, 'endpoint') or not out.endpoint:
343+
raise ManagementError(
344+
msg=f'{type(out).__name__} object does not have a valid endpoint',
345+
)
346+
347+
# Import connection module here to avoid circular imports
348+
from .. import connection
349+
350+
while True:
351+
try:
352+
# Try to establish a connection to the endpoint using context manager
353+
with connection.connect(host=out.endpoint, connect_timeout=5):
354+
pass
355+
# If connection succeeds, endpoint is ready
356+
break
357+
except Exception:
358+
# If connection fails, check timeout and retry
359+
if timeout <= 0:
360+
raise ManagementError(
361+
msg=f'Exceeded waiting time for {self.obj_type} endpoint '
362+
'to become ready',
363+
)
364+
time.sleep(interval)
365+
timeout -= interval
366+
367+
return out

singlestoredb/management/workspace.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,12 @@ def create_workspace(
17941794
interval=wait_interval,
17951795
timeout=wait_timeout,
17961796
)
1797+
# After workspace is active, wait for endpoint to be ready
1798+
out = self._wait_on_endpoint(
1799+
out,
1800+
interval=wait_interval,
1801+
timeout=wait_timeout,
1802+
)
17971803
return out
17981804

17991805
def get_workspace_group(self, id: str) -> WorkspaceGroup:

0 commit comments

Comments
 (0)