-
Notifications
You must be signed in to change notification settings - Fork 0
/
getElasticSearchClient.js
44 lines (40 loc) · 1.2 KB
/
getElasticSearchClient.js
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
const elasticsearch = require('elasticsearch');
// const { ELASTIC_SEARCH_URL } = process.env;
let client = null;
const connect = async (ELASTIC_SEARCH_URL) => {
client = new elasticsearch.Client({
host: ELASTIC_SEARCH_URL,
log: { type: 'stdio', levels: [] }
});
return client;
};
const ping = async () => {
let attempts = 0;
const pinger = ({ resolve, reject }) => {
attempts += 1;
client
.ping({ requestTimeout: 30000 })
.then(() => {
console.log('Elasticsearch server available');
resolve(true);
})
.catch(() => {
if (attempts > 100) reject(new Error('Elasticsearch failed to ping'));
console.log('Waiting for elasticsearch server...');
setTimeout(() => {
pinger({ resolve, reject });
}, 1000);
});
};
return new Promise((resolve, reject) => {
pinger({ resolve, reject });
});
};
module.exports = async (ELASTIC_SEARCH_URL) => {
if (client != null) {
return client;
}
client = await connect(ELASTIC_SEARCH_URL);
await ping();
return client;
};