|
1 | 1 | import { |
2 | 2 | isAxiosError, |
| 3 | + isCancel, |
3 | 4 | type AxiosHeaders, |
4 | 5 | type AxiosInstance, |
5 | 6 | type AxiosResponseHeaders, |
6 | 7 | type AxiosResponseTransformer, |
7 | 8 | } from "axios"; |
8 | 9 | import { Api } from "coder/site/src/api/api"; |
| 10 | +import { setTimeout as sleep } from "node:timers/promises"; |
9 | 11 | import * as vscode from "vscode"; |
10 | 12 |
|
11 | 13 | import { isSameOrigin } from "../common/url"; |
@@ -172,27 +174,52 @@ export class CoderApi extends Api implements vscode.Disposable { |
172 | 174 | /** |
173 | 175 | * Reimplemented because the SDK version polls inside a voided IIFE that |
174 | 176 | * swallows errors, hanging callers forever if a poll throws (e.g. on |
175 | | - * failed response validation). |
| 177 | + * failed response validation). Builds outlast network blips, so up to |
| 178 | + * `MAX_POLL_FAILURES` transient failures in a row are polled through. |
176 | 179 | */ |
177 | 180 | override waitForBuild = async ( |
178 | 181 | build: WorkspaceBuild, |
| 182 | + signal?: AbortSignal, |
179 | 183 | ): Promise<ProvisionerJob | undefined> => { |
| 184 | + let failures = 0; |
180 | 185 | while (true) { |
181 | | - const { job } = await this.getWorkspaceBuildByNumber( |
182 | | - build.workspace_owner_name, |
183 | | - build.workspace_name, |
184 | | - build.build_number, |
| 186 | + const job = await this.getWorkspaceBuild(build.id, signal).then( |
| 187 | + (polled) => { |
| 188 | + failures = 0; |
| 189 | + return polled.job; |
| 190 | + }, |
| 191 | + (error: unknown) => { |
| 192 | + if (!isTransientError(error) || ++failures > MAX_POLL_FAILURES) { |
| 193 | + throw error; |
| 194 | + } |
| 195 | + return undefined; |
| 196 | + }, |
185 | 197 | ); |
186 | | - if (job.status === "failed") { |
| 198 | + if (job?.status === "failed") { |
187 | 199 | throw new Error(`Build ${build.build_number} failed`); |
188 | 200 | } |
189 | | - if (job.status === "succeeded" || job.status === "canceled") { |
| 201 | + if (job?.status === "succeeded" || job?.status === "canceled") { |
190 | 202 | return job; |
191 | 203 | } |
192 | | - await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 204 | + await sleep(1000, undefined, { signal }); |
193 | 205 | } |
194 | 206 | }; |
195 | 207 |
|
| 208 | + /** |
| 209 | + * Reads a build by ID, as `coder` does while it waits. Reads by owner and |
| 210 | + * name 404 once a delete completes, because they skip deleted workspaces. |
| 211 | + */ |
| 212 | + getWorkspaceBuild = async ( |
| 213 | + buildId: string, |
| 214 | + signal?: AbortSignal, |
| 215 | + ): Promise<WorkspaceBuild> => { |
| 216 | + const { data } = await this.getAxiosInstance().get<WorkspaceBuild>( |
| 217 | + `/api/v2/workspacebuilds/${buildId}`, |
| 218 | + { signal }, |
| 219 | + ); |
| 220 | + return data; |
| 221 | + }; |
| 222 | + |
196 | 223 | hasAuthConfigChangedSince(version: number | undefined): boolean { |
197 | 224 | return this.authConfigTracker.hasChangedSince(version); |
198 | 225 | } |
@@ -738,6 +765,17 @@ function wrapWithValidation(api: CoderApi): void { |
738 | 765 | } |
739 | 766 | } |
740 | 767 |
|
| 768 | +const MAX_POLL_FAILURES = 3; |
| 769 | + |
| 770 | +/** No response, or a 5xx; a cancelled request is not transient. */ |
| 771 | +function isTransientError(error: unknown): boolean { |
| 772 | + return ( |
| 773 | + isAxiosError(error) && |
| 774 | + !isCancel(error) && |
| 775 | + (!error.response || error.response.status >= 500) |
| 776 | + ); |
| 777 | +} |
| 778 | + |
741 | 779 | function getSize(headers: AxiosHeaders, data: unknown): number | undefined { |
742 | 780 | const contentLength = headers["content-length"] as unknown; |
743 | 781 | if (typeof contentLength === "string") { |
|
0 commit comments