Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getTimeout methods to the SDKs #517

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions packages/js-sdk/src/api/schema.gen.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/js-sdk/src/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,15 @@ export class Sandbox extends SandboxApi {
await Sandbox.kill(this.sandboxId, { ...this.connectionConfig, ...opts })
}

async getTimeout(opts?: Pick<SandboxOpts, 'requestTimeoutMs'>) {
const { endAt } = await Sandbox.get(this.sandboxId, {
...this.connectionConfig,
...opts,
})

return endAt
}

/**
* Get the URL to upload a file to the sandbox.
*
Expand Down
52 changes: 52 additions & 0 deletions packages/js-sdk/src/sandbox/sandboxApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export interface SandboxInfo {
* Sandbox start time.
*/
startedAt: Date

/**
* Sandbox end time.
*/
endAt: Date
}

export class SandboxApi {
Expand Down Expand Up @@ -80,6 +85,52 @@ export class SandboxApi {
return true
}

/**
* Get running sandbox.
*
* @param sandboxId sandbox ID.
* @param opts connection options.
*
* @returns running sandbox.
*/
static async get(
sandboxId: string,
opts?: SandboxApiOpts
): Promise<SandboxInfo> {
const config = new ConnectionConfig(opts)
const client = new ApiClient(config)

const res = await client.api.GET('/sandboxes/{sandboxID}', {
params: {
path: {
sandboxID: sandboxId,
},
},
signal: config.getSignal(opts?.requestTimeoutMs),
})

const err = handleApiError(res)
if (err) {
throw err
}

if (!res.data) {
throw new Error('Sandbox not found')
}

return {
sandboxId: this.getSandboxId({
sandboxId: res.data.sandboxID,
clientId: res.data.clientID,
}),
templateId: res.data.templateID,
...(res.data.alias && { name: res.data.alias }),
metadata: res.data.metadata ?? {},
startedAt: new Date(res.data.startedAt),
endAt: new Date(res.data.endAt),
}
}

/**
* List all running sandboxes.
*
Expand Down Expand Up @@ -110,6 +161,7 @@ export class SandboxApi {
...(sandbox.alias && { name: sandbox.alias }),
metadata: sandbox.metadata ?? {},
startedAt: new Date(sandbox.startedAt),
endAt: new Date(sandbox.endAt),
})) ?? []
)
}
Expand Down
20 changes: 14 additions & 6 deletions packages/js-sdk/tests/sandbox/timeout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ sandboxTest.skipIf(isDebug)('shorten timeout', async ({ sandbox }) => {
expect(await sandbox.isRunning({ requestTimeoutMs: 1000 })).toBeFalsy()
})

sandboxTest.skipIf(isDebug)('shorten then lenghten timeout', async ({ sandbox }) => {
await sandbox.setTimeout(5000)
sandboxTest.skipIf(isDebug)(
'shorten then lenghten timeout',
async ({ sandbox }) => {
await sandbox.setTimeout(5000)

await wait(1000)
await wait(1000)

await sandbox.setTimeout(10000)
await sandbox.setTimeout(10000)

await wait(6000)
await wait(6000)

await sandbox.isRunning()
}
)

await sandbox.isRunning()
sandboxTest.skipIf(isDebug)('get sandbox timeout', async ({ sandbox }) => {
const timeout = await sandbox.getTimeout()
expect(timeout).toBeInstanceOf(Date)
})
Loading
Loading