Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions koyeb/sandbox/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def create(
_experimental_enable_light_sleep: bool = False,
delete_after_delay: int = 0,
delete_after_inactivity_delay: int = 0,
app_id: Optional[str] = None,
) -> Sandbox:
"""
Create a new sandbox instance.
Expand Down Expand Up @@ -148,6 +149,7 @@ def create(
after this many seconds since creation.
delete_after_sleep: If >0, automatically delete the sandbox if service sleeps due to inactivity
after this many seconds.
app_id: If provided, create the sandbox service in an existing app instead of creating a new one.

Returns:
Sandbox: A new Sandbox instance
Expand Down Expand Up @@ -189,6 +191,7 @@ def create(
_experimental_enable_light_sleep=_experimental_enable_light_sleep,
delete_after_delay=delete_after_delay,
delete_after_inactivity_delay=delete_after_inactivity_delay,
app_id=app_id,
)

if wait_ready:
Expand Down Expand Up @@ -220,6 +223,7 @@ def _create_sync(
_experimental_enable_light_sleep: bool = False,
delete_after_delay: int = 0,
delete_after_inactivity_delay: int = 0,
app_id: Optional[str] = None,
) -> Sandbox:
"""
Synchronous creation method that returns creation parameters.
Expand All @@ -239,13 +243,15 @@ def _create_sync(
env = {}
env["SANDBOX_SECRET"] = sandbox_secret

app_name = f"sandbox-app-{name}-{int(time.time())}"
app_response = apps_api.create_app(
app=CreateApp(
name=app_name, life_cycle=AppLifeCycle(delete_when_empty=True)
# Use provided app_id or create a new app
if app_id is None:
app_name = f"sandbox-app-{name}-{int(time.time())}"
app_response = apps_api.create_app(
app=CreateApp(
name=app_name, life_cycle=AppLifeCycle(delete_when_empty=True)
)
)
)
app_id = app_response.app.id
app_id = app_response.app.id

env_vars = build_env_vars(env)
docker_source = create_docker_source(
Expand Down Expand Up @@ -804,7 +810,7 @@ def kill_all_processes(self) -> int:
pass
return killed_count

def update_life_cycle(
def update_lifecycle(
self,
delete_after_delay: Optional[int] = None,
delete_after_inactivity: Optional[int] = None,
Expand Down Expand Up @@ -948,6 +954,7 @@ async def create(
_experimental_enable_light_sleep: bool = False,
delete_after_delay: int = 0,
delete_after_inactivity_delay: int = 0,
app_id: Optional[str] = None,
) -> AsyncSandbox:
"""
Create a new sandbox instance with async support.
Expand Down Expand Up @@ -979,6 +986,7 @@ async def create(
after this many seconds since creation.
delete_after_inactivity_delay: If >0, automatically delete the sandbox if service sleeps due to inactivity
after this many seconds.
app_id: If provided, create the sandbox service in an existing app instead of creating a new one.

Returns:
AsyncSandbox: A new AsyncSandbox instance
Expand Down Expand Up @@ -1013,6 +1021,7 @@ async def create(
_experimental_enable_light_sleep=_experimental_enable_light_sleep,
delete_after_delay=delete_after_delay,
delete_after_inactivity_delay=delete_after_inactivity_delay,
app_id=app_id,
),
)

Expand Down Expand Up @@ -1167,6 +1176,15 @@ async def kill_all_processes(self) -> int:
pass
return killed_count

@async_wrapper("update_lifecycle")
async def update_lifecycle(
self,
delete_after_delay: Optional[int] = None,
delete_after_inactivity: Optional[int] = None,
) -> None:
"""Update the sandbox's life cycle settings asynchronously."""
pass

async def __aenter__(self) -> "AsyncSandbox":
"""Async context manager entry - returns self."""
return self
Expand Down