Split search index service startup into a build and start phase

Fixes #24794. The build process takes place synchronously to the plugin
initialization rather than at startup so that the HTTP router has the
relevant types available to validate incoming requests.

Signed-off-by: Brian Phillips <28457+brianphillips@users.noreply.github.com>
This commit is contained in:
Brian Phillips
2024-05-15 11:00:33 -05:00
parent ea1e845f1d
commit 5ce928b93a
2 changed files with 22 additions and 10 deletions
+16 -5
View File
@@ -34,7 +34,7 @@ import {
* @alpha
* Options for build method on {@link SearchIndexService}.
*/
export type SearchIndexServiceStartOptions = {
export type SearchIndexServiceBuildOptions = {
searchEngine: SearchEngine;
collators: RegisterCollatorParameters[];
decorators: RegisterDecoratorParameters[];
@@ -45,15 +45,21 @@ export type SearchIndexServiceStartOptions = {
* Interface for implementation of index service.
*/
export interface SearchIndexService {
/**
* Initializes state in preparation for starting the search index service
*/
build(options: SearchIndexServiceBuildOptions): void;
/**
* Starts indexing process
*/
start(options: SearchIndexServiceStartOptions): Promise<void>;
start(): Promise<void>;
/**
* Stops indexing process
*/
stop(): Promise<void>;
/**
* Returns an index types list.
*/
@@ -83,7 +89,7 @@ type DefaultSearchIndexServiceOptions = {
/**
* @alpha
* Reponsible for register the indexing task and start the schedule.
* Responsible for register the indexing task and start the schedule.
*/
class DefaultSearchIndexService implements SearchIndexService {
private readonly logger: LoggerService;
@@ -98,7 +104,7 @@ class DefaultSearchIndexService implements SearchIndexService {
return new DefaultSearchIndexService(options);
}
async start(options: SearchIndexServiceStartOptions): Promise<void> {
build(options: SearchIndexServiceBuildOptions): void {
this.indexBuilder = new IndexBuilder({
logger: this.logger,
searchEngine: options.searchEngine,
@@ -111,8 +117,13 @@ class DefaultSearchIndexService implements SearchIndexService {
options.decorators.forEach(decorator =>
this.indexBuilder?.addDecorator(decorator),
);
}
const { scheduler } = await this.indexBuilder?.build();
async start(): Promise<void> {
if (!this.indexBuilder) {
throw new Error('IndexBuilder is not initialized, call build first');
}
const { scheduler } = await this.indexBuilder.build();
this.scheduler = scheduler;
this.scheduler!.start();
}