Skip to content

Commit bb62c75

Browse files
committed
remove unnecessary fields and methods
1 parent c8cebf0 commit bb62c75

7 files changed

Lines changed: 45 additions & 117 deletions

examples/01_create_sandbox.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ def main():
2121
api_token=api_token,
2222
)
2323

24-
# Check status
25-
status = sandbox.status()
24+
# Check health
2625
is_healthy = sandbox.is_healthy()
27-
print(f"Status: {status}, Healthy: {is_healthy}")
26+
print(f"Healthy: {is_healthy}")
2827

2928
# Test command
3029
result = sandbox.exec("echo 'Sandbox is ready!'")

examples/01_create_sandbox_async.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ async def main():
2222
api_token=api_token,
2323
)
2424

25-
# Check status
26-
status = await sandbox.status()
25+
# Check health
2726
is_healthy = await sandbox.is_healthy()
28-
print(f"Status: {status}, Healthy: {is_healthy}")
27+
print(f"Healthy: {is_healthy}")
2928

3029
# Test command
3130
result = await sandbox.exec("echo 'Sandbox is ready!'")

examples/02_create_sandbox_with_timing.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,13 @@ def main(run_long_tests=False):
8989
tracker.record("Sandbox creation", create_duration, "setup")
9090
print(f" ✓ took {create_duration:.1f}s")
9191

92-
# Check status with timing
93-
print(" → Checking sandbox status...")
94-
status_start = time.time()
95-
status = sandbox.status()
92+
# Check health with timing
93+
print(" → Checking sandbox health...")
94+
health_start = time.time()
9695
is_healthy = sandbox.is_healthy()
97-
status_duration = time.time() - status_start
98-
tracker.record("Status check", status_duration, "monitoring")
99-
print(f" ✓ took {status_duration:.1f}s")
96+
health_duration = time.time() - health_start
97+
tracker.record("Health check", health_duration, "monitoring")
98+
print(f" ✓ took {health_duration:.1f}s")
10099

101100
# Test command execution with timing
102101
print(" → Executing initial test command...")
@@ -123,14 +122,14 @@ def main(run_long_tests=False):
123122
tracker.record("Heavy computation", compute_duration, "long_tests")
124123
print(f" ✓ took {compute_duration:.1f}s")
125124

126-
# Long test 3: Multiple status checks
127-
print(" → [LONG TEST] Multiple status checks...")
125+
# Long test 3: Multiple health checks
126+
print(" → [LONG TEST] Multiple health checks...")
128127
multi_check_start = time.time()
129128
for i in range(5):
130-
sandbox.status()
129+
sandbox.is_healthy()
131130
time.sleep(0.5)
132131
multi_check_duration = time.time() - multi_check_start
133-
tracker.record("Multiple status checks (5x)", multi_check_duration, "long_tests")
132+
tracker.record("Multiple health checks (5x)", multi_check_duration, "long_tests")
134133
print(f" ✓ took {multi_check_duration:.1f}s")
135134

136135
except Exception as e:

examples/02_create_sandbox_with_timing_async.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,13 @@ async def main(run_long_tests=False):
9393
tracker.record("Sandbox creation", create_duration, "setup")
9494
print(f" ✓ took {create_duration:.1f}s")
9595

96-
# Check status with timing
97-
print(" → Checking sandbox status...")
98-
status_start = time.time()
99-
await sandbox.status()
96+
# Check health with timing
97+
print(" → Checking sandbox health...")
98+
health_start = time.time()
10099
await sandbox.is_healthy()
101-
status_duration = time.time() - status_start
102-
tracker.record("Status check", status_duration, "monitoring")
103-
print(f" ✓ took {status_duration:.1f}s")
100+
health_duration = time.time() - health_start
101+
tracker.record("Health check", health_duration, "monitoring")
102+
print(f" ✓ took {health_duration:.1f}s")
104103

105104
# Test command execution with timing
106105
print(" → Executing initial test command...")
@@ -129,15 +128,15 @@ async def main(run_long_tests=False):
129128
tracker.record("Heavy computation", compute_duration, "long_tests")
130129
print(f" ✓ took {compute_duration:.1f}s")
131130

