File size: 9,338 Bytes
7cfdc4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from fastapi import APIRouter, HTTPException, Depends, Query
from typing import Optional
from pydantic import BaseModel
from core.auth import require_admin
from core.utils.logger import logger

router = APIRouter(prefix="/admin/sandbox-pool", tags=["admin", "sandbox-pool"])


class ForceCreateRequest(BaseModel):
    count: int = 1


class RemoveFromPoolRequest(BaseModel):
    sandbox_ids: list[str]
    delete_sandbox: bool = False


@router.get("/stats")
async def get_sandbox_pool_stats(admin: dict = Depends(require_admin)):
    try:
        from core.sandbox.pool_service import get_pool_service
        from core.sandbox.pool_config import get_pool_config
        from core.sandbox import pool_repo
        
        service = get_pool_service()
        config = get_pool_config()
        pool_size = await pool_repo.get_pool_size()
        
        stats = service.get_stats()
        stats['pool_size'] = pool_size
        stats['config'] = {
            'enabled': config.enabled,
            'min_size': config.min_size,
            'max_size': config.max_size,
            'replenish_threshold': config.replenish_threshold,
            'check_interval': config.check_interval,
            'max_age': config.max_age,
        }
        
        return stats
        
    except Exception as e:
        logger.error(f"Error getting sandbox pool stats: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/health")
async def get_pool_health(admin: dict = Depends(require_admin)):
    try:
        from core.sandbox.pool_background import is_pool_service_running
        from core.sandbox.pool_config import get_pool_config
        from core.sandbox import pool_repo
        
        config = get_pool_config()
        pool_size = await pool_repo.get_pool_size()
        service_running = is_pool_service_running()
        
        health_status = "healthy"
        issues = []
        
        if not config.enabled:
            health_status = "disabled"
            issues.append("Pool service is disabled via configuration")
        elif not service_running:
            health_status = "critical"
            issues.append("Background service is not running")
        elif pool_size == 0:
            health_status = "warning"
            issues.append("Pool is empty")
        elif pool_size < config.replenish_below:
            health_status = "warning"
            issues.append(f"Pool size ({pool_size}) below replenish threshold ({config.replenish_below})")
        
        return {
            "status": health_status,
            "service_running": service_running,
            "pool_enabled": config.enabled,
            "pool_size": pool_size,
            "min_size": config.min_size,
            "replenish_threshold": config.replenish_below,
            "issues": issues,
        }
        
    except Exception as e:
        logger.error(f"Error getting pool health: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/list")
async def list_pooled_sandboxes(
    limit: int = Query(50, ge=1, le=100),
    admin: dict = Depends(require_admin)
):
    try:
        from core.sandbox import pool_repo
        
        sandboxes = await pool_repo.get_pooled_sandboxes(limit=limit)
        
        return {
            "count": len(sandboxes),
            "sandboxes": [
                {
                    "id": s["id"],
                    "external_id": s["external_id"],
                    "pooled_at": s.get("pooled_at"),
                    "created_at": s.get("created_at"),
                }
                for s in sandboxes
            ]
        }
        
    except Exception as e:
        logger.error(f"Error listing pooled sandboxes: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/replenish")
async def trigger_pool_replenish(admin: dict = Depends(require_admin)):
    try:
        from core.sandbox.pool_service import get_pool_service
        from core.sandbox import pool_repo
        
        service = get_pool_service()
        size_before = await pool_repo.get_pool_size()
        created = await service.ensure_pool_size()
        size_after = await pool_repo.get_pool_size()
        
        return {
            "success": True,
            "sandboxes_created": created,
            "pool_size_before": size_before,
            "pool_size_after": size_after,
        }
        
    except Exception as e:
        logger.error(f"Error triggering pool replenish: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/force-create")
async def force_create_sandboxes(
    request_body: ForceCreateRequest,
    admin: dict = Depends(require_admin)
):
    try:
        from core.sandbox.pool_service import get_pool_service
        from core.sandbox import pool_repo
        import asyncio
        
        count = min(request_body.count, 10)
        
        service = get_pool_service()
        size_before = await pool_repo.get_pool_size()
        
        tasks = [service.create_pooled_sandbox() for _ in range(count)]
        results = await asyncio.gather(*tasks, return_exceptions=True)
        
        created = []
        failed = []
        for i, r in enumerate(results):
            if isinstance(r, Exception):
                failed.append(str(r))
            elif r is not None:
                created.append(r)
            else:
                failed.append(f"Sandbox {i+1} returned None")
        
        size_after = await pool_repo.get_pool_size()
        
        return {
            "success": True,
            "requested": count,
            "created_count": len(created),
            "created_ids": created,
            "failed_count": len(failed),
            "failed_errors": failed[:5],
            "pool_size_before": size_before,
            "pool_size_after": size_after,
        }
        
    except Exception as e:
        logger.error(f"Error force creating sandboxes: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/cleanup")
async def trigger_pool_cleanup(admin: dict = Depends(require_admin)):
    try:
        from core.sandbox.pool_service import get_pool_service
        from core.sandbox import pool_repo
        
        service = get_pool_service()
        size_before = await pool_repo.get_pool_size()
        cleaned = await service.cleanup_stale_sandboxes()
        size_after = await pool_repo.get_pool_size()
        
        return {
            "success": True,
            "cleaned_count": cleaned,
            "pool_size_before": size_before,
            "pool_size_after": size_after,
        }
        
    except Exception as e:
        logger.error(f"Error triggering pool cleanup: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/remove")
async def remove_from_pool(
    request_body: RemoveFromPoolRequest,
    admin: dict = Depends(require_admin)
):
    try:
        from core.sandbox import pool_repo
        from core.sandbox.sandbox import delete_sandbox
        
        removed = []
        failed = []
        
        for sandbox_id in request_body.sandbox_ids[:20]:
            try:
                resource = await pool_repo.get_sandbox_by_external_id(sandbox_id)
                if not resource:
                    failed.append({"id": sandbox_id, "error": "Not found"})
                    continue
                
                if resource.get("status") != "pooled":
                    failed.append({"id": sandbox_id, "error": f"Status is {resource.get('status')}, not pooled"})
                    continue
                
                if request_body.delete_sandbox:
                    try:
                        await delete_sandbox(sandbox_id)
                    except Exception as del_err:
                        logger.warning(f"Failed to delete sandbox {sandbox_id}: {del_err}")
                
                await pool_repo.mark_sandbox_deleted(resource["id"])
                removed.append(sandbox_id)
                
            except Exception as e:
                failed.append({"id": sandbox_id, "error": str(e)})
        
        return {
            "success": True,
            "removed_count": len(removed),
            "removed_ids": removed,
            "failed_count": len(failed),
            "failed": failed,
        }
        
    except Exception as e:
        logger.error(f"Error removing sandboxes from pool: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/restart-service")
async def restart_pool_service(admin: dict = Depends(require_admin)):
    try:
        from core.sandbox.pool_background import stop_pool_service, start_pool_service, is_pool_service_running
        
        was_running = is_pool_service_running()
        await stop_pool_service()
        await start_pool_service()
        is_running = is_pool_service_running()
        
        return {
            "success": True,
            "was_running": was_running,
            "is_running": is_running,
            "message": "Pool service restarted" if is_running else "Failed to restart pool service"
        }
        
    except Exception as e:
        logger.error(f"Error restarting pool service: {e}")
        raise HTTPException(status_code=500, detail=str(e))