events-backend: more elaborate local development setup with multiple backends

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-05-25 09:38:44 +02:00
parent 79f73d9f92
commit 0393323d17
3 changed files with 90 additions and 12 deletions
+88 -12
View File
@@ -14,18 +14,60 @@
* limitations under the License.
*/
import { WinstonLogger } from '@backstage/backend-app-api';
import { createBackend } from '@backstage/backend-defaults';
import {
coreServices,
createBackendPlugin,
createServiceFactory,
} from '@backstage/backend-plugin-api';
import { mockServices } from '@backstage/backend-test-utils';
import { eventsServiceRef } from '@backstage/plugin-events-node';
const backend = createBackend();
function makeBackend(port: number) {
const backend = createBackend();
backend.add(import('../src/alpha'));
backend.add(
mockServices.rootConfig.factory({
data: {
backend: {
baseUrl: `http://localhost:${port}`,
listen: { port },
database: {
client: 'pg',
connection: {
host: 'localhost',
port: 5432,
user: process.env.USER || 'postgres',
},
},
},
},
}),
);
backend.add(
createServiceFactory({
service: coreServices.rootLogger,
deps: {},
async factory() {
return WinstonLogger.create({
meta: {
service: 'backstage',
port,
},
// level: 'debug',
format: WinstonLogger.colorFormat(),
});
},
}),
);
backend.add(import('../src/alpha'));
backend.add(
return backend;
}
const backend7008 = makeBackend(7008);
backend7008.add(
createBackendPlugin({
pluginId: 'producer',
register(reg) {
@@ -37,19 +79,29 @@ backend.add(
},
async init({ events, logger, rootLifecycle }) {
rootLifecycle.addStartupHook(async () => {
logger.info(`Publishing event to topic 'test'`);
await events.publish({
eventPayload: { foo: 'bar' },
topic: 'test',
});
const publish = () => {
logger.info(`Publishing event to topic 'test'`);
events
.publish({
eventPayload: { foo: 'bar' },
topic: 'test',
})
.catch(error => {
logger.error(`Failed to publish event from producer`, error);
});
};
publish();
setInterval(publish, 10000);
});
},
});
},
}),
);
backend7008.start();
backend.add(
const backend7009 = makeBackend(7009);
backend7009.add(
createBackendPlugin({
pluginId: 'consumer',
register(reg) {
@@ -60,10 +112,10 @@ backend.add(
},
async init({ events, logger }) {
await events.subscribe({
id: 'test-1',
id: 'test',
topics: ['test'],
async onEvent(event) {
logger.info(`Received event: ${JSON.stringify(event, null, 2)}`);
logger.info(`Received event: ${JSON.stringify(event)}`);
},
});
},
@@ -71,5 +123,29 @@ backend.add(
},
}),
);
backend7009.start();
backend.start();
const backend7010 = makeBackend(7010);
backend7010.add(
createBackendPlugin({
pluginId: 'consumer',
register(reg) {
reg.registerInit({
deps: {
events: eventsServiceRef,
logger: coreServices.logger,
},
async init({ events, logger }) {
await events.subscribe({
id: 'test',
topics: ['test'],
async onEvent(event) {
logger.info(`Received event: ${JSON.stringify(event)}`);
},
});
},
});
},
}),
);
backend7010.start();
+1
View File
@@ -67,6 +67,7 @@
"winston": "^3.2.1"
},
"devDependencies": {
"@backstage/backend-app-api": "workspace:^",
"@backstage/backend-defaults": "workspace:^",
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
+1
View File
@@ -6137,6 +6137,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@backstage/plugin-events-backend@workspace:plugins/events-backend"
dependencies:
"@backstage/backend-app-api": "workspace:^"
"@backstage/backend-common": ^0.25.0
"@backstage/backend-defaults": "workspace:^"
"@backstage/backend-openapi-utils": "workspace:^"