File tree Expand file tree Collapse file tree 5 files changed +21
-15
lines changed
Expand file tree Collapse file tree 5 files changed +21
-15
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,7 @@ export { Retries } from "./retries";
6363export type {
6464 SandboxExecParams ,
6565 SandboxFromNameParams ,
66+ SandboxTerminateParams ,
6667 SandboxCreateConnectCredentials ,
6768 SandboxCreateConnectTokenParams ,
6869 StdioBehavior ,
Original file line number Diff line number Diff line change @@ -579,6 +579,11 @@ export type SandboxExecParams = {
579579} ;
580580
581581/** Optional parameters for {@link Sandbox#createConnectToken Sandbox.createConnectToken()}. */
582+ export type SandboxTerminateParams = {
583+ /** If true, wait for the Sandbox to finish and return the exit code. */
584+ wait ?: boolean ;
585+ } ;
586+
582587export type SandboxCreateConnectTokenParams = {
583588 /** Optional user-provided metadata string that will be added to the headers by the proxy when forwarding requests to the Sandbox. */
584589 userMetadata ?: string ;
@@ -1018,11 +1023,20 @@ export class Sandbox {
10181023 return { url : resp . url , token : resp . token } ;
10191024 }
10201025
1021- async terminate ( ) : Promise < void > {
1026+ async terminate ( ) : Promise < void > ;
1027+ async terminate ( params : { wait : true } ) : Promise < number > ;
1028+ async terminate ( params ?: SandboxTerminateParams ) : Promise < number | void > {
10221029 this . #ensureAttached( ) ;
10231030 await this . #client. cpClient . sandboxTerminate ( { sandboxId : this . sandboxId } ) ;
1024- this . #taskId = undefined ; // Reset task ID after termination
1031+
1032+ let exitCode : number | undefined ;
1033+ if ( params ?. wait ) {
1034+ exitCode = await this . wait ( ) ;
1035+ }
1036+
1037+ this . #taskId = undefined ;
10251038 this . detach ( ) ;
1039+ return exitCode ;
10261040 }
10271041
10281042 /**
Original file line number Diff line number Diff line change 1- import { App , Proxy , Sandbox } from "modal" ;
1+ import { App , Proxy } from "modal" ;
22import { expect , test } from "vitest" ;
33
44test ( "CreateSandboxWithProxy" , async ( ) => {
@@ -17,10 +17,7 @@ test("CreateSandboxWithProxy", async () => {
1717 } ) ;
1818 expect ( sb . sandboxId ) . toBeTruthy ( ) ;
1919
20- await sb . terminate ( ) ;
21-
22- const sbFromId = await Sandbox . fromId ( sb . sandboxId ) ;
23- expect ( await sbFromId . wait ( ) ) . toBe ( 137 ) ;
20+ expect ( await sb . terminate ( { wait : true } ) ) . toBe ( 137 ) ;
2421} ) ;
2522
2623test ( "ProxyNotFound" , async ( ) => {
Original file line number Diff line number Diff line change @@ -14,10 +14,7 @@ test("CreateOneSandbox", async () => {
1414
1515 const sb = await app . createSandbox ( image ) ;
1616 expect ( sb . sandboxId ) . toBeTruthy ( ) ;
17- await sb . terminate ( ) ;
18-
19- const sbFromId = await Sandbox . fromId ( sb . sandboxId ) ;
20- expect ( await sbFromId . wait ( ) ) . toBe ( 137 ) ;
17+ expect ( await sb . terminate ( { wait : true } ) ) . toBe ( 137 ) ;
2118} ) ;
2219
2320test ( "PassCatToStdin" , async ( ) => {
Original file line number Diff line number Diff line change @@ -25,10 +25,7 @@ test("CreateOneSandbox", async () => {
2525
2626 const sb = await tc . sandboxes . create ( app , image ) ;
2727 expect ( sb . sandboxId ) . toBeTruthy ( ) ;
28- await sb . terminate ( ) ;
29-
30- const sbFromId = await tc . sandboxes . fromId ( sb . sandboxId ) ;
31- expect ( await sbFromId . wait ( ) ) . toBe ( 137 ) ;
28+ expect ( await sb . terminate ( { wait : true } ) ) . toBe ( 137 ) ;
3229} ) ;
3330
3431test ( "PassCatToStdin" , async ( ) => {
You can’t perform that action at this time.
0 commit comments