Merge pull request #24796 from brianphillips/search-backend-build-ordering

Split search backend plugin initialization into init and start phases
This commit is contained in:
Patrik Oldsberg
2024-05-16 16:00:41 +02:00
committed by GitHub
4 changed files with 35 additions and 16 deletions
+6
View File
@@ -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
@@ -33,19 +33,20 @@ export const searchIndexRegistryExtensionPoint: ExtensionPoint<SearchIndexRegist
// @alpha
export interface SearchIndexService {
getDocumentTypes(): Record<string, DocumentTypeInfo>;
start(options: SearchIndexServiceStartOptions): Promise<void>;
init(options: SearchIndexServiceInitOptions): void;
start(): Promise<void>;
stop(): Promise<void>;
}
// @alpha
export const searchIndexServiceRef: ServiceRef<SearchIndexService, 'plugin'>;
// @alpha
export type SearchIndexServiceStartOptions = {
export type SearchIndexServiceInitOptions = {
searchEngine: SearchEngine;
collators: RegisterCollatorParameters[];
decorators: RegisterDecoratorParameters[];
};
// @alpha
export const searchIndexServiceRef: ServiceRef<SearchIndexService, 'plugin'>;
// (No @packageDocumentation comment for this package)
```
+17 -6
View File
@@ -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<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> {
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<void> {
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();
}
+6 -5
View File
@@ -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 () => {