Skip to content

Commit

Permalink
Recreate REST client after starting a workspace (part 2)
Browse files Browse the repository at this point in the history
This is a followup to #431. It addresses the need to also recreate the
REST client used inside `maybeWaitForRunning`, since it makes API calls
before returning to `setup`.
  • Loading branch information
aaronlehmann committed Feb 19, 2025
1 parent 71f5ba5 commit c4f4b2a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ export class Remote {
* Try to get the workspace running. Return undefined if the user canceled.
*/
private async maybeWaitForRunning(
restClient: Api,
workspace: Workspace,
label: string,
binPath: string,
baseUrlRaw: string,
token: string,
): Promise<Workspace | undefined> {
const workspaceName = `${workspace.owner_name}/${workspace.name}`

Expand Down Expand Up @@ -94,6 +95,7 @@ export class Remote {
title: "Waiting for workspace build...",
},
async () => {
let restClient = await makeCoderSdk(baseUrlRaw, token, this.storage)
const globalConfigDir = path.dirname(this.storage.getSessionTokenPath(label))
while (workspace.latest_build.status !== "running") {
++attempts
Expand All @@ -109,6 +111,9 @@ export class Remote {
if (!(await this.confirmStart(workspaceName))) {
return undefined
}
// Recreate REST client since confirmStart may have waited an
// indeterminate amount of time for confirmation.
restClient = await makeCoderSdk(baseUrlRaw, token, this.storage)
writeEmitter = initWriteEmitterAndTerminal()
this.storage.writeToCoderOutputChannel(`Starting ${workspaceName}...`)
workspace = await startWorkspaceIfStoppedOrFailed(
Expand All @@ -126,6 +131,9 @@ export class Remote {
if (!(await this.confirmStart(workspaceName))) {
return undefined
}
// Recreate REST client since confirmStart may have waited an
// indeterminate amount of time for confirmation.
restClient = await makeCoderSdk(baseUrlRaw, token, this.storage)
writeEmitter = initWriteEmitterAndTerminal()
this.storage.writeToCoderOutputChannel(`Starting ${workspaceName}...`)
workspace = await startWorkspaceIfStoppedOrFailed(
Expand Down Expand Up @@ -316,7 +324,7 @@ export class Remote {

// If the workspace is not in a running state, try to get it running.
if (workspace.latest_build.status !== "running") {
if (!(await this.maybeWaitForRunning(workspaceRestClient, workspace, parts.label, binaryPath))) {
if (!(await this.maybeWaitForRunning(workspace, parts.label, binaryPath, baseUrlRaw, token))) {
// User declined to start the workspace.
await this.closeRemote()
} else {
Expand Down

0 comments on commit c4f4b2a

Please sign in to comment.