From 0a032c55ce419cafc32f172eb3a301a33d29b69e Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Fri, 4 Jun 2021 19:59:27 +0200 Subject: [PATCH] Fix bug causing search to be empty initially. Signed-off-by: Eric Peterson --- packages/backend/src/plugins/search.ts | 9 ++++++++- .../search-backend-node/src/Scheduler.test.ts | 17 +++++++++++++++++ plugins/search-backend-node/src/Scheduler.ts | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/plugins/search.ts b/packages/backend/src/plugins/search.ts index 6688b9c158..e587cdb606 100644 --- a/packages/backend/src/plugins/search.ts +++ b/packages/backend/src/plugins/search.ts @@ -26,17 +26,24 @@ export default async function createPlugin({ logger, discovery, }: PluginEnvironment) { + // Initialize a connection to a search engine. const searchEngine = new LunrSearchEngine({ logger }); const indexBuilder = new IndexBuilder({ logger, searchEngine }); + // Collators are responsible for gathering documents known to plugins. This + // particular collator gathers entities from the software catalog. indexBuilder.addCollator({ defaultRefreshIntervalSeconds: 600, collator: new DefaultCatalogCollator({ discovery }), }); + // The scheduler controls when documents are gathered from collators and sent + // to the search engine for indexing. const { scheduler } = await indexBuilder.build(); - scheduler.start(); + // A 3 second delay gives the backend server a chance to initialize before + // any collators are executed, which may attempt requests against the API. + setTimeout(() => scheduler.start(), 3000); useHotCleanup(module, () => scheduler.stop()); return await createRouter({ diff --git a/plugins/search-backend-node/src/Scheduler.test.ts b/plugins/search-backend-node/src/Scheduler.test.ts index 43fdb7ea92..53a7e418e3 100644 --- a/plugins/search-backend-node/src/Scheduler.test.ts +++ b/plugins/search-backend-node/src/Scheduler.test.ts @@ -74,4 +74,21 @@ describe('Scheduler', () => { expect(mockTask2).toHaveBeenCalled(); }); }); + + describe('start', () => { + it('should execute tasks on start', () => { + const mockTask1 = jest.fn(); + const mockTask2 = jest.fn(); + + // Add tasks and interval to schedule + testScheduler.addToSchedule(mockTask1, 2); + testScheduler.addToSchedule(mockTask2, 2); + + // Starts scheduling process + testScheduler.start(); + + expect(mockTask1).toHaveBeenCalled(); + expect(mockTask2).toHaveBeenCalled(); + }); + }); }); diff --git a/plugins/search-backend-node/src/Scheduler.ts b/plugins/search-backend-node/src/Scheduler.ts index e54a837cee..a5c5712999 100644 --- a/plugins/search-backend-node/src/Scheduler.ts +++ b/plugins/search-backend-node/src/Scheduler.ts @@ -54,6 +54,8 @@ export class Scheduler { start() { this.logger.info('Starting all scheduled search tasks.'); this.schedule.forEach(({ task, interval }) => { + // Fire the task immediately, then schedule it. + task(); this.intervalTimeouts.push( setInterval(() => { task();