import { defer , throwError , iif , timer } from 'rxjs' ; import { concatMap , retryWhen } from 'rxjs/operators' ; import { Logger } from '../../logging' ; const retryResponseStatuses = [ 503 , 401 , 403 , 408 , 410 , ] as const ; example internal export const retryCallCluster = < T extends Promise < unknown > > ( apiCaller ) : T => { return defer ( ( ) => apiCaller ( ) ) . pipe ( retryWhen ( ( errors ) => errors . pipe ( concatMap ( ( error ) => iif ( ( ) => error . name === 'NoLivingConnectionsError' , timer ( 1000 ) , throwError ( error ) ) ) ) ) ) . toPromise ( ) as T ; } ; example internal export const migrationRetryCallCluster = < T extends Promise < unknown > > ( apiCaller , log , delay = 2500 ) : T => { const previousErrors = [ ] ; return defer ( ( ) => apiCaller ( ) ) . pipe ( retryWhen ( ( errors ) => errors . pipe ( concatMap ( ( error ) => { if ( ! previousErrors . includes ( error . message ) ) { log . warn ( `Unable to connect to OpenSearch. Error: ${ error . message }` ) ; previousErrors . push ( error . message ) ; } return iif ( ( ) => error . name === 'NoLivingConnectionsError' || error . name === 'ConnectionError' || error . name === 'TimeoutError' || ( error . name === 'ResponseError' && retryResponseStatuses . includes ( error . statusCode ) ) || error ?. body ?. error ?. type === 'snapshot_in_progress_exception' , timer ( delay ) , throwError ( error ) ) ; } ) ) ) ) . toPromise ( ) as T ; } ;