Skip to content

Commit 8b69036

Browse files
fix: also retry resume (#143)
* fix: also retry resume * fix: retry resume and prevent double retries
1 parent 207dd06 commit 8b69036

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

src/Sandbox.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
vmUpdateHibernationTimeout,
99
vmUpdateSpecs,
1010
} from "./api-clients/client";
11-
import { handleResponse } from "./utils/api";
11+
import { handleResponse, retryWithDelay } from "./utils/api";
1212
import { VMTier } from "./VMTier";
1313
import { Client } from "@hey-api/client-fetch";
1414
import { connectToSandbox } from "./node";
@@ -91,7 +91,10 @@ export class Sandbox {
9191
const client = await connectToSandbox({
9292
session,
9393
getSession: async () =>
94-
this.getSession(await startVm(this.apiClient, this.id), customSession),
94+
this.getSession(
95+
await retryWithDelay(() => startVm(this.apiClient, this.id), 3, 200),
96+
customSession
97+
),
9598
});
9699

97100
if (customSession.env) {
@@ -206,7 +209,11 @@ export class Sandbox {
206209
session,
207210
getSession: async () =>
208211
this.getSession(
209-
await startVm(this.apiClient, this.id),
212+
await retryWithDelay(
213+
() => startVm(this.apiClient, this.id),
214+
3,
215+
200
216+
),
210217
customSession
211218
),
212219
})

src/Sandboxes.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,26 @@ import {
2626
StartSandboxOpts,
2727
} from "./types";
2828
import { PitcherManagerResponse } from "@codesandbox/pitcher-client";
29-
import { sleep } from "./utils/sleep";
3029

3130
export async function startVm(
3231
apiClient: Client,
3332
sandboxId: string,
3433
startOpts?: StartSandboxOpts
3534
): Promise<PitcherManagerResponse> {
36-
const startResult = await retryWithDelay(
37-
() =>
38-
vmStart({
39-
client: apiClient,
40-
body: startOpts
41-
? {
42-
ipcountry: startOpts.ipcountry,
43-
tier: startOpts.vmTier?.name,
44-
hibernation_timeout_seconds: startOpts.hibernationTimeoutSeconds,
45-
automatic_wakeup_config: startOpts.automaticWakeupConfig,
46-
}
47-
: undefined,
48-
path: {
49-
id: sandboxId,
50-
},
51-
}),
52-
3,
53-
200
54-
);
35+
const startResult = await vmStart({
36+
client: apiClient,
37+
body: startOpts
38+
? {
39+
ipcountry: startOpts.ipcountry,
40+
tier: startOpts.vmTier?.name,
41+
hibernation_timeout_seconds: startOpts.hibernationTimeoutSeconds,
42+
automatic_wakeup_config: startOpts.automaticWakeupConfig,
43+
}
44+
: undefined,
45+
path: {
46+
id: sandboxId,
47+
},
48+
});
5549

5650
const response = handleResponse(
5751
startResult,
@@ -120,7 +114,11 @@ export class Sandboxes {
120114
* Note! On CLEAN bootups the setup will run again. When hibernated a new snapshot will be created.
121115
*/
122116
async resume(sandboxId: string) {
123-
const startResponse = await startVm(this.apiClient, sandboxId);
117+
const startResponse = await retryWithDelay(
118+
() => startVm(this.apiClient, sandboxId),
119+
3,
120+
200
121+
);
124122
return new Sandbox(sandboxId, this.apiClient, startResponse);
125123
}
126124

src/bin/commands/build.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,14 @@ export const buildCommand: yargs.CommandModule<
198198
try {
199199
spinner.start(updateSpinnerMessage(index, "Starting sandbox..."));
200200

201-
const startResponse = await retryWithDelay(() =>
202-
withCustomError(
203-
startVm(apiClient, id),
204-
"Failed to start sandbox at all"
205-
)
201+
const startResponse = await retryWithDelay(
202+
() =>
203+
withCustomError(
204+
startVm(apiClient, id),
205+
"Failed to start sandbox at all"
206+
),
207+
3,
208+
200
206209
);
207210
let sandboxVM = new Sandbox(id, apiClient, startResponse);
208211

0 commit comments

Comments
 (0)