132-
# Long test 3: Multiple status checks
133-
print(" → [LONG TEST] Multiple status checks...")
131+
# Long test 3: Multiple health checks
132+
print(" → [LONG TEST] Multiple health checks...")
134133
multi_check_start = time.time()
135134
for i in range(5):
136-
await sandbox.status()
135+
await sandbox.is_healthy()
137136
await asyncio.sleep(0.5)
138137
multi_check_duration = time.time() - multi_check_start
139138
tracker.record(
140-
"Multiple status checks (5x)", multi_check_duration, "long_tests"
139+
"Multiple health checks (5x)", multi_check_duration, "long_tests"
141140
)
142141
print(f" ✓ took {multi_check_duration:.1f}s")
143142

examples/15_get_sandbox.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ def main():
5050
assert original_sandbox.id == retrieved_sandbox.id, "Sandbox IDs should match!"
5151
print(" ✓ Confirmed: Same sandbox retrieved")
5252

53-
# Check status
54-
status = retrieved_sandbox.status()
53+
# Check health
5554
is_healthy = retrieved_sandbox.is_healthy()
56-
print(f" Status: {status}, Healthy: {is_healthy}")
55+
print(f" Healthy: {is_healthy}")
5756

5857
# Execute a command with the retrieved sandbox
5958
if is_healthy:

examples/15_get_sandbox_async.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ async def main():
5151
assert original_sandbox.id == retrieved_sandbox.id, "Sandbox IDs should match!"
5252
print(" ✓ Confirmed: Same sandbox retrieved")
5353

54-
# Check status
55-
status = await retrieved_sandbox.status()
54+
# Check health
5655
is_healthy = await retrieved_sandbox.is_healthy()
57-
print(f" Status: {status}, Healthy: {is_healthy}")
56+
print(f" Healthy: {is_healthy}")
5857

5958
# Execute a command with the retrieved sandbox
6059
if is_healthy:

koyeb/sandbox/sandbox.py

