|
37 | 37 | from rock.sandbox.base_manager import BaseManager |
38 | 38 | from rock.sandbox.operator.abstract import AbstractOperator |
39 | 39 | from rock.sandbox.sandbox_actor import SandboxActor |
| 40 | +from rock.sandbox.service.sandbox_proxy_service import SandboxProxyService |
40 | 41 | from rock.sdk.common.exceptions import BadRequestRockError, InternalServerRockError |
41 | 42 | from rock.utils import ( |
42 | 43 | EAGLE_EYE_TRACE_ID, |
@@ -71,6 +72,7 @@ def __init__( |
71 | 72 | self._ray_namespace = ray_namespace |
72 | 73 | self._operator = operator |
73 | 74 | self._aes_encrypter = AESEncryption() |
| 75 | + self._proxy_service = SandboxProxyService(rock_config=rock_config, redis_provider=redis_provider) |
74 | 76 | if redis_provider: |
75 | 77 | self._operator.set_redis_provider(redis_provider) |
76 | 78 | logger.info("sandbox service init success") |
@@ -318,63 +320,28 @@ async def get_remote_status(self, sandbox_id: str, host_ip: str) -> ServiceStatu |
318 | 320 | return ServiceStatus() |
319 | 321 |
|
320 | 322 | async def create_session(self, request: CreateSessionRequest) -> CreateBashSessionResponse: |
321 | | - actor_name = self.deployment_manager.get_actor_name(request.sandbox_id) |
322 | | - sandbox_actor = await self._ray_service.async_ray_get_actor(actor_name, self._ray_namespace) |
323 | | - if sandbox_actor is None: |
324 | | - raise Exception(f"sandbox {request.sandbox_id} not found to create session") |
325 | | - await self._update_expire_time(request.sandbox_id) |
326 | | - return await self._ray_service.async_ray_get(sandbox_actor.create_session.remote(request)) |
| 323 | + return await self._proxy_service.create_session(request) |
327 | 324 |
|
328 | 325 | @monitor_sandbox_operation() |
329 | 326 | async def run_in_session(self, action: Action) -> BashObservation: |
330 | | - actor_name = self.deployment_manager.get_actor_name(action.sandbox_id) |
331 | | - sandbox_actor = await self._ray_service.async_ray_get_actor(actor_name, self._ray_namespace) |
332 | | - if sandbox_actor is None: |
333 | | - raise Exception(f"sandbox {action.sandbox_id} not found to run in session") |
334 | | - await self._update_expire_time(action.sandbox_id) |
335 | | - return await self._ray_service.async_ray_get(sandbox_actor.run_in_session.remote(action)) |
| 327 | + return await self._proxy_service.run_in_session(action) |
336 | 328 |
|
337 | 329 | async def close_session(self, request: CloseBashSessionRequest) -> CloseBashSessionResponse: |
338 | | - actor_name = self.deployment_manager.get_actor_name(request.sandbox_id) |
339 | | - sandbox_actor = await self._ray_service.async_ray_get_actor(actor_name, self._ray_namespace) |
340 | | - if sandbox_actor is None: |
341 | | - raise Exception(f"sandbox {request.sandbox_id} not found to close session") |
342 | | - await self._update_expire_time(request.sandbox_id) |
343 | | - return await self._ray_service.async_ray_get(sandbox_actor.close_session.remote(request)) |
| 330 | + return await self._proxy_service.close_session(request) |
344 | 331 |
|
345 | 332 | async def execute(self, command: Command) -> CommandResponse: |
346 | | - actor_name = self.deployment_manager.get_actor_name(command.sandbox_id) |
347 | | - sandbox_actor = await self._ray_service.async_ray_get_actor(actor_name, self._ray_namespace) |
348 | | - if sandbox_actor is None: |
349 | | - raise Exception(f"sandbox {command.sandbox_id} not found to execute") |
350 | | - await self._update_expire_time(command.sandbox_id) |
351 | | - return await self._ray_service.async_ray_get(sandbox_actor.execute.remote(command)) |
| 333 | + return await self._proxy_service.execute(command) |
352 | 334 |
|
353 | 335 | async def read_file(self, request: ReadFileRequest) -> ReadFileResponse: |
354 | | - actor_name = self.deployment_manager.get_actor_name(request.sandbox_id) |
355 | | - sandbox_actor = await self._ray_service.async_ray_get_actor(actor_name, self._ray_namespace) |
356 | | - if sandbox_actor is None: |
357 | | - raise Exception(f"sandbox {request.sandbox_id} not found to read file") |
358 | | - await self._update_expire_time(request.sandbox_id) |
359 | | - return await self._ray_service.async_ray_get(sandbox_actor.read_file.remote(request)) |
| 336 | + return await self._proxy_service.read_file(request) |
360 | 337 |
|
361 | 338 | @monitor_sandbox_operation() |
362 | 339 | async def write_file(self, request: WriteFileRequest) -> WriteFileResponse: |
363 | | - actor_name = self.deployment_manager.get_actor_name(request.sandbox_id) |
364 | | - sandbox_actor = await self._ray_service.async_ray_get_actor(actor_name, self._ray_namespace) |
365 | | - if sandbox_actor is None: |
366 | | - raise Exception(f"sandbox {request.sandbox_id} not found to write file") |
367 | | - await self._update_expire_time(request.sandbox_id) |
368 | | - return await self._ray_service.async_ray_get(sandbox_actor.write_file.remote(request)) |
| 340 | + return await self._proxy_service.write_file(request) |
369 | 341 |
|
370 | 342 | @monitor_sandbox_operation() |
371 | 343 | async def upload(self, file: UploadFile, target_path: str, sandbox_id: str) -> UploadResponse: |
372 | | - actor_name = self.deployment_manager.get_actor_name(sandbox_id) |
373 | | - sandbox_actor = await self._ray_service.async_ray_get_actor(actor_name, self._ray_namespace) |
374 | | - if sandbox_actor is None: |
375 | | - raise Exception(f"sandbox {sandbox_id} not found to upload file") |
376 | | - await self._update_expire_time(sandbox_id) |
377 | | - return await self._ray_service.async_ray_get(sandbox_actor.upload.remote(file, target_path)) |
| 344 | + return await self._proxy_service.upload(file, target_path, sandbox_id) |
378 | 345 |
|
379 | 346 | async def _is_expired(self, sandbox_id): |
380 | 347 | timeout_dict = await self._redis_provider.json_get(timeout_sandbox_key(sandbox_id), "$") |
|
0 commit comments