diff --git a/.changeset/chilly-adults-sing.md b/.changeset/chilly-adults-sing.md new file mode 100644 index 0000000000..82f4cb88b2 --- /dev/null +++ b/.changeset/chilly-adults-sing.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Add lifecycle monitoring for the catalog processing diff --git a/.changeset/late-planes-fix.md b/.changeset/late-planes-fix.md new file mode 100644 index 0000000000..a57d7ec1d4 --- /dev/null +++ b/.changeset/late-planes-fix.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-search-backend-node': patch +'@backstage/plugin-search-backend': patch +--- + +Add lifecycle monitoring for the search index registry diff --git a/plugins/catalog-backend/src/service/CatalogPlugin.ts b/plugins/catalog-backend/src/service/CatalogPlugin.ts index 039d9368bf..dd44e20add 100644 --- a/plugins/catalog-backend/src/service/CatalogPlugin.ts +++ b/plugins/catalog-backend/src/service/CatalogPlugin.ts @@ -201,7 +201,7 @@ export const catalogPlugin = createBackendPlugin({ permissions: coreServices.permissions, database: coreServices.database, httpRouter: coreServices.httpRouter, - lifecycle: coreServices.lifecycle, + lifecycle: coreServices.rootLifecycle, scheduler: coreServices.scheduler, discovery: coreServices.discovery, auth: coreServices.auth, @@ -253,7 +253,9 @@ export const catalogPlugin = createBackendPlugin({ const { processingEngine, router } = await builder.build(); - await processingEngine.start(); + lifecycle.addStartupHook(async () => { + await processingEngine.start(); + }); lifecycle.addShutdownHook(() => processingEngine.stop()); httpRouter.use(router); }, diff --git a/plugins/search-backend-node/api-report-alpha.md b/plugins/search-backend-node/api-report-alpha.md index c8e5cbb65d..8e35e70f65 100644 --- a/plugins/search-backend-node/api-report-alpha.md +++ b/plugins/search-backend-node/api-report-alpha.md @@ -34,6 +34,7 @@ export const searchIndexRegistryExtensionPoint: ExtensionPoint; start(options: SearchIndexServiceStartOptions): Promise; + stop(): Promise; } // @alpha diff --git a/plugins/search-backend-node/src/alpha.ts b/plugins/search-backend-node/src/alpha.ts index 43bda67658..331f0ae304 100644 --- a/plugins/search-backend-node/src/alpha.ts +++ b/plugins/search-backend-node/src/alpha.ts @@ -22,11 +22,11 @@ import { LoggerService, } from '@backstage/backend-plugin-api'; import { DocumentTypeInfo } from '@backstage/plugin-search-common'; - import { IndexBuilder, RegisterCollatorParameters, RegisterDecoratorParameters, + Scheduler, SearchEngine, } from '@backstage/plugin-search-backend-node'; @@ -49,6 +49,11 @@ export interface SearchIndexService { * Starts indexing process */ start(options: SearchIndexServiceStartOptions): Promise; + + /** + * Stops indexing process + */ + stop(): Promise; /** * Returns an index types list. */ @@ -81,8 +86,9 @@ type DefaultSearchIndexServiceOptions = { * Reponsible for register the indexing task and start the schedule. */ class DefaultSearchIndexService implements SearchIndexService { - private logger: LoggerService; + private readonly logger: LoggerService; private indexBuilder: IndexBuilder | null = null; + private scheduler: Scheduler | null = null; private constructor(options: DefaultSearchIndexServiceOptions) { this.logger = options.logger; @@ -107,7 +113,15 @@ class DefaultSearchIndexService implements SearchIndexService { ); const { scheduler } = await this.indexBuilder?.build(); - scheduler.start(); + this.scheduler = scheduler; + this.scheduler!.start(); + } + + async stop(): Promise { + if (this.scheduler) { + this.scheduler.stop(); + this.scheduler = null; + } } getDocumentTypes(): Record { diff --git a/plugins/search-backend/src/alpha.ts b/plugins/search-backend/src/alpha.ts index 0ca5777102..a17b542903 100644 --- a/plugins/search-backend/src/alpha.ts +++ b/plugins/search-backend/src/alpha.ts @@ -98,6 +98,7 @@ export default createBackendPlugin({ auth: coreServices.auth, http: coreServices.httpRouter, httpAuth: coreServices.httpAuth, + lifecycle: coreServices.rootLifecycle, searchIndexService: searchIndexServiceRef, }, async init({ @@ -108,6 +109,7 @@ export default createBackendPlugin({ auth, http, httpAuth, + lifecycle, searchIndexService, }) { let searchEngine = searchEngineRegistry.getSearchEngine(); @@ -120,10 +122,16 @@ export default createBackendPlugin({ const collators = searchIndexRegistry.getCollators(); const decorators = searchIndexRegistry.getDecorators(); - await searchIndexService.start({ - searchEngine, - collators, - decorators, + lifecycle.addStartupHook(async () => { + await searchIndexService.start({ + searchEngine: searchEngine!, + collators, + decorators, + }); + }); + + lifecycle.addShutdownHook(async () => { + await searchIndexService.stop(); }); const router = await createRouter({