-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
context.ts
99 lines (83 loc) · 2.79 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* 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 { createReadySignal, ReadySignal } from '../lib/ready_signal';
export type EsClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'asScoped'>;
export interface EsContext {
logger: Logger;
esNames: EsNames;
initialize(): void;
waitTillReady(): Promise<boolean>;
callEs(operation: string, body?: any): Promise<any>;
}
export interface EsError {
readonly statusCode: number;
readonly message: string;
}
export function createEsContext(params: EsContextCtorParams): EsContext {
return new EsContextImpl(params);
}
export interface EsContextCtorParams {
logger: Logger;
clusterClient: EsClusterClient;
indexNameRoot: string;
}
class EsContextImpl implements EsContext {
public readonly logger: Logger;
public readonly esNames: EsNames;
private readonly clusterClient: EsClusterClient;
private readonly readySignal: ReadySignal<boolean>;
private initialized: boolean;
constructor(params: EsContextCtorParams) {
this.logger = params.logger;
this.esNames = getEsNames(params.indexNameRoot);
this.clusterClient = params.clusterClient;
this.readySignal = createReadySignal();
this.initialized = false;
}
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();
}
async callEs(operation: string, body?: any): Promise<any> {
try {
this.debug(`callEs(${operation}) calls:`, body);
const result = await this.clusterClient.callAsInternalUser(operation, body);
this.debug(`callEs(${operation}) result:`, result);
return result;
} catch (err) {
this.debug(`callEs(${operation}) error:`, {
message: err.message,
statusCode: err.statusCode,
});
throw err;
}
}
private async _initialize() {
await initializeEs(this);
}
private debug(message: string, object?: any) {
const objectString = object == null ? '' : JSON.stringify(object);
this.logger.debug(`esContext: ${message} ${objectString}`);
}
}