Spaces:
Paused
Paused
Commit
·
d8be8d5
1
Parent(s):
7747993
feat: udapte timeout feature
Browse files- web_server.py +11 -33
web_server.py
CHANGED
|
@@ -151,32 +151,6 @@ def delete_client_space(client_space_id: str):
|
|
| 151 |
print(f"Failed to delete space {client_space_id}. It may not exist or already deleted.")
|
| 152 |
|
| 153 |
|
| 154 |
-
def get_env_id(team_id: str, submission_id: str) -> str:
|
| 155 |
-
"""
|
| 156 |
-
Generate a unique environment ID based on team ID and submission ID.
|
| 157 |
-
Args:
|
| 158 |
-
team_id (str): The team ID.
|
| 159 |
-
submission_id (str): The submission ID.
|
| 160 |
-
Returns:
|
| 161 |
-
str: The unique environment ID.
|
| 162 |
-
"""
|
| 163 |
-
return f"{team_id}____{submission_id}"
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
def parse_env_id(env_id: str) -> Tuple[str, str]:
|
| 167 |
-
"""
|
| 168 |
-
Parse the environment ID to extract team ID and submission ID.
|
| 169 |
-
Args:
|
| 170 |
-
env_id (str): The environment ID.
|
| 171 |
-
Returns:
|
| 172 |
-
Dict[str, str]: A dictionary containing team ID and submission ID.
|
| 173 |
-
"""
|
| 174 |
-
parts = env_id.split('____')
|
| 175 |
-
if len(parts) != 2:
|
| 176 |
-
raise ValueError("Invalid environment ID format.")
|
| 177 |
-
return parts[0], parts[1]
|
| 178 |
-
|
| 179 |
-
|
| 180 |
class FifoDict:
|
| 181 |
def __init__(self, max_size: int):
|
| 182 |
self.max_size = max_size
|
|
@@ -399,6 +373,7 @@ class EnvHandler:
|
|
| 399 |
class EnvHandlerManager:
|
| 400 |
def __init__(self):
|
| 401 |
self._env_handlers = {}
|
|
|
|
| 402 |
self._lock = threading.Lock()
|
| 403 |
threading.Thread(target=self._clean_expired_env_handlers, daemon=True).start()
|
| 404 |
|
|
@@ -452,7 +427,7 @@ class EnvHandlerManager:
|
|
| 452 |
with self._lock:
|
| 453 |
return env_id in self._env_handlers
|
| 454 |
|
| 455 |
-
def get_env_handler(self, env_id: str) -> EnvHandler:
|
| 456 |
"""
|
| 457 |
Get the environment handler for the given environment ID.
|
| 458 |
Args:
|
|
@@ -463,6 +438,7 @@ class EnvHandlerManager:
|
|
| 463 |
with self._lock:
|
| 464 |
if env_id not in self._env_handlers:
|
| 465 |
self._env_handlers[env_id] = self._generate_env_handler(env_id)
|
|
|
|
| 466 |
return self._env_handlers[env_id]
|
| 467 |
|
| 468 |
def close_env_handler(self, env_id: str):
|
|
@@ -493,8 +469,11 @@ class EnvHandlerManager:
|
|
| 493 |
]
|
| 494 |
for env_id in expired_env_ids:
|
| 495 |
self.close_env_handler(env_id)
|
| 496 |
-
|
| 497 |
-
|
|
|
|
|
|
|
|
|
|
| 498 |
except Exception as e:
|
| 499 |
print(f"Error in cleaning expired environment handlers: {e}")
|
| 500 |
time.sleep(15)
|
|
@@ -520,11 +499,10 @@ def _get_env_handler(
|
|
| 520 |
|
| 521 |
submission_id = token_info["submission_id"]
|
| 522 |
team_id = token_info["team_id"]
|
| 523 |
-
|
| 524 |
-
if not env_manager.exists_env_handler(env_id):
|
| 525 |
update_submission_data(team_id, submission_id, {"status": SubmissionStatus.PROCESSING.value})
|
| 526 |
|
| 527 |
-
env_handler = env_manager.get_env_handler(
|
| 528 |
if env_handler is None:
|
| 529 |
raise HTTPException(status_code=404, detail="Environment handler already closed.")
|
| 530 |
return env_handler
|
|
@@ -590,7 +568,7 @@ def execute_action_endpoint(
|
|
| 590 |
execute_result = env_handler.execute_action(plan_traj)
|
| 591 |
if execute_result.done:
|
| 592 |
token_info = get_token_info(auth_token)
|
| 593 |
-
env_manager.close_env_handler(
|
| 594 |
delete_client_space(token_info["client_space_id"])
|
| 595 |
final_score = env_handler.calculate_score()
|
| 596 |
update_submission_data(token_info["team_id"], token_info["submission_id"], {"status": SubmissionStatus.SUCCESS.value, "score": final_score})
|
|
|
|
| 151 |
print(f"Failed to delete space {client_space_id}. It may not exist or already deleted.")
|
| 152 |
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
class FifoDict:
|
| 155 |
def __init__(self, max_size: int):
|
| 156 |
self.max_size = max_size
|
|
|
|
| 373 |
class EnvHandlerManager:
|
| 374 |
def __init__(self):
|
| 375 |
self._env_handlers = {}
|
| 376 |
+
self._token_info_map = {}
|
| 377 |
self._lock = threading.Lock()
|
| 378 |
threading.Thread(target=self._clean_expired_env_handlers, daemon=True).start()
|
| 379 |
|
|
|
|
| 427 |
with self._lock:
|
| 428 |
return env_id in self._env_handlers
|
| 429 |
|
| 430 |
+
def get_env_handler(self, env_id: str, token_info: Dict[str, Any]) -> EnvHandler:
|
| 431 |
"""
|
| 432 |
Get the environment handler for the given environment ID.
|
| 433 |
Args:
|
|
|
|
| 438 |
with self._lock:
|
| 439 |
if env_id not in self._env_handlers:
|
| 440 |
self._env_handlers[env_id] = self._generate_env_handler(env_id)
|
| 441 |
+
self._token_info_map[env_id] = token_info
|
| 442 |
return self._env_handlers[env_id]
|
| 443 |
|
| 444 |
def close_env_handler(self, env_id: str):
|
|
|
|
| 469 |
]
|
| 470 |
for env_id in expired_env_ids:
|
| 471 |
self.close_env_handler(env_id)
|
| 472 |
+
token_info = self._token_info_map.pop(env_id, None)
|
| 473 |
+
if token_info:
|
| 474 |
+
update_submission_data(token_info["team_id"], token_info["submission_id"], {"status": SubmissionStatus.FAILED.value, "error_message": "SPACE_TIMEOUT"})
|
| 475 |
+
delete_client_space(token_info["client_space_id"])
|
| 476 |
+
|
| 477 |
except Exception as e:
|
| 478 |
print(f"Error in cleaning expired environment handlers: {e}")
|
| 479 |
time.sleep(15)
|
|
|
|
| 499 |
|
| 500 |
submission_id = token_info["submission_id"]
|
| 501 |
team_id = token_info["team_id"]
|
| 502 |
+
if not env_manager.exists_env_handler(submission_id):
|
|
|
|
| 503 |
update_submission_data(team_id, submission_id, {"status": SubmissionStatus.PROCESSING.value})
|
| 504 |
|
| 505 |
+
env_handler = env_manager.get_env_handler(submission_id, token_info)
|
| 506 |
if env_handler is None:
|
| 507 |
raise HTTPException(status_code=404, detail="Environment handler already closed.")
|
| 508 |
return env_handler
|
|
|
|
| 568 |
execute_result = env_handler.execute_action(plan_traj)
|
| 569 |
if execute_result.done:
|
| 570 |
token_info = get_token_info(auth_token)
|
| 571 |
+
env_manager.close_env_handler(token_info["submission_id"])
|
| 572 |
delete_client_space(token_info["client_space_id"])
|
| 573 |
final_score = env_handler.calculate_score()
|
| 574 |
update_submission_data(token_info["team_id"], token_info["submission_id"], {"status": SubmissionStatus.SUCCESS.value, "score": final_score})
|