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

Refactor ElasticsearchDelegator: update initClient to be asynchronous and await getClient for improved handling of client initialization

Shun Miyazawa 9 месяцев назад
Родитель
Сommit
bab8dc3109

+ 17 - 10
apps/app/src/server/service/search-delegator/elasticsearch-client-delegator/get-client.ts

@@ -2,12 +2,13 @@ import type { ClientOptions as ES7ClientOptions } from '@elastic/elasticsearch7'
 import type { ClientOptions as ES8ClientOptions } from '@elastic/elasticsearch8';
 import type { ClientOptions as ES9ClientOptions } from '@elastic/elasticsearch9';
 
-import { ES7ClientDelegator } from './es7-client-delegator';
-import { ES8ClientDelegator } from './es8-client-delegator';
-import { ES9ClientDelegator } from './es9-client-delegator';
 
+import { type ES7ClientDelegator } from './es7-client-delegator';
+import { type ES8ClientDelegator } from './es8-client-delegator';
+import { type ES9ClientDelegator } from './es9-client-delegator';
 import type { ElasticSEarchClientDeletegator } from './interfaces';
 
+
 type GetDelegatorOptions = {
   version: 7;
   options: ES7ClientOptions;
@@ -36,19 +37,25 @@ type Delegator<Opts extends GetDelegatorOptions> =
 
 let instance: ElasticSEarchClientDeletegator;
 
-export const getClient = <Opts extends GetDelegatorOptions>(opts: Opts): Delegator<Opts> => {
+export const getClient = async<Opts extends GetDelegatorOptions>(opts: Opts): Promise<Delegator<Opts>> => {
   if (instance == null) {
     if (opts.version === 7) {
-      instance = new ES7ClientDelegator(opts.options, opts.rejectUnauthorized);
-      return instance as Delegator<Opts>;
+      await import('./es7-client-delegator').then(({ ES7ClientDelegator }) => {
+        instance = new ES7ClientDelegator(opts.options, opts.rejectUnauthorized);
+        return instance as Delegator<Opts>;
+      });
     }
     if (opts.version === 8) {
-      instance = new ES8ClientDelegator(opts.options, opts.rejectUnauthorized);
-      return instance as Delegator<Opts>;
+      await import('./es8-client-delegator').then(({ ES8ClientDelegator }) => {
+        instance = new ES8ClientDelegator(opts.options, opts.rejectUnauthorized);
+        return instance as Delegator<Opts>;
+      });
     }
     if (opts.version === 9) {
-      instance = new ES9ClientDelegator(opts.options, opts.rejectUnauthorized);
-      return instance as Delegator<Opts>;
+      await import('./es9-client-delegator').then(({ ES9ClientDelegator }) => {
+        instance = new ES9ClientDelegator(opts.options, opts.rejectUnauthorized);
+        return instance as Delegator<Opts>;
+      });
     }
   }
 

+ 3 - 4
apps/app/src/server/service/search-delegator/elasticsearch.ts

@@ -80,15 +80,13 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
     this.elasticsearchVersion = elasticsearchVersion;
 
     this.isElasticsearchReindexOnBoot = configManager.getConfig('app:elasticsearchReindexOnBoot');
-
-    this.initClient();
   }
 
   get aliasName(): string {
     return `${this.indexName}-alias`;
   }
 
-  initClient(): void {
+  async initClient(): Promise<void> {
     const { host, auth, indexName } = this.getConnectionInfo();
 
     const rejectUnauthorized = configManager.getConfig('app:elasticsearchRejectUnauthorized');
@@ -99,7 +97,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
       requestTimeout: configManager.getConfig('app:elasticsearchRequestTimeout'),
     };
 
-    this.client = getClient({ version: this.elasticsearchVersion, options, rejectUnauthorized });
+    this.client = await getClient({ version: this.elasticsearchVersion, options, rejectUnauthorized });
     this.indexName = indexName;
   }
 
@@ -139,6 +137,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
   }
 
   async init(): Promise<void> {
+    await this.initClient();
     const normalizeIndices = await this.normalizeIndices();
     if (this.isElasticsearchReindexOnBoot) {
       try {