Skip to content

Commit d96e5ad

Browse files
committed
Add waitUntilRunning and use before SSH
Introduce waitUntilRunning helper that polls getSandbox until the sandbox status is RUNNING (default 60s timeout, 2s interval). Invoke it in create and start before connecting via SSH. Also unify auth token usage in start to pass it to the helper
1 parent 81d39ab commit d96e5ad

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

apps/cli/src/cmd/create.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Sandbox } from "../types/sandbox";
55
import connectToSandbox from "./ssh";
66
import { c } from "../theme";
77
import { expandRepo } from "../lib/expandRepo";
8+
import waitUntilRunning from "../lib/waitUntilRunning";
89

910
async function createSandbox(
1011
name: string,
@@ -52,6 +53,7 @@ async function createSandbox(
5253
);
5354
return;
5455
}
56+
await waitUntilRunning(sandbox.data.name, token);
5557
await connectToSandbox(sandbox.data.name);
5658
} catch (error) {
5759
consola.error(`Failed to create sandbox: ${error}`);

apps/cli/src/cmd/start.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { client } from "../client";
55
import { env } from "../lib/env";
66
import connectToSandbox from "./ssh";
77
import { expandRepo } from "../lib/expandRepo";
8+
import waitUntilRunning from "../lib/waitUntilRunning";
89

910
async function start(
1011
name: string,
@@ -14,6 +15,8 @@ async function start(
1415
if (repo) repo = expandRepo(repo);
1516

1617
try {
18+
const authToken = env.POCKETENV_TOKEN || token;
19+
1720
await client.post(
1821
"/xrpc/io.pocketenv.sandbox.startSandbox",
1922
{
@@ -24,12 +27,13 @@ async function start(
2427
id: name,
2528
},
2629
headers: {
27-
Authorization: `Bearer ${env.POCKETENV_TOKEN || token}`,
30+
Authorization: `Bearer ${authToken}`,
2831
},
2932
},
3033
);
3134

3235
if (ssh) {
36+
await waitUntilRunning(name, authToken);
3337
await connectToSandbox(name);
3438
return;
3539
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { client } from "../client";
2+
import type { Sandbox } from "../types/sandbox";
3+
4+
async function waitUntilRunning(
5+
name: string,
6+
authToken: string,
7+
timeoutMs = 60_000,
8+
intervalMs = 2_000,
9+
): Promise<void> {
10+
const deadline = Date.now() + timeoutMs;
11+
while (Date.now() < deadline) {
12+
const response = await client.get<{ sandbox: Sandbox | null }>(
13+
"/xrpc/io.pocketenv.sandbox.getSandbox",
14+
{
15+
params: { id: name },
16+
headers: { Authorization: `Bearer ${authToken}` },
17+
},
18+
);
19+
if (response.data.sandbox?.status === "RUNNING") return;
20+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
21+
}
22+
throw new Error(
23+
`Sandbox ${name} did not reach RUNNING state within ${timeoutMs / 1000}s`,
24+
);
25+
}
26+
27+
export default waitUntilRunning;

0 commit comments

Comments
 (0)