Преглед изворни кода

improve error handling of buildIndex method

Yuki Takei пре 6 година
родитељ
комит
b5dfd29b33
1 измењених фајлова са 33 додато и 36 уклоњено
  1. 33 36
      src/server/service/search-delegator/elasticsearch.js

+ 33 - 36
src/server/service/search-delegator/elasticsearch.js

@@ -156,45 +156,42 @@ class ElasticsearchDelegator {
 
     const tmpIndexName = `${indexName}-tmp`;
 
-    // reindex to tmp index
-    await this.createIndex(tmpIndexName);
-    await client.reindex({
-      waitForCompletion: false,
-      body: {
-        source: { index: indexName },
-        dest: { index: tmpIndexName },
-      },
-    });
-
-    // update alias
-    await client.indices.updateAliases({
-      body: {
-        actions: [
-          { add: { alias: aliasName, index: tmpIndexName } },
-          { remove: { alias: aliasName, index: indexName } },
-        ],
-      },
-    });
+    try {
+      // reindex to tmp index
+      await this.createIndex(tmpIndexName);
+      await client.reindex({
+        waitForCompletion: false,
+        body: {
+          source: { index: indexName },
+          dest: { index: tmpIndexName },
+        },
+      });
 
-    // flush index
-    await client.indices.delete({
-      index: indexName,
-    });
-    await this.createIndex(indexName);
-    await this.addAllPages();
+      // update alias
+      await client.indices.updateAliases({
+        body: {
+          actions: [
+            { add: { alias: aliasName, index: tmpIndexName } },
+            { remove: { alias: aliasName, index: indexName } },
+          ],
+        },
+      });
 
-    // update alias
-    await client.indices.updateAliases({
-      body: {
-        actions: [
-          { add: { alias: aliasName, index: indexName } },
-          { remove: { alias: aliasName, index: tmpIndexName } },
-        ],
-      },
-    });
+      // flush index
+      await client.indices.delete({
+        index: indexName,
+      });
+      await this.createIndex(indexName);
+      await this.addAllPages();
+    }
+    catch (err) {
+      logger.warn('An error occured while \'buildIndex\', normalize indices anyway.');
+      throw err;
+    }
+    finally {
+      await this.normalizeIndices();
+    }
 
-    // remove tmp index
-    await client.indices.delete({ index: tmpIndexName });
   }
 
   async normalizeIndices() {