remove singleton pattern from indexer
Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
committed by
Eric Peterson
parent
51fc8de48c
commit
5a045906c2
@@ -14,23 +14,21 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { createRouter } from '@backstage/plugin-search-backend';
|
||||
import {
|
||||
collateDocuments,
|
||||
// registerCollator,
|
||||
} from '@backstage/plugin-search-backend-node';
|
||||
import { Registry } from '@backstage/plugin-search-backend-node';
|
||||
import { PluginEnvironment } from '../types';
|
||||
// import { DefaultCatalogCollator } from '@backstage/plugin-catalog-backend';
|
||||
|
||||
export default async function createPlugin({ logger }: PluginEnvironment) {
|
||||
const indexRegistry = new Registry();
|
||||
// TODO: Within this PR, update to use REST API instead of Catalog Builder.
|
||||
/* registerCollator({
|
||||
/* indexRegistry.addCollator({
|
||||
type: 'software-catalog',
|
||||
defaultRefreshIntervalSeconds: 600,
|
||||
collator: new DefaultCatalogCollator(entitiesCatalog),
|
||||
});*/
|
||||
|
||||
// TODO: Make this a more proper refresh loop.
|
||||
collateDocuments();
|
||||
indexRegistry.execute();
|
||||
|
||||
return await createRouter({
|
||||
logger,
|
||||
|
||||
@@ -19,7 +19,6 @@ import {
|
||||
DocumentDecorator,
|
||||
IndexableDocument,
|
||||
} from '@backstage/search-common';
|
||||
import { registerCollator, registerDecorator } from './';
|
||||
import { Registry } from './registry';
|
||||
|
||||
class TestDocumentCollator implements DocumentCollator {
|
||||
@@ -35,31 +34,29 @@ class TestDocumentDecorator implements DocumentDecorator {
|
||||
}
|
||||
|
||||
describe('search indexer external api', () => {
|
||||
let testRegistry: Registry;
|
||||
let testCollator: DocumentCollator;
|
||||
let testDecorator: DocumentDecorator;
|
||||
|
||||
beforeEach(() => {
|
||||
testRegistry = new Registry();
|
||||
testCollator = new TestDocumentCollator();
|
||||
testDecorator = new TestDocumentDecorator();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
Registry.getInstance()._reset();
|
||||
});
|
||||
|
||||
describe('registerCollator', () => {
|
||||
it('registers a collator', async () => {
|
||||
const collatorSpy = jest.spyOn(testCollator, 'execute');
|
||||
|
||||
// Register a collator.
|
||||
registerCollator({
|
||||
testRegistry.addCollator({
|
||||
type: 'anything',
|
||||
defaultRefreshIntervalSeconds: 600,
|
||||
collator: testCollator,
|
||||
});
|
||||
|
||||
// Execute the registry and ensure the collator was invoked.
|
||||
await Registry.getInstance().execute();
|
||||
await testRegistry.execute();
|
||||
expect(collatorSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -69,19 +66,19 @@ describe('search indexer external api', () => {
|
||||
const decoratorSpy = jest.spyOn(testDecorator, 'execute');
|
||||
|
||||
// Register a collator.
|
||||
registerCollator({
|
||||
testRegistry.addCollator({
|
||||
type: 'anything',
|
||||
defaultRefreshIntervalSeconds: 600,
|
||||
collator: testCollator,
|
||||
});
|
||||
|
||||
// Register a decorator.
|
||||
registerDecorator({
|
||||
testRegistry.addDecorator({
|
||||
decorator: testDecorator,
|
||||
});
|
||||
|
||||
// Execute the registry and ensure the decorator was invoked.
|
||||
await Registry.getInstance().execute();
|
||||
await testRegistry.execute();
|
||||
expect(decoratorSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -98,20 +95,20 @@ describe('search indexer external api', () => {
|
||||
const decoratorSpy = jest.spyOn(testDecorator, 'execute');
|
||||
|
||||
// Register a collator.
|
||||
registerCollator({
|
||||
testRegistry.addCollator({
|
||||
type: expectedType,
|
||||
defaultRefreshIntervalSeconds: 600,
|
||||
collator: testCollator,
|
||||
});
|
||||
|
||||
// Register a decorator for the same type.
|
||||
registerDecorator({
|
||||
testRegistry.addDecorator({
|
||||
types: [expectedType],
|
||||
decorator: testDecorator,
|
||||
});
|
||||
|
||||
// Execute the registry and ensure the decorator was invoked.
|
||||
await Registry.getInstance().execute();
|
||||
await testRegistry.execute();
|
||||
expect(decoratorSpy).toHaveBeenCalled();
|
||||
expect(decoratorSpy).toHaveBeenCalledWith([docFixture]);
|
||||
});
|
||||
@@ -129,20 +126,20 @@ describe('search indexer external api', () => {
|
||||
const decoratorSpy = jest.spyOn(testDecorator, 'execute');
|
||||
|
||||
// Register a collator.
|
||||
registerCollator({
|
||||
testRegistry.addCollator({
|
||||
type: expectedType,
|
||||
defaultRefreshIntervalSeconds: 600,
|
||||
collator: testCollator,
|
||||
});
|
||||
|
||||
// Register a decorator for a different type.
|
||||
registerDecorator({
|
||||
testRegistry.addDecorator({
|
||||
types: ['not-the-expected-type'],
|
||||
decorator: testDecorator,
|
||||
});
|
||||
|
||||
// Execute the registry and ensure the decorator was not invoked.
|
||||
await Registry.getInstance().execute();
|
||||
await testRegistry.execute();
|
||||
expect(decoratorSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,19 +14,4 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Registry } from './registry';
|
||||
import {
|
||||
RegisterCollatorParameters,
|
||||
RegisterDecoratorParameters,
|
||||
} from './types';
|
||||
|
||||
const registry = Registry.getInstance();
|
||||
export const registerCollator = (params: RegisterCollatorParameters) => {
|
||||
registry.addCollator(params);
|
||||
};
|
||||
export const registerDecorator = (params: RegisterDecoratorParameters) => {
|
||||
registry.addDecorator(params);
|
||||
};
|
||||
export const collateDocuments = () => {
|
||||
registry.execute();
|
||||
};
|
||||
export { Registry } from './registry';
|
||||
|
||||
@@ -29,20 +29,11 @@ export class Registry {
|
||||
private collators: Record<string, CollatorRegistryEntry>;
|
||||
private decorators: Record<string, DocumentDecorator[]>;
|
||||
|
||||
private static instance: Registry;
|
||||
|
||||
private constructor() {
|
||||
constructor() {
|
||||
this.collators = {};
|
||||
this.decorators = {};
|
||||
}
|
||||
|
||||
static getInstance(): Registry {
|
||||
if (!Registry.instance) {
|
||||
Registry.instance = new Registry();
|
||||
}
|
||||
return Registry.instance;
|
||||
}
|
||||
|
||||
addCollator({
|
||||
type,
|
||||
collator,
|
||||
@@ -84,13 +75,4 @@ export class Registry {
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method for tests. Do not use otherwise.
|
||||
* @private
|
||||
*/
|
||||
_reset() {
|
||||
this.collators = {};
|
||||
this.decorators = {};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user