@@ -149,11 +149,32 @@ export function sleep(ms: number): Promise<void> {
149149 return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
150150}
151151
152+ export async function assertClusterIsAvailable (
153+ session : Session ,
154+ projectId : string ,
155+ clusterName : string
156+ ) : Promise < boolean > {
157+ try {
158+ await session . apiClient . getCluster ( {
159+ params : {
160+ path : {
161+ groupId : projectId ,
162+ clusterName,
163+ } ,
164+ } ,
165+ } ) ;
166+ return true ;
167+ } catch {
168+ return false ;
169+ }
170+ }
171+
152172export async function deleteAndWaitCluster (
153173 session : Session ,
154174 projectId : string ,
155175 clusterName : string ,
156- pollingInterval : number = 1000
176+ pollingInterval : number = 1000 ,
177+ maxPollingIterations : number = 300
157178) : Promise < void > {
158179 await session . apiClient . deleteCluster ( {
159180 params : {
@@ -163,31 +184,28 @@ export async function deleteAndWaitCluster(
163184 } ,
164185 } ,
165186 } ) ;
166- while ( true ) {
167- try {
168- await session . apiClient . getCluster ( {
169- params : {
170- path : {
171- groupId : projectId ,
172- clusterName,
173- } ,
174- } ,
175- } ) ;
176- await sleep ( pollingInterval ) ;
177- } catch {
178- break ;
187+
188+ for ( let i = 0 ; i < maxPollingIterations ; i ++ ) {
189+ const isAvailable = await assertClusterIsAvailable ( session , projectId , clusterName ) ;
190+ if ( ! isAvailable ) {
191+ return ;
179192 }
193+ await sleep ( pollingInterval ) ;
180194 }
195+ throw new Error (
196+ `Cluster deletion timeout: ${ clusterName } did not delete within ${ maxPollingIterations } iterations`
197+ ) ;
181198}
182199
183200export async function waitCluster (
184201 session : Session ,
185202 projectId : string ,
186203 clusterName : string ,
187204 check : ( cluster : ClusterDescription20240805 ) => boolean | Promise < boolean > ,
188- pollingInterval : number = 1000
205+ pollingInterval : number = 1000 ,
206+ maxPollingIterations : number = 300
189207) : Promise < void > {
190- while ( true ) {
208+ for ( let i = 0 ; i < maxPollingIterations ; i ++ ) {
191209 const cluster = await session . apiClient . getCluster ( {
192210 params : {
193211 path : {
@@ -201,4 +219,8 @@ export async function waitCluster(
201219 }
202220 await sleep ( pollingInterval ) ;
203221 }
222+
223+ throw new Error (
224+ `Cluster wait timeout: ${ clusterName } did not meet condition within ${ maxPollingIterations } iterations`
225+ ) ;
204226}
0 commit comments