Просмотр исходного кода

make ElasticsearchDelegator.getInfo() to be on-demand

Yuki Takei 6 лет назад
Родитель
Сommit
7890fbd359
2 измененных файлов с 28 добавлено и 43 удалено
  1. 27 42
      src/server/service/search-delegator/elasticsearch.js
  2. 1 1
      src/server/service/search.js

+ 27 - 42
src/server/service/search-delegator/elasticsearch.js

@@ -21,9 +21,6 @@ class ElasticsearchDelegator {
     this.configManager = configManager;
     this.searchEvent = searchEvent;
 
-    this.esVersion = 'unknown';
-    this.esNodeInfos = {};
-
     this.client = null;
 
     // In Elasticsearch RegExp, we don't need to used ^ and $.
@@ -68,11 +65,33 @@ class ElasticsearchDelegator {
     this.indexName = indexName;
   }
 
-  getInfo() {
-    return {
-      esVersion: this.esVersion,
-      esNodeInfos: this.esNodeInfos,
-    };
+  async getInfo() {
+    const info = await this.client.nodes.info();
+    if (!info._nodes || !info.nodes) {
+      throw new Error('There is no nodes');
+    }
+
+    let esVersion = 'unknown';
+    const esNodeInfos = {};
+
+    for (const [nodeName, nodeInfo] of Object.entries(info.nodes)) {
+      esVersion = nodeInfo.version;
+
+      const filteredInfo = {
+        name: nodeInfo.name,
+        version: nodeInfo.version,
+        plugins: nodeInfo.plugins.map((pluginInfo) => {
+          return {
+            name: pluginInfo.name,
+            version: pluginInfo.version,
+          };
+        }),
+      };
+
+      esNodeInfos[nodeName] = filteredInfo;
+    }
+
+    return { esVersion, esNodeInfos };
   }
 
   /**
@@ -156,41 +175,7 @@ class ElasticsearchDelegator {
     await client.indices.delete({ index: tmpIndexName });
   }
 
-  /**
-   * retrieve elasticsearch node information
-   */
-  async checkESVersion() {
-    try {
-      const info = await this.client.nodes.info();
-      if (!info._nodes || !info.nodes) {
-        throw new Error('no nodes info');
-      }
-
-      for (const [nodeName, nodeInfo] of Object.entries(info.nodes)) {
-        this.esVersion = nodeInfo.version;
-
-        const filteredInfo = {
-          name: nodeInfo.name,
-          version: nodeInfo.version,
-          plugins: nodeInfo.plugins.map((pluginInfo) => {
-            return {
-              name: pluginInfo.name,
-              version: pluginInfo.version,
-            };
-          }),
-        };
-
-        this.esNodeInfos[nodeName] = filteredInfo;
-      }
-    }
-    catch (error) {
-      logger.error('Couldn\'t check ES version:', error);
-    }
-  }
-
   async initIndices() {
-    await this.checkESVersion();
-
     const { client, indexName, aliasName } = this;
 
     const tmpIndexName = `${indexName}-tmp`;

+ 1 - 1
src/server/service/search.js

@@ -64,7 +64,7 @@ class SearchService {
     tagEvent.on('update', this.delegator.syncTagChanged.bind(this.delegator));
   }
 
-  getInfo() {
+  async getInfo() {
     return this.delegator.getInfo();
   }