message: refactor build method in indexBuilder
diff: import { DocumentCollator, DocumentDecorator } from '@backstage/search-common';
  import { Logger } from 'winston';
  -
  +import { Scheduler } from './index';
  import {
  RegisterCollatorParameters,
  RegisterDecoratorParameters,
  @@ -92,15 +92,15 @@ export class IndexBuilder {
  }
  /**
  - * Starts the process of executing collators and decorators and building the
  - * search index.
  - *
  - * TODO: But like with coordination, timing, error handling, and what have you.
  + * Compiles collators and decorators into tasks, which are added to a
  + * scheduler returned to the caller.
  */
  - async build() {
  - return Promise.all(
  - Object.keys(this.collators).map(async type => {
  - setInterval(async () => {
  + async build(): Promise<{ scheduler: Scheduler }> {
  + const scheduler = new Scheduler({ logger: this.logger });
  +
  + Object.keys(this.collators).map(type => {
  + scheduler.addToSchedule(async () => {
  + // Collate, Decorate, Index.
  const decorators: DocumentDecorator[] = (
  this.decorators['*'] || []
  ).concat(this.decorators[type] || []);
  @@ -123,9 +123,11 @@ export class IndexBuilder {
  // pushing documents to index to a configured search engine.
  this.searchEngine.index(type, documents);
  - // refreshInterval configured in seconds, setInterval want milliseconds
  }, this.collators[type].refreshInterval * 1000);
  - }),
  - );
  + });
  +
  + return {
  + scheduler,
  + };
  }
  }
  