@@ -13,7 +13,7 @@ import {
13
13
getStartOptions ,
14
14
getStartResponse ,
15
15
handleResponse ,
16
- withCustomTimeout ,
16
+ retryWithDelay ,
17
17
} from "./utils/api" ;
18
18
19
19
import {
@@ -33,22 +33,24 @@ export async function startVm(
33
33
sandboxId : string ,
34
34
startOpts ?: StartSandboxOpts
35
35
) : Promise < PitcherManagerResponse > {
36
- const startResult = await withCustomTimeout ( ( signal ) =>
37
- vmStart ( {
38
- client : apiClient ,
39
- body : startOpts
40
- ? {
41
- ipcountry : startOpts . ipcountry ,
42
- tier : startOpts . vmTier ?. name ,
43
- hibernation_timeout_seconds : startOpts . hibernationTimeoutSeconds ,
44
- automatic_wakeup_config : startOpts . automaticWakeupConfig ,
45
- }
46
- : undefined ,
47
- path : {
48
- id : sandboxId ,
49
- } ,
50
- signal,
51
- } )
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
52
54
) ;
53
55
54
56
const response = handleResponse (
@@ -91,7 +93,6 @@ export class Sandboxes {
91
93
description : opts ?. description ,
92
94
tags : tagsWithSdk ,
93
95
path,
94
- start_options : getStartOptions ( opts ) ,
95
96
} ,
96
97
path : {
97
98
id : templateId ,
@@ -100,11 +101,13 @@ export class Sandboxes {
100
101
101
102
const sandbox = handleResponse ( result , "Failed to create sandbox" ) ;
102
103
103
- return new Sandbox (
104
- sandbox . id ,
105
- this . apiClient ,
106
- getStartResponse ( sandbox . start_response )
104
+ const startResponse = await retryWithDelay (
105
+ ( ) => startVm ( this . apiClient , sandbox . id , getStartOptions ( opts ) ) ,
106
+ 3 ,
107
+ 200
107
108
) ;
109
+
110
+ return new Sandbox ( sandbox . id , this . apiClient , startResponse ) ;
108
111
}
109
112
110
113
/**
@@ -125,14 +128,16 @@ export class Sandboxes {
125
128
* Shuts down a sandbox. Files will be saved, and the sandbox will be stopped.
126
129
*/
127
130
async shutdown ( sandboxId : string ) : Promise < void > {
128
- const response = await withCustomTimeout ( ( signal ) =>
129
- vmShutdown ( {
130
- client : this . apiClient ,
131
- path : {
132
- id : sandboxId ,
133
- } ,
134
- signal,
135
- } )
131
+ const response = await retryWithDelay (
132
+ ( ) =>
133
+ vmShutdown ( {
134
+ client : this . apiClient ,
135
+ path : {
136
+ id : sandboxId ,
137
+ } ,
138
+ } ) ,
139
+ 3 ,
140
+ 200
136
141
) ;
137
142
138
143
handleResponse ( response , `Failed to shutdown sandbox ${ sandboxId } ` ) ;
@@ -156,53 +161,39 @@ export class Sandboxes {
156
161
* Will resolve once the sandbox is restarted with its setup running.
157
162
*/
158
163
public async restart ( sandboxId : string , opts ?: StartSandboxOpts ) {
159
- let didRestart = false ;
160
-
161
- for ( let attempt = 1 ; attempt <= 3 ; attempt ++ ) {
162
- try {
163
- await this . shutdown ( sandboxId ) ;
164
- didRestart = true ;
165
- break ;
166
- } catch ( e ) {
167
- await sleep ( 500 ) ;
168
- }
164
+ try {
165
+ await this . shutdown ( sandboxId ) ;
166
+ } catch ( e ) {
167
+ throw new Error ( "Failed to shutdown VM, " + String ( e ) ) ;
169
168
}
170
169
171
- if ( ! didRestart ) {
172
- throw new Error ( "Failed to shutdown VM after 3 attempts" ) ;
173
- }
174
-
175
- let startResponse : PitcherManagerResponse | undefined ;
176
-
177
- for ( let attempt = 1 ; attempt <= 3 ; attempt ++ ) {
178
- try {
179
- startResponse = await startVm ( this . apiClient , sandboxId , opts ) ;
180
- break ;
181
- } catch ( e ) {
182
- await sleep ( 500 ) ;
183
- }
184
- }
170
+ try {
171
+ const startResponse = await retryWithDelay (
172
+ ( ) => startVm ( this . apiClient , sandboxId , opts ) ,
173
+ 3 ,
174
+ 200
175
+ ) ;
185
176
186
- if ( ! startResponse ) {
187
- throw new Error ( "Failed to start VM after 3 attempts" ) ;
177
+ return new Sandbox ( sandboxId , this . apiClient , startResponse ) ;
178
+ } catch ( e ) {
179
+ throw new Error ( "Failed to start VM, " + String ( e ) ) ;
188
180
}
189
-
190
- return new Sandbox ( sandboxId , this . apiClient , startResponse ) ;
191
181
}
192
-
193
182
/**
194
183
* Hibernates a sandbox. Files will be saved, and the sandbox will be put to sleep. Next time
195
184
* you resume the sandbox it will continue from the last state it was in.
196
185
*/
197
186
async hibernate ( sandboxId : string ) : Promise < void > {
198
- const response = await withCustomTimeout ( ( signal ) =>
199
- vmHibernate ( {
200
- client : this . apiClient ,
201
- path : {
202
- id : sandboxId ,
203
- } ,
204
- signal,
205
- } )
187
+ const response = await retryWithDelay (
188
+ ( ) =>
189
+ vmHibernate ( {
190
+ client : this . apiClient ,
191
+ path : {
192
+ id : sandboxId ,
193
+ } ,
194
+ } ) ,
195
+ 3 ,
196
+ 200
206
197
) ;
207
198
208
199
handleResponse ( response , `Failed to hibernate sandbox ${ sandboxId } ` ) ;
0 commit comments