Skip to content

Commit 568f6ee

Browse files
authored
Add sb.terminate({ wait: true }) parameter to JS (#277)
1 parent 22cd3d2 commit 568f6ee

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

modal-js/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export { Retries } from "./retries";
6363
export type {
6464
SandboxExecParams,
6565
SandboxFromNameParams,
66+
SandboxTerminateParams,
6667
SandboxCreateConnectCredentials,
6768
SandboxCreateConnectTokenParams,
6869
StdioBehavior,

modal-js/src/sandbox.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff 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+
582587
export 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
/**

modal-js/test/legacy/proxy.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App, Proxy, Sandbox } from "modal";
1+
import { App, Proxy } from "modal";
22
import { expect, test } from "vitest";
33

44
test("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

2623
test("ProxyNotFound", async () => {

modal-js/test/legacy/sandbox.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff 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

2320
test("PassCatToStdin", async () => {

modal-js/test/sandbox.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff 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

3431
test("PassCatToStdin", async () => {

0 commit comments

Comments
 (0)