import { Logger , ElasticsearchClient } from 'src/core/server' ; import { EsNames , getEsNames } from './names' ; import { initializeEs } from './init' ; import { ClusterClientAdapter , IClusterClientAdapter } from './cluster_client_adapter' ; import { createReadySignal , ReadySignal } from '../lib/ready_signal' ; export interface EsContext { logger : Logger ; esNames : EsNames ; esAdapter : IClusterClientAdapter ; initialize ( ) : void ; shutdown ( ) : Promise < void > ; waitTillReady ( ) : Promise < boolean > ; initialized : boolean ; } export interface EsError { readonly statusCode : number ; readonly message : string ; } export function createEsContext ( params ) { return new EsContextImpl ( params ) ; } export interface EsContextCtorParams { logger : Logger ; indexNameRoot : string ; kibanaVersion : string ; elasticsearchClientPromise : Promise < ElasticsearchClient > ; } class EsContextImpl implements EsContext { public readonly logger : Logger ; public readonly esNames : EsNames ; public esAdapter : IClusterClientAdapter ; private readonly readySignal : ReadySignal < boolean > ; public initialized : boolean ; constructor ( params ) { this . logger = params . logger ; this . esNames = getEsNames ( params . indexNameRoot , params . kibanaVersion ) ; this . readySignal = createReadySignal ( ) ; this . initialized = false ; this . esAdapter = new ClusterClientAdapter ( { logger : params . logger , elasticsearchClientPromise : params . elasticsearchClientPromise , wait : ( ) => this . readySignal . wait ( ) , } ) ; } initialize ( ) { if ( this . initialized ) return ; this . initialized = true ; this . logger . debug ( 'initializing EsContext' ) ; setImmediate ( async ( ) => { try { const success = await this . _initialize ( ) ; this . logger . debug ( `readySignal.signal(${ success })` ) ; this . readySignal . signal ( success ) ; } catch ( err ) { this . logger . debug ( 'readySignal.signal(false)' ) ; this . readySignal . signal ( false ) ; } } ) ; } async shutdown ( ) { await this . esAdapter . shutdown ( ) ; } async waitTillReady ( ) { return await this . readySignal . wait ( ) ; } private async _initialize ( ) { return await initializeEs ( this ) ; } }