move refresh loop to builder and use configured refreshInterval

Signed-off-by: Emma Indal <emma.indahl@gmail.com>
This commit is contained in:
Emma Indal
2021-04-23 15:03:32 +02:00
parent 039dbb4e20
commit cdda9c671d
2 changed files with 21 additions and 23 deletions
+1 -6
View File
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { useHotCleanup } from '@backstage/backend-common';
import { createRouter } from '@backstage/plugin-search-backend';
import {
IndexBuilder,
@@ -35,11 +34,7 @@ export default async function createPlugin({
collator: new DefaultCatalogCollator(discovery),
});
// TODO: Move refresh loop logic into the builder.
const timerId = setInterval(() => {
indexBuilder.build();
}, 60000);
useHotCleanup(module, () => clearInterval(timerId));
indexBuilder.build();
return await createRouter({
engine: indexBuilder.getSearchEngine(),
+20 -17
View File
@@ -100,28 +100,31 @@ export class IndexBuilder {
async build() {
return Promise.all(
Object.keys(this.collators).map(async type => {
const decorators: DocumentDecorator[] = (
this.decorators['*'] || []
).concat(this.decorators[type] || []);
setInterval(async () => {
const decorators: DocumentDecorator[] = (
this.decorators['*'] || []
).concat(this.decorators[type] || []);
this.logger.info(
`Collating documents for ${type} via ${this.collators[type].collate.constructor.name}`,
);
let documents = await this.collators[type].collate.execute();
for (let i = 0; i < decorators.length; i++) {
this.logger.info(
`Decorating ${type} documents via ${decorators[i].constructor.name}`,
`Collating documents for ${type} via ${this.collators[type].collate.constructor.name}`,
);
documents = await decorators[i].execute(documents);
}
let documents = await this.collators[type].collate.execute();
for (let i = 0; i < decorators.length; i++) {
this.logger.info(
`Decorating ${type} documents via ${decorators[i].constructor.name}`,
);
documents = await decorators[i].execute(documents);
}
if (!documents || documents.length === 0) {
this.logger.info(`No documents for type "${type}" to index`);
return;
}
if (!documents || documents.length === 0) {
this.logger.info(`No documents for type "${type}" to index`);
return;
}
// pushing documents to index to a configured search engine.
this.searchEngine.index(type, documents);
// 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);
}),
);
}