From 5ce928b93a09271ea69f6e978411d45a031ce313 Mon Sep 17 00:00:00 2001 From: Brian Phillips <28457+brianphillips@users.noreply.github.com> Date: Wed, 15 May 2024 11:00:33 -0500 Subject: [PATCH 1/6] 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> --- plugins/search-backend-node/src/alpha.ts | 21 ++++++++++++++++----- plugins/search-backend/src/alpha.ts | 11 ++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/plugins/search-backend-node/src/alpha.ts b/plugins/search-backend-node/src/alpha.ts index 331f0ae304..20b0f22089 100644 --- a/plugins/search-backend-node/src/alpha.ts +++ b/plugins/search-backend-node/src/alpha.ts @@ -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; + 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 { + 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 { + 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(); } diff --git a/plugins/search-backend/src/alpha.ts b/plugins/search-backend/src/alpha.ts index a17b542903..a7c2c6c872 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.build({ + searchEngine: searchEngine!, + collators, + decorators, + }); lifecycle.addStartupHook(async () => { - await searchIndexService.start({ - searchEngine: searchEngine!, - collators, - decorators, - }); + await searchIndexService.start(); }); lifecycle.addShutdownHook(async () => { From 5b6f979d8608ab80b61a71924403c3848c5c9c35 Mon Sep 17 00:00:00 2001 From: Brian Phillips <28457+brianphillips@users.noreply.github.com> Date: Wed, 15 May 2024 11:04:36 -0500 Subject: [PATCH 2/6] Add changeset Signed-off-by: Brian Phillips <28457+brianphillips@users.noreply.github.com> --- .changeset/seven-geese-raise.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/seven-geese-raise.md diff --git a/.changeset/seven-geese-raise.md b/.changeset/seven-geese-raise.md new file mode 100644 index 0000000000..176f6ae479 --- /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 "build" and "start" stages to ensure necessary initialization has happened before startup From c31ad6bee331cba5ca1e2671f5777f873ed86295 Mon Sep 17 00:00:00 2001 From: Brian Phillips <28457+brianphillips@users.noreply.github.com> Date: Wed, 15 May 2024 11:25:48 -0500 Subject: [PATCH 3/6] update API reports for search-backend-node Signed-off-by: Brian Phillips <28457+brianphillips@users.noreply.github.com> --- plugins/search-backend-node/api-report-alpha.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/plugins/search-backend-node/api-report-alpha.md b/plugins/search-backend-node/api-report-alpha.md index 8e35e70f65..4c1e70b32b 100644 --- a/plugins/search-backend-node/api-report-alpha.md +++ b/plugins/search-backend-node/api-report-alpha.md @@ -32,20 +32,21 @@ export const searchIndexRegistryExtensionPoint: ExtensionPoint; - start(options: SearchIndexServiceStartOptions): Promise; + start(): Promise; stop(): Promise; } // @alpha -export const searchIndexServiceRef: ServiceRef; - -// @alpha -export type SearchIndexServiceStartOptions = { +export type SearchIndexServiceBuildOptions = { searchEngine: SearchEngine; collators: RegisterCollatorParameters[]; decorators: RegisterDecoratorParameters[]; }; +// @alpha +export const searchIndexServiceRef: ServiceRef; + // (No @packageDocumentation comment for this package) ``` From b1ea88559cf79ef2d4061305e47dd52f7f6f67bf Mon Sep 17 00:00:00 2001 From: Brian Phillips <28457+brianphillips@users.noreply.github.com> Date: Thu, 16 May 2024 08:11:09 -0500 Subject: [PATCH 4/6] rename method per code review feedback Signed-off-by: Brian Phillips <28457+brianphillips@users.noreply.github.com> --- plugins/search-backend-node/api-report-alpha.md | 4 ++-- plugins/search-backend-node/src/alpha.ts | 8 ++++---- plugins/search-backend/src/alpha.ts | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/search-backend-node/api-report-alpha.md b/plugins/search-backend-node/api-report-alpha.md index 4c1e70b32b..4f3015a0e4 100644 --- a/plugins/search-backend-node/api-report-alpha.md +++ b/plugins/search-backend-node/api-report-alpha.md @@ -32,14 +32,14 @@ export const searchIndexRegistryExtensionPoint: ExtensionPoint; + init(options: SearchIndexServiceInitOptions): void; start(): Promise; stop(): Promise; } // @alpha -export type SearchIndexServiceBuildOptions = { +export type SearchIndexServiceInitOptions = { searchEngine: SearchEngine; collators: RegisterCollatorParameters[]; decorators: RegisterDecoratorParameters[]; diff --git a/plugins/search-backend-node/src/alpha.ts b/plugins/search-backend-node/src/alpha.ts index 20b0f22089..2a41bdbf81 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 SearchIndexServiceBuildOptions = { +export type SearchIndexServiceInitOptions = { searchEngine: SearchEngine; collators: RegisterCollatorParameters[]; decorators: RegisterDecoratorParameters[]; @@ -48,7 +48,7 @@ export interface SearchIndexService { /** * Initializes state in preparation for starting the search index service */ - build(options: SearchIndexServiceBuildOptions): void; + init(options: SearchIndexServiceInitOptions): void; /** * Starts indexing process @@ -104,7 +104,7 @@ class DefaultSearchIndexService implements SearchIndexService { return new DefaultSearchIndexService(options); } - build(options: SearchIndexServiceBuildOptions): void { + init(options: SearchIndexServiceInitOptions): void { this.indexBuilder = new IndexBuilder({ logger: this.logger, searchEngine: options.searchEngine, diff --git a/plugins/search-backend/src/alpha.ts b/plugins/search-backend/src/alpha.ts index a7c2c6c872..3a3ba82c3e 100644 --- a/plugins/search-backend/src/alpha.ts +++ b/plugins/search-backend/src/alpha.ts @@ -121,7 +121,7 @@ export default createBackendPlugin({ const collators = searchIndexRegistry.getCollators(); const decorators = searchIndexRegistry.getDecorators(); - searchIndexService.build({ + searchIndexService.init({ searchEngine: searchEngine!, collators, decorators, From 42189602af0b49a3cce3e68a4c5d159eefe9b42e Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 16 May 2024 15:33:12 +0200 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Emma Indal Signed-off-by: Patrik Oldsberg --- .changeset/seven-geese-raise.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/seven-geese-raise.md b/.changeset/seven-geese-raise.md index 176f6ae479..3d6e2347b9 100644 --- a/.changeset/seven-geese-raise.md +++ b/.changeset/seven-geese-raise.md @@ -3,4 +3,4 @@ '@backstage/plugin-search-backend': patch --- -Split backend search plugin startup into "build" and "start" stages to ensure necessary initialization has happened before startup +Split backend search plugin startup into "init" and "start" stages to ensure necessary initialization has happened before startup From 021d4cbfd1cc5deaddef68f6dcbe808deea5fbf0 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 16 May 2024 15:42:09 +0200 Subject: [PATCH 6/6] Update plugins/search-backend-node/src/alpha.ts Co-authored-by: Andre Wanlin <67169551+awanlin@users.noreply.github.com> Signed-off-by: Patrik Oldsberg --- plugins/search-backend-node/src/alpha.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/search-backend-node/src/alpha.ts b/plugins/search-backend-node/src/alpha.ts index 2a41bdbf81..6fb0c609e5 100644 --- a/plugins/search-backend-node/src/alpha.ts +++ b/plugins/search-backend-node/src/alpha.ts @@ -121,7 +121,7 @@ class DefaultSearchIndexService implements SearchIndexService { async start(): Promise { if (!this.indexBuilder) { - throw new Error('IndexBuilder is not initialized, call build first'); + throw new Error('IndexBuilder is not initialized, call init first'); } const { scheduler } = await this.indexBuilder.build(); this.scheduler = scheduler;