Lines changed: 17 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
create_koyeb_sandbox_routes,
3232
create_sandbox_client,
3333
get_api_client,
34-
is_sandbox_healthy,
3534
logger,
3635
run_sync_in_executor,
3736
validate_port,
@@ -80,15 +79,13 @@ def __init__(
8079
sandbox_id: str,
8180
app_id: str,
8281
service_id: str,
83-
instance_id: str,
8482
name: Optional[str] = None,
8583
api_token: Optional[str] = None,
8684
sandbox_secret: Optional[str] = None,
8785
):
8886
self.sandbox_id = sandbox_id
8987
self.app_id = app_id
9088
self.service_id = service_id
91-
self.instance_id = instance_id
9289
self.name = name
9390
self.api_token = api_token
9491
self.sandbox_secret = sandbox_secret
@@ -224,41 +221,11 @@ def _create_sync(
224221
create_service = CreateService(app_id=app_id, definition=deployment_definition)
225222
service_response = services_api.create_service(service=create_service)
226223
service_id = service_response.service.id
227-
deployment_id = service_response.service.latest_deployment_id
228-
229-
deployments_api = DeploymentsApi(services_api.api_client)
230-
231-
max_wait = min(timeout // 2, 60) if timeout > 60 else timeout
232-
wait_interval = 0.5
233-
start_time = time.time()
234-
235-
while time.time() - start_time < max_wait:
236-
try:
237-
scaling_response = deployments_api.get_deployment_scaling(
238-
id=deployment_id
239-
)
240-
241-
if scaling_response.replicas and scaling_response.replicas[0].instances:
242-
instance_id = scaling_response.replicas[0].instances[0].id
243-
break
244-
else:
245-
logger.debug(
246-
f"Waiting for instances to be created... (elapsed: {time.time() - start_time:.1f}s)"
247-
)
248-
time.sleep(wait_interval)
249-
except Exception as e:
250-
logger.warning(f"Error getting deployment scaling: {e}")
251-
time.sleep(wait_interval)
252-
else:
253-
raise SandboxError(
254-
f"No instances found in deployment after {max_wait} seconds"
255-
)
256224

257225
return cls(
258226
sandbox_id=name,
259227
app_id=app_id,
260228
service_id=service_id,
261-
instance_id=instance_id,
262229
name=name,
263230
api_token=api_token,
264231
sandbox_secret=sandbox_secret,
@@ -314,7 +281,6 @@ def get_from_id(
314281
# Get deployment to extract sandbox_secret from env vars
315282
deployment_id = service.active_deployment_id or service.latest_deployment_id
316283
sandbox_secret = None
317-
instance_id = None
318284

319285
if deployment_id:
320286
try:
@@ -329,36 +295,13 @@ def get_from_id(
329295
if env_var.key == "SANDBOX_SECRET":
330296
sandbox_secret = env_var.value
331297
break
332-
333-
# Get instance_id from deployment scaling
334-
try:
335-
scaling_response = deployments_api.get_deployment_scaling(
336-
id=deployment_id
337-
)
338-
if (
339-
scaling_response.replicas
340-
and scaling_response.replicas[0].instances
341-
and len(scaling_response.replicas[0].instances) > 0
342-
):
343-
instance_id = scaling_response.replicas[0].instances[0].id
344-
except Exception:
345-
logger.debug(
346-
f"Could not get instance for deployment {deployment_id}"
347-
)
348298
except Exception as e:
349299
logger.debug(f"Could not get deployment {deployment_id}: {e}")
350300

351-
if not instance_id:
352-
raise SandboxError(
353-
f"Could not find instance for sandbox {id}. "
354-
"The sandbox may not be fully provisioned yet."
355-
)
356-
357301
return cls(
358302
sandbox_id=service.id,
359303
app_id=service.app_id,
360304
service_id=service.id,
361-
instance_id=instance_id,
362305
name=sandbox_name,
363306
api_token=api_token,
364307
sandbox_secret=sandbox_secret,
@@ -391,12 +334,7 @@ def wait_ready(
391334
time.sleep(poll_interval)
392335
continue
393336

394-
is_healthy = is_sandbox_healthy(
395-
self.instance_id,
396-
sandbox_url=sandbox_url,
397-
sandbox_secret=self.sandbox_secret,
398-
api_token=self.api_token,
399-
)
337+
is_healthy = self.is_healthy()
400338

401339
if is_healthy:
402340
return True
@@ -561,22 +499,25 @@ def _check_response_error(self, response: Dict, operation: str) -> None:
561499
error_msg = response.get("error", "Unknown error")
562500
raise SandboxError(f"Failed to {operation}: {error_msg}")
563501

564-
def status(self) -> str:
565-
"""Get current sandbox status"""
566-
from .utils import get_sandbox_status
567-
568-
status = get_sandbox_status(self.instance_id, self.api_token)
569-
return status.value
570-
571502
def is_healthy(self) -> bool:
572503
"""Check if sandbox is healthy and ready for operations"""
573504
sandbox_url = self._get_sandbox_url()
574-
return is_sandbox_healthy(
575-
self.instance_id,
576-
sandbox_url=sandbox_url,
577-
sandbox_secret=self.sandbox_secret,
578-
api_token=self.api_token,
579-
)
505+
if not sandbox_url or not self.sandbox_secret:
506+
return False
507+
508+
# Check executor health directly - this is what matters for operations
509+
# If executor is healthy, the sandbox is usable (will wake up service if needed)
510+
try:
511+
from .executor_client import SandboxClient
512+
513+
client = SandboxClient(sandbox_url, self.sandbox_secret)
514+
health_response = client.health()
515+
if isinstance(health_response, dict):
516+
status = health_response.get("status", "").lower()
517+
return status in ["ok", "healthy", "ready"]
518+
return True # If we got a response, consider it healthy
519+
except Exception:
520+
return False
580521

581522
@property
582523
def filesystem(self) -> "SandboxFilesystem":
@@ -874,7 +815,6 @@ async def get_from_id(
874815
sandbox_id=sync_sandbox.sandbox_id,
875816
app_id=sync_sandbox.app_id,
876817
service_id=sync_sandbox.service_id,
877-
instance_id=sync_sandbox.instance_id,
878818
name=sync_sandbox.name,
879819
api_token=sync_sandbox.api_token,
880820
sandbox_secret=sync_sandbox.sandbox_secret,
@@ -952,7 +892,6 @@ async def create(
952892
sandbox_id=sync_result.sandbox_id,
953893
app_id=sync_result.app_id,
954894
service_id=sync_result.service_id,
955-
instance_id=sync_result.instance_id,
956895
name=sync_result.name,
957896
api_token=sync_result.api_token,
958897
sandbox_secret=sync_result.sandbox_secret,
@@ -1030,11 +969,6 @@ async def delete(self) -> None:
1030969
"""Delete the sandbox instance asynchronously."""
1031970
pass
1032971

1033-
@async_wrapper("status")
1034-
async def status(self) -> str:
1035-
"""Get current sandbox status asynchronously"""
1036-
pass
1037-
1038972
@async_wrapper("is_healthy")
1039973
async def is_healthy(self) -> bool:
1040974
"""Check if sandbox is healthy and ready for operations asynchronously"""

0 commit comments

Comments
 (0)