mirror of
https://github.com/langgenius/dify.git
synced 2026-03-06 06:35:24 -05:00
- Moved sandbox-related classes and functions into a dedicated module for better organization. - Updated the sandbox initialization process to streamline asset management and environment setup. - Removed deprecated constants and refactored related code to utilize new sandbox entities. - Enhanced the workflow context to support sandbox integration, allowing for improved state management during execution. - Adjusted various components to utilize the new sandbox structure, ensuring compatibility across the application.
74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from core.sandbox.storage.sandbox_storage import SandboxStorage
|
|
from core.virtual_environment.__base.virtual_environment import VirtualEnvironment
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Sandbox:
|
|
def __init__(
|
|
self,
|
|
*,
|
|
vm: VirtualEnvironment,
|
|
storage: SandboxStorage,
|
|
tenant_id: str,
|
|
user_id: str,
|
|
app_id: str,
|
|
assets_id: str,
|
|
) -> None:
|
|
self._vm = vm
|
|
self._storage = storage
|
|
self._tenant_id = tenant_id
|
|
self._user_id = user_id
|
|
self._app_id = app_id
|
|
self._assets_id = assets_id
|
|
|
|
@property
|
|
def vm(self) -> VirtualEnvironment:
|
|
return self._vm
|
|
|
|
@property
|
|
def storage(self) -> SandboxStorage:
|
|
return self._storage
|
|
|
|
@property
|
|
def tenant_id(self) -> str:
|
|
return self._tenant_id
|
|
|
|
@property
|
|
def user_id(self) -> str:
|
|
return self._user_id
|
|
|
|
@property
|
|
def app_id(self) -> str:
|
|
return self._app_id
|
|
|
|
@property
|
|
def assets_id(self) -> str:
|
|
return self._assets_id
|
|
|
|
def mount(self) -> bool:
|
|
return self._storage.mount(self._vm)
|
|
|
|
def unmount(self) -> bool:
|
|
return self._storage.unmount(self._vm)
|
|
|
|
def release(self) -> None:
|
|
sandbox_id = self._vm.metadata.id
|
|
try:
|
|
self._storage.unmount(self._vm)
|
|
logger.info("Sandbox storage unmounted: sandbox_id=%s", sandbox_id)
|
|
except Exception:
|
|
logger.exception("Failed to unmount sandbox storage: sandbox_id=%s", sandbox_id)
|
|
|
|
try:
|
|
self._vm.release_environment()
|
|
logger.info("Sandbox released: sandbox_id=%s", sandbox_id)
|
|
except Exception:
|
|
logger.exception("Failed to release sandbox: sandbox_id=%s", sandbox_id)
|