跳转到内容
返回主页

生命周期、启停与重连

沙箱会经历创建、就绪、运行操作和清理阶段。所有运行时操作都应基于 create/connect 返回的 SDK 对象进行。

create -> starting -> running -> pause/connect or refresh -> delete
动作使用时机
createbasecode-interpretertpl-... 分配运行时。
waitReady下一步立即需要文件、命令或端口能力。
connect用已有 Sandbox ID 恢复运行时 URL/Token。
pause停止运行资源,但保留元数据供后续重连。
refresh为活跃任务延长 TTL。
setTimeout修改秒级生命周期超时。
delete永久清理一次性任务。

API 响应只暴露产品安全状态。Atlas 内部的 newwarmingwarmcreatingactivestoppeddestroying 等状态会在返回给公开客户端前折叠。

State/status含义
starting运行时正在创建,或等待健康心跳。
running运行时 API 可用。
pausedRuntime 已停止,但持久元数据保留,可 connect/resume。
deleted沙箱已移除。
failed / error运行时未能变为可用,或生命周期操作失败。

沙箱创建、列表和详情响应可能包含 timelinediagnostic。面向用户的进度和排障文案应优先使用这些字段,而不是解析原始日志。

参数单位说明
templateIDstring必填模板引用。
timeout沙箱生命周期 TTL。
waitReadyboolean等待运行时进入可用状态。
envVarsmap运行时环境变量覆盖。
metadatamap用户侧元数据,便于搜索和调试。
autoPauseboolean支持时可将超时动作映射为 pause 而不是 kill。

不要发送过期兼容字段,例如 timeoutMsautoResumesecureallow_internet_accessnetworkmcpvolume_mounts 或请求级 volumeMounts。这些字段会被拒绝,而不是静默忽略。

const sandbox = await Sandbox.create("base", {
timeout: 1800,
waitReady: true,
envVars: { FOO: "bar" },
metadata: { purpose: "demo" },
});
const sandboxId = sandbox.sandboxId;
await sandbox.connect({ timeout: 1800 });
// 或在另一个进程中:
const reconnected = await Sandbox.connect(sandboxId, { timeout: 1800 });
await sandbox.setTimeout(3600);
await sandbox.refresh();
await sandbox.pause();
await sandbox.delete();
MethodPath说明
POST/api/v1/sandboxes创建沙箱。
GET/api/v1/sandboxes/:sandboxID查询详情和运行时连接数据。
POST/api/v1/sandboxes/:sandboxID/connect重连并刷新运行时访问。
POST/api/v1/sandboxes/:sandboxID/pause暂停运行时。
POST/api/v1/sandboxes/:sandboxID/refreshes刷新 TTL。
POST/api/v1/sandboxes/:sandboxID/timeout修改秒级超时。
DELETE/api/v1/sandboxes/:sandboxID删除沙箱。
信号路由用途
沙箱详情GET /api/v1/sandboxes/:sandboxID状态、运行时 URL/Token、生命周期时间戳、timelinediagnostic
沙箱日志GET /api/v1/sandboxes/:sandboxID/logs启动输出、就绪失败、应用日志和镜像拉取问题。
沙箱指标GET /api/v1/sandboxes/:sandboxID/metrics当前 CPU、内存、磁盘、网络和任务计数。
可观测性摘要GET /api/v1/observability/summary用户/项目用量、配额检查、接口可用性和建议动作。