-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
context.ts
84 lines (70 loc) · 2.34 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Logger, ClusterClient } 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 type EsClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'asScoped'>;
export interface EsContext {
logger: Logger;
esNames: EsNames;
esAdapter: IClusterClientAdapter;
initialize(): void;
waitTillReady(): Promise<boolean>;
initialized: boolean;
}
export interface EsError {
readonly statusCode: number;
readonly message: string;
}
export function createEsContext(params: EsContextCtorParams): EsContext {
return new EsContextImpl(params);
}
export interface EsContextCtorParams {
logger: Logger;
clusterClientPromise: Promise<EsClusterClient>;
indexNameRoot: string;
}
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: EsContextCtorParams) {
this.logger = params.logger;
this.esNames = getEsNames(params.indexNameRoot);
this.readySignal = createReadySignal();
this.initialized = false;
this.esAdapter = new ClusterClientAdapter({
logger: params.logger,
clusterClientPromise: params.clusterClientPromise,
});
}
initialize() {
// only run the initialization method once
if (this.initialized) return;
this.initialized = true;
this.logger.debug('initializing EsContext');
setImmediate(async () => {
try {
await this._initialize();
this.logger.debug('readySignal.signal(true)');
this.readySignal.signal(true);
} catch (err) {
this.logger.debug('readySignal.signal(false)');
this.readySignal.signal(false);
}
});
}
async waitTillReady(): Promise<boolean> {
return await this.readySignal.wait();
}
private async _initialize() {
await initializeEs(this);
}
}