diff --git a/.changeset/seven-geese-raise.md b/.changeset/seven-geese-raise.md new file mode 100644 index 0000000000..3d6e2347b9 --- /dev/null +++ b/.changeset/seven-geese-raise.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-search-backend-node': patch +'@backstage/plugin-search-backend': patch +--- + +Split backend search plugin startup into "init" and "start" stages to ensure necessary initialization has happened before startup diff --git a/plugins/search-backend-node/api-report-alpha.md b/plugins/search-backend-node/api-report-alpha.md index 8e35e70f65..4f3015a0e4 100644 --- a/plugins/search-backend-node/api-report-alpha.md +++ b/plugins/search-backend-node/api-report-alpha.md @@ -33,19 +33,20 @@ export const searchIndexRegistryExtensionPoint: ExtensionPoint; - start(options: SearchIndexServiceStartOptions): Promise; + init(options: SearchIndexServiceInitOptions): void; + start(): Promise; stop(): Promise; } // @alpha -export const searchIndexServiceRef: ServiceRef; - -// @alpha -export type SearchIndexServiceStartOptions = { +export type SearchIndexServiceInitOptions = { searchEngine: SearchEngine; collators: RegisterCollatorParameters[]; decorators: RegisterDecoratorParameters[]; }; +// @alpha +export const searchIndexServiceRef: ServiceRef; + // (No @packageDocumentation comment for this package) ``` diff --git a/plugins/search-backend-node/src/alpha.ts b/plugins/search-backend-node/src/alpha.ts index 331f0ae304..6fb0c609e5 100644 --- a/plugins/search-backend-node/src/alpha.ts +++ b/plugins/search-backend-node/src/alpha.ts @@ -32,9 +32,9 @@ import { /** * @alpha - * Options for build method on {@link SearchIndexService}. + * Options for the init method on {@link SearchIndexService}. */ -export type SearchIndexServiceStartOptions = { +export type SearchIndexServiceInitOptions = { 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 + */ + init(options: SearchIndexServiceInitOptions): void; + /** * Starts indexing process */ - start(options: SearchIndexServiceStartOptions): Promise; + start(): Promise; /** * Stops indexing process */ stop(): Promise; + /** * 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 { + init(options: SearchIndexServiceInitOptions): 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 { + if (!this.indexBuilder) { + throw new Error('IndexBuilder is not initialized, call init first'); + } + const { scheduler } = await this.indexBuilder.build(); this.scheduler = scheduler; this.scheduler!.start(); } diff --git a/plugins/search-backend/src/alpha.ts b/plugins/search-backend/src/alpha.ts index a17b542903..3a3ba82c3e 100644 --- a/plugins/search-backend/src/alpha.ts +++ b/plugins/search-backend/src/alpha.ts @@ -121,13 +121,14 @@ export default createBackendPlugin({ const collators = searchIndexRegistry.getCollators(); const decorators = searchIndexRegistry.getDecorators(); + searchIndexService.init({ + searchEngine: searchEngine!, + collators, + decorators, + }); lifecycle.addStartupHook(async () => { - await searchIndexService.start({ - searchEngine: searchEngine!, - collators, - decorators, - }); + await searchIndexService.start(); }); lifecycle.addShutdownHook(async () => {