File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import type { Sandbox } from "../types/sandbox";
55import connectToSandbox from "./ssh" ;
66import { c } from "../theme" ;
77import { expandRepo } from "../lib/expandRepo" ;
8+ import waitUntilRunning from "../lib/waitUntilRunning" ;
89
910async 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 } ` ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { client } from "../client";
55import { env } from "../lib/env" ;
66import connectToSandbox from "./ssh" ;
77import { expandRepo } from "../lib/expandRepo" ;
8+ import waitUntilRunning from "../lib/waitUntilRunning" ;
89
910async 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 }
Original file line number Diff line number Diff line change 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 ;
You can’t perform that action at this time.
0 commit comments